Multiclass Classification

One-vs-All


In [1]:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import LabelBinarizer

In [2]:
X = [[1,2], [2,4], [4,5], [3,2], [3,1]]
y = [0, 0, 1, 1, 2]

In [3]:
classifier = OneVsRestClassifier(estimator=SVC(random_state=0))
classifier.fit(X,y).predict(X)


Out[3]:
array([0, 0, 1, 1, 2])

In [4]:
X = np.random.random(100)
Y = np.random.random(100)
X1 = np.where(Y <= 0.5)
X2 = np.where(Y > 0.5)
plt.scatter(X[X1],Y[X1], c='red')
plt.scatter(X[X2],Y[X2], c='blue')
plt.show()



In [6]:
Y1 = np.ones(X1[0].size)

In [7]:
Y2 = 2*np.ones(X2[0].size)

In [8]:
classifier = OneVsRestClassifier(SVC(kernel='linear'))
classifier.fit(X1,Y1)


/home/argyle/.local/lib/python3.6/site-packages/sklearn/multiclass.py:76: UserWarning: Label not 1.0 is present in all training examples.
  str(classes[c]))
Out[8]:
OneVsRestClassifier(estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False),
          n_jobs=1)

In [9]:
[X,Y][0]


Out[9]:
array([0.98274913, 0.87096002, 0.43352222, 0.2467874 , 0.94825179,
       0.38836739, 0.40183119, 0.17865769, 0.32010443, 0.18979345,
       0.63880158, 0.41459047, 0.86737186, 0.34350463, 0.15700424,
       0.64223173, 0.5172487 , 0.84002242, 0.85620314, 0.65102208,
       0.41821324, 0.64376098, 0.59021199, 0.44213175, 0.5275839 ,
       0.05479569, 0.83737646, 0.24613794, 0.82940021, 0.86501734,
       0.27294441, 0.77828812, 0.45221314, 0.5101498 , 0.24699385,
       0.26020361, 0.5254884 , 0.86220826, 0.50484228, 0.34400023,
       0.31120894, 0.8315049 , 0.71450381, 0.97373881, 0.54593548,
       0.66502738, 0.32655956, 0.06183302, 0.04343859, 0.15834699,
       0.35642182, 0.13168792, 0.47578724, 0.32660047, 0.12613645,
       0.61344449, 0.14313765, 0.06243968, 0.43184136, 0.73540989,
       0.05688792, 0.49348011, 0.9973419 , 0.49951856, 0.50293529,
       0.76068125, 0.69197011, 0.81679532, 0.68440574, 0.47596034,
       0.8469228 , 0.39709575, 0.56344013, 0.38158749, 0.27895585,
       0.52393202, 0.41579921, 0.45640001, 0.61530153, 0.97024249,
       0.8543469 , 0.95428963, 0.4213718 , 0.57468436, 0.45600325,
       0.50629001, 0.83307268, 0.9289533 , 0.81558948, 0.16510387,
       0.79722614, 0.94187913, 0.99607408, 0.89502191, 0.9498347 ,
       0.75907316, 0.37158433, 0.01532592, 0.23593598, 0.23938244])

In [ ]: