User Tools

Site Tools


code:multi_class

Multi-class classification in scikit-learn

Let's use a One-vs-the-rest classifier on the iris dataset. The data has four features that describe features of three types of iris flowers.

multi_class.py
 
import numpy as np
from sklearn import datasets
from sklearn.multiclass import OneVsRestClassifier,OneVsOneClassifier
from sklearn.svm import LinearSVC,SVC
from sklearn import cross_validation
 
# load the iris dataset:
 
iris = datasets.load_iris()
X, y = iris.data, iris.target
 
# prepare cross validation folds
cv = cross_validation.StratifiedKFold(y, 5, shuffle=True, random_state=0)
 
# one-vs-the-rest
classifier = OneVsRestClassifier(LinearSVC())
print (np.mean(cross_validation.cross_val_score(classifier, X, y, cv=cv)))
# one-vs-one
classifier = OneVsOneClassifier(LinearSVC())
print (np.mean(cross_validation.cross_val_score(classifier, X, y, cv=cv)))
 
# does this mean that one-vs-one is better?  not necessarily...
classifier = OneVsRestClassifier(SVC(C=1, kernel='rbf', gamma=0.5))
print (np.mean(cross_validation.cross_val_score(classifier, X, y, cv=cv)))
code/multi_class.txt ยท Last modified: 2016/10/11 12:57 by asa