**In this blog we are creating our own dataset.**

## Import required libraries

```
# For mathematical calculation
import numpy as np
# For plotting graphs
import matplotlib.pyplot as plt
# Import the sklearn for SVM
from sklearn import svm
# For creating datasets
from sklearn.datasets import make_circles
```

## Create dataset

```
df, value = make_circles(n_samples=500,
noise=.05,factor=.5)
```

## Plot dataset

```
# Plot the dataset
plt.scatter(df[:,0],df[:,1],c=
```value)
plt.show()

` `

## Train and test the model.

```
# Calculate the higher dimension value
x = df[:,0]
y = df[:,1]
z = x**2 + y**2
kernals = ['linear','poly','rbf']
training_set = np.c_[x,y]
# Train and predict for each kernal
for kernal in kernals:
clf=svm.SVC(kernel=kernal,
```gamma=2)
# Train the model
clf.fit(training_set,value)
# Test the model
prediction = clf.predict([[-0.4,-0.4]])
print prediction
'''
Output:
[0] - linear kernal
[1] - polynomial kernal
[1] - rbf kernal
'''
.
.
.

## Plot various kernals

```
.
.
.
# plot the line, the points,
# and the nearest vectors to the plane
X = training_set
y = value
X0 = X[np.where(y == 0)]
X1 = X[np.where(y == 1)]
plt.figure()
x_min = X[:, 0].min()
x_max = X[:, 0].max()
y_min = X[:, 1].min()
y_max = X[:, 1].max()
XX, YY = np.mgrid[x_min:x_max:200j
, y_min:y_max:200j]
Z = clf.decision_function(np.c_[
```XX.ravel()
, YY.ravel()])
# Put the result into a color plot
Z = Z.reshape(XX.shape)
plt.pcolormesh(XX, YY, Z > 0
, cmap=plt.cm.Paired)
plt.contour(XX, YY, Z, colors=['k', 'k', 'k'],
linestyles=['--', '-', '--'],
levels=[-.5, 0, .5])
plt.scatter(X0[:, 0], X0[:, 1], c='r',s=50)
plt.scatter(X1[:, 0], X1[:, 1], c='b',s=50)
title = ('SVC with {} kernal').format(kernal)
plt.title(title)
plt.show()

**(See how rbf kernal clearly classifies the dataset)**

## No comments:

## Post a Comment