简体   繁体   English

使用cross_validation.cross_val_score和metrics.precision_recall_fscore_support

[英]Using cross_validation.cross_val_score with metrics.precision_recall_fscore_support

I'm new to scikits-learn and I'd like to use cross_validation.cross_val_score with metrics.precision_recall_fscore_support so that I can get all relevant cross-validation metrics without having to run my cross-validation once for accuracy, once for precision, once for recall, and once for f1. 我是scikits-learn的新手,我想将cross_validation.cross_val_scoremetrics.precision_recall_fscore_support结合使用,这样我就可以获取所有相关的交叉验证指标,而不必为准确性,一次精度和一次运行交叉验证召回,一次f1。 But when I try this I get a ValueError: 但是当我尝试这个时,我得到一个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)

Here's the error: 这是错误:

  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()

Note, you need the .14-git version to use the scoring parameter in cross_validation.cross_val_score . 请注意,您需要使用.14-git版本才能在cross_validation.cross_val_score使用评分参数。

import sklearn
sklearn.__version__

'0.14-git'

You should update the sci-kit learn to the latest version 0.16. 您应该将sci-kit learning更新到最新版本0.16。

See this page for scoring paramters 请参阅此页面以获取参数

Not all the sklearn.metrics work and the names are different. 并非所有的sklearn.metrics都起作用,并且名称不同。 The following parameters are accepted: 接受以下参数:

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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cross_validation.cross_val_score 返回什么样的分数? - What kind of scores are returned by cross_validation.cross_val_score? Scikit:使用cross_val_score函数计算精度和召回率 - Scikit: calculate precision and recall using cross_val_score function python 中 cross_val_score 的精确召回 auc - precision-recall auc from cross_val_score in python sklearn.metrics.precision_recall_fscore_support的输出解释 - Interpretation of the output of sklearn.metrics.precision_recall_fscore_support 使用交叉验证计算 4 个模型的召回率和精度以及 F 分数时出错? - Error when computing recall and precision and F-score of 4 models using cross validation? precision_recall_fscore_support返回相同值的精度调用 - precision_recall_fscore_support return the same value precision recall 与GridSearchCV一起使用时,fbeta_score和precision_recall_fscore_support之间的区别是什么? - Difference between fbeta_score and precision_recall_fscore_support when used with GridSearchCV? 如何使用交叉验证在多类数据集中对精度、召回率和 f1-score 进行评分? - how to score precision, recall and f1-score in a multi-class dataset using cross-validate? 如何计算 K 折交叉验证的不平衡数据集的精度、召回率和 f1 分数? - How to compute precision,recall and f1 score of an imbalanced dataset for K fold cross validation? 在sklearn.cross_validation.cross_val_score中使用python pandas时间戳 - Using python pandas timestamps in sklearn.cross_validation.cross_val_score
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM