Warning: Declaration of action_plugin_tablewidth::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /s/bach/b/class/cs545/public_html/fall16/lib/plugins/tablewidth/action.php on line 93
code:multi_class [CS545 fall 2016]

User Tools

Site Tools


code:multi_class

This is an old revision of the document!



Warning: Declaration of syntax_plugin_comment::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /s/bach/b/class/cs545/public_html/fall16/lib/plugins/comment/syntax.php on line 19

Warning: Declaration of syntax_plugin_comment::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /s/bach/b/class/cs545/public_html/fall16/lib/plugins/comment/syntax.php on line 19

Warning: Declaration of syntax_plugin_fontsize2::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /s/bach/b/class/cs545/public_html/fall16/lib/plugins/fontsize2/syntax.php on line 116

Warning: Declaration of syntax_plugin_fontsize2::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /s/bach/b/class/cs545/public_html/fall16/lib/plugins/fontsize2/syntax.php on line 116

Warning: Declaration of syntax_plugin_tablewidth::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /s/bach/b/class/cs545/public_html/fall16/lib/plugins/tablewidth/syntax.php on line 57

Warning: Declaration of syntax_plugin_tablewidth::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /s/bach/b/class/cs545/public_html/fall16/lib/plugins/tablewidth/syntax.php on line 57

Warning: Declaration of syntax_plugin_mathjax_protecttex::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /s/bach/b/class/cs545/public_html/fall16/lib/plugins/mathjax/syntax/protecttex.php on line 157

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.

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

And here's the code without the python prompts to get in the way:

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
iris = datasets.load_iris()
X, y = iris.data, iris.target
 
classifier = OneVsRestClassifier(LinearSVC())
 
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))
code/multi_class.1470759925.txt.gz ยท Last modified: 2016/10/11 12:57 (external edit)