Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. But when I try this I get a 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.

import sklearn
sklearn.__version__

'0.14-git'
like image 632
Solomon Avatar asked Oct 04 '22 12:10

Solomon


1 Answers

You should update the sci-kit learn to the latest version 0.16.

See this page for scoring paramters

Not all the sklearn.metrics work and the names are different. 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']
like image 100
Ty Shaikh Avatar answered Oct 10 '22 02:10

Ty Shaikh