model selection in sklean

cross_val_score

import numpy
from sklearn import svm, model_selection, datasets

digit = datasets.load_digits()
x = digit.data
y = digit.target

cs = numpy.logspace(-10, 0, 11)
mean_scores = []
std_scores = []
for c in cs:
    scores = model_selection.cross_val_score(svm.SVC(kernel='linear', C=c), x, y, cv=10, n_jobs=-1)
    mean_scores.append(numpy.mean(scores))
    std_scores.append(numpy.std(scores))

import matplotlib.pyplot as plt
plt.figure()
plt.semilogx(cs, mean_scores)
plt.semilogx(cs, numpy.array(mean_scores)+numpy.array(std_scores), 'b--')
plt.semilogx(cs, numpy.array(mean_scores)-numpy.array(std_scores), 'b--')
plt.xlabel('C')
plt.ylabel('score')
plt.show()
GridSearchCV
import numpy
from sklearn import svm, model_selection, datasets

digit = datasets.load_digits()
x = digit.data
y = digit.target

Cs = numpy.logspace(-10, 0, 11)
gs = model_selection.GridSearchCV(svm.SVC(kernel='linear'), dict(C=Cs), n_jobs=-1)
gs.fit(x, y)
mean_scores = gs.cv_results_['mean_train_score']
std_scores = gs.cv_results_['std_train_score']

import matplotlib.pyplot as plt
plt.figure()
plt.semilogx(Cs, mean_scores)
plt.semilogx(Cs, numpy.array(mean_scores)+numpy.array(std_scores), 'b--')
plt.semilogx(Cs, numpy.array(mean_scores)-numpy.array(std_scores), 'b--')
plt.xlabel('C')
plt.ylabel('score')
plt.plot(gs.best_estimator_.C, gs.best_score_, 'o')
plt.show()

 

你可能感兴趣的:(model selection in sklean)