本文整理汇总了Python中sklearn.model_selection.cross_val_score方法的典型用法代码示例。如果您正苦于以下问题:Python model_selection.cross_val_score方法的具体用法?Python model_selection.cross_val_score怎么用?Python model_selection.cross_val_score使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.model_selection
的用法示例。
在下文中一共展示了model_selection.cross_val_score方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search_cv
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def search_cv(x_train, y_train, x_test, y_test, model=GradientBoostingClassifier(n_estimators=30)):
# grid search找到最好的参数
parameters = {'kernel': ('linear', 'rbf'), 'C': [1, 2, 4], 'gamma': [0.125, 0.25, 0.5, 1, 2, 4]}
clf = GridSearchCV(model, param_grid=parameters)
grid_search = clf.fit(x_train, y_train)
# 对结果打分
print("Best score: %0.3f" % grid_search.best_score_)
print(grid_search.best_estimator_)
# best prarams
print('best prarams:', clf.best_params_)
print('-----grid search end------------')
print('on all train set')
scores = cross_val_score(grid_search.best_estimator_, x_train, y_train, cv=3, scoring='accuracy')
print(scores.mean(), scores)
print('on test set')
scores = cross_val_score(grid_search.best_estimator_, x_test, y_test, cv=3, scoring='accuracy')
print(scores.mean(), scores)
示例2: mmb_evaluate_model
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def mmb_evaluate_model(self):
"""
Returns scores from cross validation evaluation on the malicious / benign classifier
"""
predictive_features = self.features['predictive_features']
self.clf_X = self.modeldata[predictive_features].values
self.clf_y = np.array(self.modeldata['label'])
X_train, X_test, y_train, y_test = train_test_split(self.clf_X, self.clf_y, test_size=0.2, random_state=0)
lb = LabelBinarizer()
y_train = np.array([number[0] for number in lb.fit_transform(y_train)])
eval_cls = RandomForestClassifier(n_estimators=100, max_features=.2)
eval_cls.fit(X_train, y_train)
recall = cross_val_score(eval_cls, X_train, y_train, cv=5, scoring='recall')
precision = cross_val_score(eval_cls, X_train, y_train, cv=5, scoring='precision')
accuracy = cross_val_score(eval_cls, X_train, y_train, cv=5, scoring='accuracy')
f1_score = cross_val_score(eval_cls, X_train, y_train, cv=5, scoring='f1_macro')
return {'accuracy': accuracy, 'f1': f1_score, 'precision': precision, 'recall': recall}
示例3: test_build_meowa_factory
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def test_build_meowa_factory():
iris = datasets.load_iris()
X = iris.data
y = iris.target
from sklearn.preprocessing import MinMaxScaler
X = MinMaxScaler().fit_transform(X)
l = nfpc.FuzzyPatternClassifier(membership_factory=t_factory,
aggregation_factory=nfpc.MEOWAFactory())
from sklearn.model_selection import cross_val_score
scores = cross_val_score(l, X, y, cv=10)
mean = np.mean(scores)
assert 0.80 < mean
示例4: test_build_ps_owa_factory
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def test_build_ps_owa_factory():
iris = datasets.load_iris()
X = iris.data
y = iris.target
from sklearn.preprocessing import MinMaxScaler
X = MinMaxScaler().fit_transform(X)
l = nfpc.FuzzyPatternClassifier(
membership_factory=t_factory,
aggregation_factory=nfpc.GAOWAFactory(optimizer=nfpc.ps_owa_optimizer())
)
from sklearn.model_selection import cross_val_score
scores = cross_val_score(l, X, y, cv=10)
mean = np.mean(scores)
print("mean", mean)
assert 0.92 < mean
示例5: test_classifier_iris
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def test_classifier_iris():
iris = load_iris()
X = iris.data
y = iris.target
from sklearn.preprocessing import MinMaxScaler
X = MinMaxScaler().fit_transform(X)
l = fpcga.FuzzyPatternClassifierGA(iterations=100, random_state=1)
from sklearn.model_selection import cross_val_score
scores = cross_val_score(l, X, y, cv=10)
assert len(scores) == 10
assert np.mean(scores) > 0.6
mean = np.mean(scores)
print("mean", mean)
assert 0.92 == pytest.approx(mean, 0.01)
示例6: test_check_scoring_gridsearchcv
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def test_check_scoring_gridsearchcv():
# test that check_scoring works on GridSearchCV and pipeline.
# slightly redundant non-regression test.
grid = GridSearchCV(LinearSVC(), param_grid={'C': [.1, 1]})
scorer = check_scoring(grid, "f1")
assert isinstance(scorer, _PredictScorer)
pipe = make_pipeline(LinearSVC())
scorer = check_scoring(pipe, "f1")
assert isinstance(scorer, _PredictScorer)
# check that cross_val_score definitely calls the scorer
# and doesn't make any assumptions about the estimator apart from having a
# fit.
scores = cross_val_score(EstimatorWithFit(), [[1], [2], [3]], [1, 0, 1],
scoring=DummyScorer())
assert_array_equal(scores, 1)
示例7: test_cross_val_score_predict_groups
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def test_cross_val_score_predict_groups():
# Check if ValueError (when groups is None) propagates to cross_val_score
# and cross_val_predict
# And also check if groups is correctly passed to the cv object
X, y = make_classification(n_samples=20, n_classes=2, random_state=0)
clf = SVC(kernel="linear")
group_cvs = [LeaveOneGroupOut(), LeavePGroupsOut(2), GroupKFold(),
GroupShuffleSplit()]
for cv in group_cvs:
assert_raise_message(ValueError,
"The 'groups' parameter should not be None.",
cross_val_score, estimator=clf, X=X, y=y, cv=cv)
assert_raise_message(ValueError,
"The 'groups' parameter should not be None.",
cross_val_predict, estimator=clf, X=X, y=y, cv=cv)
示例8: test_cross_val_score_pandas
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def test_cross_val_score_pandas():
# check cross_val_score doesn't destroy pandas dataframe
types = [(MockDataFrame, MockDataFrame)]
try:
from pandas import Series, DataFrame
types.append((Series, DataFrame))
except ImportError:
pass
for TargetType, InputFeatureType in types:
# X dataframe, y series
# 3 fold cross val is used so we need atleast 3 samples per class
X_df, y_ser = InputFeatureType(X), TargetType(y2)
check_df = lambda x: isinstance(x, InputFeatureType)
check_series = lambda x: isinstance(x, TargetType)
clf = CheckingClassifier(check_X=check_df, check_y=check_series)
cross_val_score(clf, X_df, y_ser)
示例9: test_cross_val_score_precomputed
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection 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 = cross_val_score(svm, linear_kernel, y)
svm = SVC(kernel="linear")
score_linear = cross_val_score(svm, X, y)
assert_array_almost_equal(score_precomputed, score_linear)
# test with callable
svm = SVC(gamma='scale', kernel=lambda x, y: np.dot(x, y.T))
score_callable = cross_val_score(svm, X, y)
assert_array_almost_equal(score_precomputed, score_callable)
# Error raised for non-square X
svm = SVC(kernel="precomputed")
assert_raises(ValueError, cross_val_score, svm, X, y)
# test error is raised when the precomputed kernel is not array-like
# or sparse
assert_raises(ValueError, cross_val_score, svm,
linear_kernel.tolist(), y)
示例10: test_cross_val_score_with_score_func_classification
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection 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 = 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 = 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 = 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)
示例11: test_score_memmap
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def test_score_memmap():
# Ensure a scalar score of memmap type is accepted
iris = load_iris()
X, y = iris.data, iris.target
clf = MockClassifier()
tf = tempfile.NamedTemporaryFile(mode='wb', delete=False)
tf.write(b'Hello world!!!!!')
tf.close()
scores = np.memmap(tf.name, dtype=np.float64)
score = np.memmap(tf.name, shape=(), mode='r', dtype=np.float64)
try:
cross_val_score(clf, X, y, scoring=lambda est, X, y: score)
# non-scalar should still fail
assert_raises(ValueError, cross_val_score, clf, X, y,
scoring=lambda est, X, y: scores)
finally:
# Best effort to release the mmap file handles before deleting the
# backing file under Windows
scores, score = None, None
for _ in range(3):
try:
os.unlink(tf.name)
break
except WindowsError:
sleep(1.)
示例12: getFitness
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def getFitness(individual, X, y):
"""
Feature subset fitness function
"""
if(individual.count(0) != len(individual)):
# get index with value 0
cols = [index for index in range(
len(individual)) if individual[index] == 0]
# get features subset
X_parsed = X.drop(X.columns[cols], axis=1)
X_subset = pd.get_dummies(X_parsed)
# apply classification algorithm
clf = LogisticRegression()
return (avg(cross_val_score(clf, X_subset, y, cv=5)),)
else:
return(0,)
示例13: get_chromosome_score
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def get_chromosome_score(self, X_chromosome):
"""
Computes fitness using the subset of data in X_chromosome.
:param X_chromosome: subset of full data set, containing only a selection of the features.
:return: mean R2 or keras history last column entry.
"""
np.random.seed(self.random_state)
# Use either cross validation
if self.scoring == 'cv':
scores = cross_val_score(self.clf, X_chromosome, np.array(self.y), cv=self.n_cv)
return np.mean(scores)
# Or keras history in the case of neural networks (based on keras/tensorflow)
else:
try:
history = self.clf.fit(X_chromosome, np.array(self.y))
return history.history[self.scoring][-1]
except:
raise ValueError('Use either "cv" or keras history metrics.')
示例14: example_of_cross_validation_using_model_selection
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def example_of_cross_validation_using_model_selection(raw_data, labels, num_subjects, num_epochs_per_subj):
# NOTE: this method does not work for sklearn.svm.SVC with precomputed kernel
# when the kernel matrix is computed in portions; also, this method only works
# for self-correlation, i.e. correlation between the same data matrix.
# no shrinking, set C=1
svm_clf = svm.SVC(kernel='precomputed', shrinking=False, C=1, gamma='auto')
#logit_clf = LogisticRegression()
clf = Classifier(svm_clf, epochs_per_subj=num_epochs_per_subj)
# doing leave-one-subject-out cross validation
# no shuffling in cv
skf = model_selection.StratifiedKFold(n_splits=num_subjects,
shuffle=False)
scores = model_selection.cross_val_score(clf, list(zip(raw_data, raw_data)),
y=labels,
cv=skf)
print(scores)
logger.info(
'the overall cross validation accuracy is %.2f' %
np.mean(scores)
)
示例15: _sfn
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import cross_val_score [as 别名]
def _sfn(data, mask, myrad, bcast_var):
"""Score classifier on searchlight data using cross-validation.
The classifier is in `bcast_var[2]`. The labels are in `bast_var[0]`. The
number of cross-validation folds is in `bast_var[1].
"""
clf = bcast_var[2]
masked_data = data[0][mask, :].T
# print(l[0].shape, mask.shape, data.shape)
skf = model_selection.StratifiedKFold(n_splits=bcast_var[1],
shuffle=False)
accuracy = np.mean(model_selection.cross_val_score(clf, masked_data,
y=bcast_var[0],
cv=skf,
n_jobs=1))
return accuracy