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:model_selection [CS545 fall 2016]

User Tools

Site Tools


code:model_selection

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

model selection in scikit-learn

<code python>

“”“classifier evaluation using scikit-learn

more details at: http://scikit-learn.org/stable/modules/cross_validation.html http://scikit-learn.org/stable/tutorial/statistical_inference/model_selection.html ”“”

import numpy as np from sklearn import cross_validation from sklearn import svm from sklearn import metrics

# read in the heart dataset

data=np.genfromtxt(“../data/heart_scale.data”, delimiter=“,”) X=data[:,1:] y=data[:,0]

# first let's do regular cross-validation:

cv = cross_validation.StratifiedKFold(y, 5, shuffle=True, random_state=0) print (cv.test_folds)

classifier = svm.SVC(kernel='linear', C=1)

y_predict = cross_validation.cross_val_predict(classifier, X, y, cv=cv) print(metrics.accuracy_score(y, y_predict))

# grid search

# let's perform model selection using grid search

from sklearn.grid_search import GridSearchCV Cs = np.logspace(-2, 3, 6) classifier = GridSearchCV(estimator=svm.LinearSVC(), param_grid=dict(C=Cs) ) classifier.fit(X, y)

# print the best accuracy, classifier and parameters: print (classifier.best_score_) print (classifier.best_estimator_) print (classifier.best_params_)

# performing nested cross validation:

y_predict = cross_validation.cross_val_predict(classifier, X, y, cv=cv) print(metrics.accuracy_score(y, y_predict))

# if we want to do grid search over multiple parameters: param_grid = [

{'C': [1, 10, 100, 1000], 'kernel': ['linear']},
{'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},

] classifier = GridSearchCV(estimator=svm.SVC(), param_grid=param_grid)

y_predict = cross_validation.cross_val_predict(classifier, X, y, cv=cv) print(metrics.accuracy_score(y, y_predict))

</file>

code/model_selection.1475787500.txt.gz · Last modified: 2016/10/06 14:58 by asa