我是 scikits-learn 的新手,我想使用 cross_validation.cross_val_score
与 metrics.precision_recall_fscore_support
这样我就可以获得所有相关的交叉验证指标,而不必为准确性运行一次交叉验证,一次为精确度,一次为召回率,一次为 f1。但是当我尝试这个时,我得到了一个 ValueError:
from sklearn.datasets import fetch_20newsgroups
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import metrics
from sklearn import cross_validation
import numpy as np
data_train = fetch_20newsgroups(subset='train', #categories=categories,
shuffle=True, random_state=42)
clf = LinearSVC(loss='l1', penalty='l2')
vectorizer = TfidfVectorizer(
sublinear_tf=False,
max_df=0.5,
min_df=2,
ngram_range = (1,1),
use_idf=False,
stop_words='english')
X_train = vectorizer.fit_transform(data_train.data)
# Cross-validate:
scores = cross_validation.cross_val_score(
clf, X_train, data_train.target, cv=5,
scoring=metrics.precision_recall_fscore_support)
这是错误:
File "<stdin>", line 3, in <module>
File "sklearn/cross_validation.py", line 1148, in cross_val_score
for train, test in cv)
File "sklearn/externals/joblib/parallel.py", line 514, in __call__
self.dispatch(function, args, kwargs)
File "sklearn/externals/joblib/parallel.py", line 311, in dispatch
job = ImmediateApply(func, args, kwargs)
File "sklearn/externals/joblib/parallel.py", line 135, in __init__
self.results = func(*args, **kwargs)
File "sklearn/cross_validation.py", line 1075, in _cross_val_score
score = scorer(estimator, X_test, y_test)
File "sklearn/metrics/metrics.py", line 1261, in precision_recall_fscore_support
print beta
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
请注意,您需要 .14-git 版本才能使用
cross_validation.cross_val_score
中的评分参数.import sklearn
sklearn.__version__
'0.14-git'
最佳答案
您应该将 sci-kit learn 更新到最新版本 0.16。
See this page for scoring paramters
并非所有 sklearn.metrics 都有效,名称也不同。接受以下参数:
ValueError: 'wrong_choice' is not a valid scoring value. Valid options are
['accuracy', 'adjusted_rand_score', 'average_precision', 'f1', 'f1_macro',
'f1_micro', 'f1_samples', 'f1_weighted', 'log_loss', 'mean_absolute_error',
'mean_squared_error', 'median_absolute_error', 'precision',
'precision_macro', 'precision_micro', 'precision_samples',
'precision_weighted', 'r2', 'recall', 'recall_macro', 'recall_micro',
'recall_samples', 'recall_weighted', 'roc_auc']
关于python - 使用 cross_validation.cross_val_score 和 metrics.precision_recall_fscore_support,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15796627/