Tuesday, 19 September 2017

Support Vector Machine (SVM)-Types

Support Vector Machine (SVM)-Types and their output

 Support Vector Machine (SVM) technique is tested here to see their accuracy in terms of output.

Python program:


>>> import numpy as np
>>> x = np.array([[-2, 2], [-3, 2], [1, 2], [3, 3]])
>>> y = np.array([1, 2, 2, 3])
>>> from sklearn.svm import NuSVC
>>> from sklearn.svm import SVC, LinearSVC
>>> clf1 = SVC()
>>> clf1.fit(x,y)
Output:

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

>>> print(clf1.predict([[-0.8, -1]]))
[2]
>>> clf2=NuSVC()
>>> clf2.fit(x, y)
Output:

NuSVC(cache_size=200, class_weight=None, coef0=0.0,
   decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
   max_iter=-1, nu=0.5, probability=False, random_state=None,
   shrinking=True, tol=0.001, verbose=False)
>>> plt.show()
>>> print(clf2.predict([[-0.8, -1]]))
[2]
>>> clf3=LinearSVC()
>>> clf3.fit(x, y)
Output:

LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
     intercept_scaling=1, loss='squared_hinge', max_iter=1000,
     multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
     verbose=0)
>>> print(clf3.predict([[-0.8, -1]]))
[2]


Plots for SVC, LinearSVC & NuSVC
>>> from sklearn.svm import NuSVC
>>> from sklearn.svm import SVC, LinearSVC
>>> import numpy as np
>>> import matplotlib.pyplot as plt

>>> xx, yy = np.meshgrid(np.linspace(-3, 3, 500), np.linspace(-3, 3, 500))
>>> np.random.seed(0)

>>> x = np.random.randn(300, 2)
>>> y = np.logical_xor(x[:, 0]>0, x[:, 1]>0)
>>> clf1 = svm.SVC()
>>> clf2 = svm.LinearSVC()
>>> clf3 = svm.NuSVC()

>>> clf1.fit(x, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

>>> clf2.fit(x, y)
LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
     intercept_scaling=1, loss='squared_hinge', max_iter=1000,
     multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
     verbose=0)

>>> clf3.fit(x, y)
NuSVC(cache_size=200, class_weight=None, coef0=0.0,
   decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
   max_iter=-1, nu=0.5, probability=False, random_state=None,
   shrinking=True, tol=0.001, verbose=False)

>>> z1 = clf1.decision_function(np.c_[xx.ravel(), yy.ravel()])
>>> z2 = clf2.decision_function(np.c_[xx.ravel(), yy.ravel()])
>>> z3 = clf3.decision_function(np.c_[xx.ravel(), yy.ravel()])

>>> z1 = z1.reshape(xx.shape)
>>> z2 = z2.reshape(xx.shape)
>>> z3 = z3.reshape(xx.shape)


Plot for SVC

>>> plt.imshow(z1, interpolation="nearest", extent=(xx.min(), xx.max(), yy.min(), yy.max()), aspect='auto', origin='lower', cmap=plt.cm.bwr_r)
<matplotlib.image.AxesImage object at 0x000001DD5C2AC160>
>>> contours3 = plt.contour(xx, yy, z1, levels=[0], linewidth=2, linetypes='--')
>>> plt.scatter(x[:,0], x[:, 1], s=30, c=y, cmap=plt.cm.Paired, edgecolors='k')
<matplotlib.collections.PathCollection object at 0x000001DD7087D860>
>>> plt.xticks(())
([], <a list of 0 Text xticklabel objects>)
>>> plt.yticks(())
([], <a list of 0 Text yticklabel objects>)
>>> plt.axis([-3, 3, -3, 3])
[-3, 3, -3, 3]
>>> plt.show()

SVC plot


Plot for Linear SVC

>>> plt.imshow(z2, interpolation="nearest", extent=(xx.min(), xx.max(), yy.min(), yy.max()), aspect='auto', origin='lower', cmap=plt.cm.bwr_r)
<matplotlib.image.AxesImage object at 0x000001DD6CC291D0>
>>> contours3 = plt.contour(xx, yy, z2, levels=[0], linewidth=2, linetypes='--')
>>> plt.scatter(x[:,0], x[:, 1], s=30, c=y, cmap=plt.cm.Paired, edgecolors='k')
<matplotlib.collections.PathCollection object at 0x000001DD6CBC6668>
>>> plt.xticks(())
([], <a list of 0 Text xticklabel objects>)
>>> plt.yticks(())
([], <a list of 0 Text yticklabel objects>)
>>> plt.axis([-3, 3, -3, 3])
[-3, 3, -3, 3]
>>> plt.show()
LinearSVC plot

Plot for NuSVC

>>> plt.imshow(z3, interpolation="nearest", extent=(xx.min(), xx.max(), yy.min(), yy.max()), aspect='auto', origin='lower', cmap=plt.cm.bwr_r)
<matplotlib.image.AxesImage object at 0x000001DD72C61CC0>
>>> contours3 = plt.contour(xx, yy, z3, levels=[0], linewidth=2, linetypes='--')
>>> plt.scatter(x[:,0], x[:, 1], s=30, c=y, cmap=plt.cm.Paired, edgecolors='k')
<matplotlib.collections.PathCollection object at 0x000001DD00CD0D68>
>>> plt.xticks(())
([], <a list of 0 Text xticklabel objects>)
>>> plt.yticks(())
([], <a list of 0 Text yticklabel objects>)
>>> plt.axis([-3, 3, -3, 3])
[-3, 3, -3, 3]
>>> plt.show()

NuSVC plot

No comments:

Post a Comment