Scikit-Learn


Multiclass & Multilabel fitting



Yüklə 1,92 Mb.
Pdf görüntüsü
səhifə27/124
tarix23.02.2023
ölçüsü1,92 Mb.
#101394
1   ...   23   24   25   26   27   28   29   30   ...   124
sklearn

Multiclass & Multilabel fitting 
In case of multiclass fitting, both learning and the prediction tasks are dependent on the 
format of the target data fit upon. The module used is 
sklearn.multiclass

Check the 
example below, where multiclass classifier is fit on a 1d array. 
from sklearn.svm import SVC 
from sklearn.multiclass import OneVsRestClassifier 
from sklearn.preprocessing import LabelBinarizer 
X = [[1, 2], [3, 4], [4, 5], [5, 2], [1, 1]] 


Scikit-Learn
29 
y = [0, 0, 1, 1, 2] 
classif = OneVsRestClassifier(estimator=SVC(gamma='scale',random_state=0)) 
classif.fit(X, y).predict(X) 
Output 
array([0, 0, 1, 1, 2]) 
In the above example, classifier is fit on one dimensional array of multiclass labels and the 
predict() 
method hence provides corresponding multiclass prediction. But on the other 
hand, it is also possible to fit upon a two-dimensional array of binary label indicators as 
follows: 
from sklearn.svm import SVC 
from sklearn.multiclass import OneVsRestClassifier 
from sklearn.preprocessing import LabelBinarizer 
X = [[1, 2], [3, 4], [4, 5], [5, 2], [1, 1]] 
y = LabelBinarizer().fit_transform(y) 
classif.fit(X, y).predict(X) 
Output 
array([[0, 0, 0], 
[0, 0, 0], 
[0, 1, 0], 
[0, 1, 0], 
[0, 0, 0]]) 
Similarly, in case of multilabel fitting, an instance can be assigned multiple labels as 
follows: 
from sklearn.preprocessing import MultiLabelBinarizer 
y = [[0, 1], [0, 2], [1, 3], [0, 2, 3], [2, 4]] 
y = MultiLabelBinarizer().fit_transform(y) 
classif.fit(X, y).predict(X) 
Output 
array([[1, 0, 1, 0, 0], 
[1, 0, 1, 0, 0], 
[1, 0, 1, 1, 0], 


Scikit-Learn
30 
[1, 0, 1, 1, 0], 
[1, 0, 1, 0, 0]]) 
In the above example, 

Yüklə 1,92 Mb.

Dostları ilə paylaş:
1   ...   23   24   25   26   27   28   29   30   ...   124




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə