本文整理匯總了Python中sklearn.cross_validation.cross_val_score方法的典型用法代碼示例。如果您正苦於以下問題:Python cross_validation.cross_val_score方法的具體用法?Python cross_validation.cross_val_score怎麽用?Python cross_validation.cross_val_score使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.cross_validation
的用法示例。
在下文中一共展示了cross_validation.cross_val_score方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_cross_val_score_multilabel
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def test_cross_val_score_multilabel():
X = np.array([[-3, 4], [2, 4], [3, 3], [0, 2], [-3, 1],
[-2, 1], [0, 0], [-2, -1], [-1, -2], [1, -2]])
y = np.array([[1, 1], [0, 1], [0, 1], [0, 1], [1, 1],
[0, 1], [1, 0], [1, 1], [1, 0], [0, 0]])
clf = KNeighborsClassifier(n_neighbors=1)
scoring_micro = make_scorer(precision_score, average='micro')
scoring_macro = make_scorer(precision_score, average='macro')
scoring_samples = make_scorer(precision_score, average='samples')
score_micro = cval.cross_val_score(clf, X, y, scoring=scoring_micro, cv=5)
score_macro = cval.cross_val_score(clf, X, y, scoring=scoring_macro, cv=5)
score_samples = cval.cross_val_score(clf, X, y,
scoring=scoring_samples, cv=5)
assert_almost_equal(score_micro, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 3])
assert_almost_equal(score_macro, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 4])
assert_almost_equal(score_samples, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 4])
示例2: classify
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def classify(X, y, cl, name=''):
"""Classification using gene features"""
from sklearn.metrics import classification_report, accuracy_score
np.random.seed()
ind = np.random.permutation(len(X))
from sklearn.cross_validation import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.4)
#print X
cl.fit(Xtrain, ytrain)
ypred = cl.predict(Xtest)
print (classification_report(ytest, ypred))
#print accuracy_score(ytest, ypred)
from sklearn import cross_validation
yl = pd.Categorical(y).labels
sc = cross_validation.cross_val_score(cl, X, yl, scoring='roc_auc', cv=5)
print("AUC: %0.2f (+/- %0.2f)" % (sc.mean(), sc.std() * 2))
return cl
示例3: run_model
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def run_model(model,dtrain,predictor_var,target,scoring_method='mean_squared_error'):
cv_method = KFold(len(dtrain),5)
cv_scores = cross_val_score(model,dtrain[predictor_var],dtrain[target],cv=cv_method,scoring=scoring_method)
#print cv_scores, np.mean(cv_scores), np.sqrt((-1)*np.mean(cv_scores))
dtrain_for_val = dtrain[dtrain['Year']<2000]
dtest_for_val = dtrain[dtrain['Year']>1999]
#cv_method = KFold(len(dtrain_for_val),5)
#cv_scores_2 = cross_val_score(model,dtrain_for_val[predictor_var],dtrain_for_val[target],cv=cv_method,scoring=scoring_method)
#print cv_scores_2, np.mean(cv_scores_2)
dtrain_for_val_ini = dtrain_for_val[predictor_var]
dtest_for_val_ini = dtest_for_val[predictor_var]
model.fit(dtrain_for_val_ini,dtrain_for_val[target])
pred_for_val = model.predict(dtest_for_val_ini)
#print math.sqrt(mean_squared_error(dtest_for_val['Footfall'],pred_for_val))
示例4: test_cross_val_score_mask
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def test_cross_val_score_mask():
# test that cross_val_score works with boolean masks
svm = SVC(kernel="linear")
iris = load_iris()
X, y = iris.data, iris.target
cv_indices = cval.KFold(len(y), 5)
scores_indices = cval.cross_val_score(svm, X, y, cv=cv_indices)
cv_indices = cval.KFold(len(y), 5)
cv_masks = []
for train, test in cv_indices:
mask_train = np.zeros(len(y), dtype=np.bool)
mask_test = np.zeros(len(y), dtype=np.bool)
mask_train[train] = 1
mask_test[test] = 1
cv_masks.append((train, test))
scores_masks = cval.cross_val_score(svm, X, y, cv=cv_masks)
assert_array_equal(scores_indices, scores_masks)
示例5: test_cross_val_score_precomputed
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def test_cross_val_score_precomputed():
# test for svm with precomputed kernel
svm = SVC(kernel="precomputed")
iris = load_iris()
X, y = iris.data, iris.target
linear_kernel = np.dot(X, X.T)
score_precomputed = cval.cross_val_score(svm, linear_kernel, y)
svm = SVC(kernel="linear")
score_linear = cval.cross_val_score(svm, X, y)
assert_array_equal(score_precomputed, score_linear)
# Error raised for non-square X
svm = SVC(kernel="precomputed")
assert_raises(ValueError, cval.cross_val_score, svm, X, y)
# test error is raised when the precomputed kernel is not array-like
# or sparse
assert_raises(ValueError, cval.cross_val_score, svm,
linear_kernel.tolist(), y)
示例6: test_cross_val_score_with_score_func_classification
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def test_cross_val_score_with_score_func_classification():
iris = load_iris()
clf = SVC(kernel='linear')
# Default score (should be the accuracy score)
scores = cval.cross_val_score(clf, iris.data, iris.target, cv=5)
assert_array_almost_equal(scores, [0.97, 1., 0.97, 0.97, 1.], 2)
# Correct classification score (aka. zero / one score) - should be the
# same as the default estimator score
zo_scores = cval.cross_val_score(clf, iris.data, iris.target,
scoring="accuracy", cv=5)
assert_array_almost_equal(zo_scores, [0.97, 1., 0.97, 0.97, 1.], 2)
# F1 score (class are balanced so f1_score should be equal to zero/one
# score
f1_scores = cval.cross_val_score(clf, iris.data, iris.target,
scoring="f1_weighted", cv=5)
assert_array_almost_equal(f1_scores, [0.97, 1., 0.97, 0.97, 1.], 2)
示例7: test_cross_val_score_with_score_func_regression
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def test_cross_val_score_with_score_func_regression():
X, y = make_regression(n_samples=30, n_features=20, n_informative=5,
random_state=0)
reg = Ridge()
# Default score of the Ridge regression estimator
scores = cval.cross_val_score(reg, X, y, cv=5)
assert_array_almost_equal(scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2)
# R2 score (aka. determination coefficient) - should be the
# same as the default estimator score
r2_scores = cval.cross_val_score(reg, X, y, scoring="r2", cv=5)
assert_array_almost_equal(r2_scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2)
# Mean squared error; this is a loss function, so "scores" are negative
neg_mse_scores = cval.cross_val_score(reg, X, y, cv=5,
scoring="neg_mean_squared_error")
expected_neg_mse = np.array([-763.07, -553.16, -274.38, -273.26, -1681.99])
assert_array_almost_equal(neg_mse_scores, expected_neg_mse, 2)
# Explained variance
scoring = make_scorer(explained_variance_score)
ev_scores = cval.cross_val_score(reg, X, y, cv=5, scoring=scoring)
assert_array_almost_equal(ev_scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2)
示例8: estimate_model
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def estimate_model(positive_data_matrix=None,
negative_data_matrix=None,
target=None,
estimator=None,
n_jobs=4):
"""estimate_model."""
X, y = make_data_matrix(positive_data_matrix=positive_data_matrix,
negative_data_matrix=negative_data_matrix,
target=target)
logger.info('Test set')
logger.info(describe(X))
logger.info('-' * 80)
logger.info('Test Estimate')
predictions = estimator.predict(X)
margins = estimator.decision_function(X)
logger.info(classification_report(y, predictions))
apr = average_precision_score(y, margins)
logger.info('APR: %.3f' % apr)
roc = roc_auc_score(y, margins)
logger.info('ROC: %.3f' % roc)
logger.info('Cross-validated estimate')
scoring_strings = ['accuracy', 'precision', 'recall', 'f1',
'average_precision', 'roc_auc']
for scoring in scoring_strings:
scores = cross_validation.cross_val_score(
estimator, X, y, cv=5,
scoring=scoring, n_jobs=n_jobs)
logger.info('%20s: %.3f +- %.3f' % (scoring,
np.mean(scores),
np.std(scores)))
return roc, apr
示例9: evaluate_cross_validation
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def evaluate_cross_validation(clf, X, y, K):
# create a k-fold cross validation iterator
cv = KFold(len(y), K, shuffle=True, random_state=0)
# by default the score used is the one returned by score method of the estimator (accuracy)
scores = cross_val_score(clf, X, y, cv=cv)
print "Scores: ", (scores)
print ("Mean score: {0:.3f} (+/-{1:.3f})".format(np.mean(scores), sem(scores)))
# Confusion Matrix and Results
開發者ID:its-izhar,項目名稱:Emotion-Recognition-Using-SVMs,代碼行數:12,代碼來源:Train Classifier and Test Video Feed.py
示例10: compute_score
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def compute_score(clf, X, y,scoring='accuracy'):
xval = cross_val_score(clf, X, y, cv = 5,scoring=scoring)
return np.mean(xval)
示例11: accuracy
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def accuracy(features, labels):
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn import cross_validation
# We use logistic regression because it is very fast.
# Feel free to experiment with other classifiers
clf = Pipeline([('preproc', StandardScaler()),
('classifier', LogisticRegression())])
cv = cross_validation.LeaveOneOut(len(features))
scores = cross_validation.cross_val_score(
clf, features, labels, cv=cv)
return scores.mean()
開發者ID:PacktPublishing,項目名稱:Building-Machine-Learning-Systems-With-Python-Second-Edition,代碼行數:15,代碼來源:image-classification.py
示例12: stump
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def stump(X, y):
score = cross_val_score(LinearSVC(), X, y, cv = 5, n_jobs=5, scoring = 'average_precision')
clf = LinearSVC()
clf.fit(X, y)
coef = clf.coef_[0,0]
inter = clf.intercept_[0]
return np.mean(score), np.sign(coef), inter / np.abs(coef)
示例13: _f
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def _f(x):
# iris = load_iris()
X, y = X, y = make_hastie_10_2(random_state=0)
x = np.ravel(x)
f = np.zeros(x.shape)
for i in range(f.size):
clf = RandomForestClassifier(n_estimators=1, min_samples_leaf=int(np.round(x[i])), random_state=0)
# scores = cross_val_score(clf, iris.data, iris.target)
scores = cross_val_score(clf, X, y, cv=5)
f[i] = -scores.mean()
return f.ravel()
示例14: train
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def train(self):
feats = self.get_features()
scores = np.array(self.scores)
# Compute error metrics for the estimator.
self.cv_scores = cross_validation.cross_val_score(self.classifier, feats, scores)
self.cv_score = self.cv_scores.mean()
self.cv_dev = self.cv_scores.std()
self.classifier.fit(feats, scores)
self.fit_done = True
示例15: run_croos_validation
# 需要導入模塊: from sklearn import cross_validation [as 別名]
# 或者: from sklearn.cross_validation import cross_val_score [as 別名]
def run_croos_validation(self):
features,labels,cv = self.getFeaturesLabel()
scores = cross_validation.cross_val_score(self.clf, features, labels, cv=cv, scoring=mean_absolute_percentage_error_scoring, n_jobs = -1)
print "cross validation scores: means, {}, std, {}, details,{}".format(np.absolute(scores.mean()), scores.std(), np.absolute(scores))
return -np.absolute(scores.mean())