User Tools

Site Tools


code:multi_class

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
code:multi_class [2016/08/09 10:25]
127.0.0.1 external edit
code:multi_class [2016/10/11 12:57] (current)
asa
Line 3: Line 3:
 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> +<file python ​multi_class.py>
-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>​ 
  
 import numpy as np import numpy as np
Line 44: Line 11:
 from sklearn.svm import LinearSVC,​SVC from sklearn.svm import LinearSVC,​SVC
 from sklearn import cross_validation from sklearn import cross_validation
 +
 +# load the iris dataset:
 +
 iris = datasets.load_iris() iris = datasets.load_iris()
 X, y = iris.data, iris.target 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()) classifier = OneVsRestClassifier(LinearSVC())
- +print (np.mean(cross_validation.cross_val_score(classifier,​ X, y, cv=cv))) 
-print np.mean(cross_validation.cross_val_score(classifier,​ X, y, cv=5)) +# one-vs-one
 classifier = OneVsOneClassifier(LinearSVC()) classifier = OneVsOneClassifier(LinearSVC())
 +print (np.mean(cross_validation.cross_val_score(classifier,​ X, y, cv=cv)))
  
-print np.mean(cross_validation.cross_val_score(classifier,​ X, y, cv=5)) +# does this mean that one-vs-one ​is better? ​ not necessarily...
- +
-# does this mean that oneVsOne ​is better? ​ not necessarily... +
 classifier = OneVsRestClassifier(SVC(C=1,​ kernel='​rbf',​ gamma=0.5)) classifier = OneVsRestClassifier(SVC(C=1,​ kernel='​rbf',​ gamma=0.5))
- +print (np.mean(cross_validation.cross_val_score(classifier,​ X, y, cv=cv)))
-print np.mean(cross_validation.cross_val_score(classifier,​ X, y, cv=5)) +
  
 </​file>​ </​file>​
code/multi_class.txt · Last modified: 2016/10/11 12:57 by asa