This shows you the differences between two versions of the page.
Next revision Both sides next revision | |||
code:multi_class [2015/10/05 15:13] asa created |
code:multi_class [2015/10/08 14:12] asa |
||
---|---|---|---|
Line 2: | Line 2: | ||
Let's use a One-vs-the-rest classifier on the [[https://archive.ics.uci.edu/ml/datasets/Iris | iris dataset]]. The data has four features that describe features of three types of iris flowers. | Let's use a One-vs-the-rest classifier on the [[https://archive.ics.uci.edu/ml/datasets/Iris | iris dataset]]. The data has four features that describe features of three types of iris flowers. | ||
+ | |||
+ | <code python> | ||
+ | In [1]: import numpy as np | ||
+ | |||
+ | In [2]: from sklearn import datasets | ||
+ | |||
+ | In [3]: from sklearn.multiclass import OneVsRestClassifier,OneVsOneClassifier | ||
+ | |||
+ | In [4]: from sklearn.svm import LinearSVC,SVC | ||
+ | |||
+ | In [5]: from sklearn import cross_validation | ||
+ | |||
+ | In [6]: iris = datasets.load_iris() | ||
+ | |||
+ | In [7]: X, y = iris.data, iris.target | ||
+ | |||
+ | In [8]: classifier = OneVsRestClassifier(LinearSVC()) | ||
+ | |||
+ | In [9]: print np.mean(cross_validation.cross_val_score(classifier, X, y, cv=5)) | ||
+ | 0.966666666667 | ||
+ | |||
+ | In [10]: classifier = OneVsOneClassifier(LinearSVC()) | ||
+ | |||
+ | In [11]: print np.mean(cross_validation.cross_val_score(classifier, X, y, cv=5))0.98 | ||
+ | |||
+ | In [12]: # does this mean that oneVsOne is better? not necessarily... | ||
+ | |||
+ | In [13]: classifier = OneVsRestClassifier(SVC(C=1, kernel='rbf', gamma=0.5)) | ||
+ | |||
+ | In [14]: print np.mean(cross_validation.cross_val_score(classifier, X, y, cv=5))0.98 | ||
+ | |||
+ | </code> | ||
+ | |||
+ | And here's the code without the python prompts to get in the way: | ||
<file python multi_class.py> | <file python multi_class.py> | ||
+ | import numpy as np | ||
from sklearn import datasets | from sklearn import datasets | ||
- | from sklearn.multiclass import OneVsRestClassifier | + | from sklearn.multiclass import OneVsRestClassifier,OneVsOneClassifier |
- | from sklearn.svm import LinearSVC | + | from sklearn.svm import LinearSVC,SVC |
from sklearn import cross_validation | from sklearn import cross_validation | ||
iris = datasets.load_iris() | iris = datasets.load_iris() | ||
X, y = iris.data, iris.target | X, y = iris.data, iris.target | ||
+ | |||
classifier = OneVsRestClassifier(LinearSVC()) | classifier = OneVsRestClassifier(LinearSVC()) | ||
- | print cross_validation.cross_val_score(classifier, X, y, cv=5) | + | print np.mean(cross_validation.cross_val_score(classifier, X, y, cv=5)) |
+ | |||
+ | classifier = OneVsOneClassifier(LinearSVC()) | ||
+ | |||
+ | print np.mean(cross_validation.cross_val_score(classifier, X, y, cv=5)) | ||
+ | |||
+ | # does this mean that oneVsOne 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=5)) | ||
</file> | </file> |