我正在学习一个教程,它有以下部分:
>>> import numpy as np
>>> import pandas as pd
>>> from sklearn.feature_extraction.text import TfidfVectorizer
>>> from sklearn.linear_model.logistic import LogisticRegression
>>> from sklearn.cross_validation import train_test_split, cross_val_score
>>> df = pd.read_csv('data/sms.csv')
>>> X_train_raw, X_test_raw, y_train, y_test = train_test_split(df['message'], df['label'])
>>> vectorizer = TfidfVectorizer()
>>> X_train = vectorizer.fit_transform(X_train_raw)
>>> X_test = vectorizer.transform(X_test_raw)
>>> classifier = LogisticRegression()
>>> classifier.fit(X_train, y_train)
>>> precisions = cross_val_score(classifier, X_train, y_train, cv=5, scoring='precision')
>>> print 'Precision', np.mean(precisions), precisions
>>> recalls = cross_val_score(classifier, X_train, y_train, cv=5, scoring='recall')
>>> print 'Recalls', np.mean(recalls), recalls
然后我复制了一些修改:
ddir = (sys.argv[1])
df = pd.read_csv(ddir + '/SMSSpamCollection', sep='t', quoting=csv.QUOTE_NONE, names=["label", "message"])
X_train_raw, X_test_raw, y_train, y_test = train_test_split(df['label'], df['message'])
vectorizer = TfidfVectorizer()
X_train = vectorizer.fit_transform(X_train_raw)
X_test = vectorizer.transform(X_test_raw)
classifier = LogisticRegression()
classifier.fit(X_train, y_train)
precisions = cross_val_score(classifier, X_train, y_train, cv=5, scoring='precision')
recalls = cross_val_score(classifier, X_train, y_train, cv=5, scoring='recall')
print 'Precision', np.mean(precisions), precisions
print 'Recalls', np.mean(recalls), recalls
然而,尽管代码几乎没有差异,但书中的结果比我的要好得多:
的书:Precision 0.992137651822 [ 0.98717949 0.98666667 1. 0.98684211 1. ]
Recall 0.677114261885 [ 0.7 0.67272727 0.6 0.68807339 0.72477064]
我的:Precision 0.108435683974 [ 2.33542342e-06 1.22271611e-03 1.68918919e-02 1.97530864e-01 3.26530612e-01]
Recalls 0.235220281632 [ 0.00152053 0.03370787 0.125 0.44444444 0.57142857]
回到脚本看看哪里出了问题,我认为第18行:
X_train_raw, X_test_raw, y_train, y_test = train_test_split(df['label'], df['message'])
是罪魁祸首,并将(df['label'], df['message'])
更改为(df['message'], df['label'])
。但是这给了我一个错误:
Traceback (most recent call last):
File "Chapter4[B-FLGTLG]C[Y-BCPM][G-PAR--[00].py", line 30, in <module>
precisions = cross_val_score(classifier, X_train, y_train, cv=5, scoring='precision')
File "/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py", line 1433, in cross_val_score
for train, test in cv)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.py", line 800, in __call__
while self.dispatch_one_batch(iterator):
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.py", line 658, in dispatch_one_batch
self._dispatch(tasks)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.py", line 566, in _dispatch
job = ImmediateComputeBatch(batch)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.py", line 180, in __init__
self.results = batch()
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.py", line 72, in __call__
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py", line 1550, in _fit_and_score
test_score = _score(estimator, X_test, y_test, scorer)
File "/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py", line 1606, in _score
score = scorer(estimator, X_test, y_test)
File "/usr/local/lib/python2.7/dist-packages/sklearn/metrics/scorer.py", line 90, in __call__
**self._kwargs)
File "/usr/local/lib/python2.7/dist-packages/sklearn/metrics/classification.py", line 1203, in precision_score
sample_weight=sample_weight)
File "/usr/local/lib/python2.7/dist-packages/sklearn/metrics/classification.py", line 984, in precision_recall_fscore_support
(pos_label, present_labels))
ValueError: pos_label=1 is not a valid label: array(['ham', 'spam'],
dtype='|S4')
这里可能有什么问题?数据在这里:http://archive.ics.uci.edu/ml/datasets/SMS+Spam+Collection如果有人想尝试。
堆栈跟踪末尾的错误是理解这里发生的事情的关键。
ValueError: pos_label=1不是一个有效的标签:array(['ham', 'spam'],dtype = | S4)
你试图用精确度和召回率来给你的模型打分。回想一下,这些评分方法是根据真阳性、假阳性和假阴性来制定的。但是sklearn
怎么知道什么是正的,什么是负的呢?是"火腿"还是"垃圾邮件"?我们需要一种方式告诉sklearn
,我们认为"垃圾邮件"是正面标签,"火腿"是负面标签。根据sklearn
文档,精度和召回评分器默认期望1
的正标签,因此错误消息的pos_label=1
部分。
至少有3种方法可以解决这个问题。
1。直接从数据源将'ham'和'spam'值编码为0和1,以适应精度/召回评分者:
# Map dataframe to encode values and put values into a numpy array
encoded_labels = df['label'].map(lambda x: 1 if x == 'spam' else 0).values # ham will be 0 and spam will be 1
# Continue as normal
X_train_raw, X_test_raw, y_train, y_test = train_test_split(df['message'], encoded_labels)
2。使用sklearn
的内置函数(label_binarize
)将分类数据转换为编码的整数,以适应精度/召回分数:
这将把分类数据转换为整数。
# Encode labels
from sklearn.preprocessing import label_binarize
encoded_column_vector = label_binarize(df['label'], classes=['ham','spam']) # ham will be 0 and spam will be 1
encoded_labels = np.ravel(encoded_column_vector) # Reshape array
# Continue as normal
X_train_raw, X_test_raw, y_train, y_test = train_test_split(df['message'], encoded_labels)
3。为pos_label
创建带有自定义参数的得分对象:
正如文档所说,精度和召回分数默认情况下有一个pos_label
参数1
,但这可以更改为通知评分者哪个字符串表示正标签。您可以使用make_scorer
构造具有不同参数的计分器对象。
# Start out as you did originally with string labels
X_train_raw, X_test_raw, y_train, y_test = train_test_split(df['message'], df['label'])
# Fit classifier as normal ...
# Get precision and recall
from sklearn.metrics import precision_score, recall_score, make_scorer
# Precision
precision_scorer = make_scorer(precision_score, pos_label='spam')
precisions = cross_val_score(classifier, X_train, y_train, cv=5, scoring=precision_scorer)
print 'Precision', np.mean(precisions), precisions
# Recall
recall_scorer = make_scorer(recall_score, pos_label='spam')
recalls = cross_val_score(classifier, X_train, y_train, cv=5, scoring=recall_scorer)
print 'Recalls', np.mean(recalls), recalls
在对代码进行任何这些更改之后,我得到了0.990
和0.704
左右的平均精度和召回分数,与书中的数字一致。
在所有三个选项中,我最推荐#3,因为它更不容易出错。