Python sklearn OneVsRestClassifier:Score函数给出了ValueError

时间:2015-12-15 21:46:58

标签: python machine-learning scikit-learn logistic-regression multilabel-classification

我正在研究多标签分类问题

import pandas as pd
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import SGDClassifier 
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import train_test_split

tdf = pd.read_csv("data.csv", index_col="DocID",error_bad_lines=False)[:8]

print tdf

给了我

DocID   Content             Tags           
1       some text here...   [70]
2       some text here...   [59]
3       some text here...  [183]
4       some text here...  [173]
5       some text here...   [71]
6       some text here...   [98]
7       some text here...  [211]
8       some text here...  [188]

然后根据需要识别和转换列

X=tdf["Content"]
y=tdf["Tags"]

t=TfidfVectorizer()
print t.fit_transform(X).toarray()
print MultiLabelBinarizer().fit_transform(y)

给了我

[[ 0.          0.01058315  0.         ...,  0.00529157  0.          0.        ]
 [ 0.          0.00947091  0.         ...,  0.00473545  0.          0.        ]
 [ 0.01190602  0.00950931  0.         ...,  0.00475465  0.          0.        ]
 ..., 
 [ 0.          0.01314373  0.         ...,  0.00657187  0.          0.        ]
 [ 0.          0.01200425  0.37574455 ...,  0.00600212  0.01502978  0.        ]
 [ 0.          0.02206688  0.         ...,  0.01103344  0.          0.        ]]

 [[1 0 0 0 0 1 0 0 1 1]
 [0 0 0 0 1 0 0 1 1 1]
 [0 1 0 1 0 0 1 0 1 1]
 [0 1 0 1 0 1 0 0 1 1]
 [0 1 0 0 0 1 0 0 1 1]
 [0 0 0 0 0 0 1 1 1 1]
 [0 1 1 0 0 0 0 0 1 1]
 [0 1 0 0 0 0 1 0 1 1]]

查看我的数据,y不应该只有 8列吗? 为什么有10列?

然后我分裂,变换,适合和得分

Xtrain, Xvalidate, ytrain, yvalidate = train_test_split(X, y, test_size=.5)

Xtrain=t.fit_transform(Xtrain).toarray()
Xvalidate=t.fit_transform(Xvalidate).toarray()

ytrain=MultiLabelBinarizer().fit_transform(ytrain)
yvalidate=MultiLabelBinarizer().fit_transform(yvalidate)

clf = OneVsRestClassifier(LogisticRegression(penalty='l2', C=0.01)).fit(Xtrain, ytrain)

print "One vs rest accuracy: %.3f"  % clf.score(Xvalidate,yvalidate)

但我收到了错误

print "One vs rest accuracy: %.3f"  % clf.score(Xvalidate,yvalidate)
  File "X:\Anaconda2\lib\site-packages\sklearn\base.py", line 310, in score
    return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
  File "X:\Anaconda2\lib\site-packages\sklearn\multiclass.py", line 325, in predict
    indices.extend(np.where(_predict_binary(e, X) > thresh)[0])
  File "X:\Anaconda2\lib\site-packages\sklearn\multiclass.py", line 83, in _predict_binary
    score = np.ravel(estimator.decision_function(X))
  File "X:\Anaconda2\lib\site-packages\sklearn\linear_model\base.py", line 249, in decision_function
    % (X.shape[1], n_features))
ValueError: X has 1546 features per sample; expecting 1354

这个错误是什么意思?它可能是数据吗?我使用完全相同的算法与相似(相同的列格式和数据格式)数据,并没有问题。另外,为什么fit函数有效?

我在这里做错了什么?

修改

所以在我的Tags列中,数据被读取为字符串。因此y中有两个额外的列。我试过了

X=tdf["Content"]
y=tdf["Tags"]
y = [map(int, list(_y.replace(',','').replace('[','').replace(']',''))) for _y in y]

以适应多个值,但我仍然是同样的错误。至少我得到y的正确列数。

1 个答案:

答案 0 :(得分:1)

当您致电fit_transform()时,您首先将特征提取器调整为数据(拟合部分),然后转换数据(变换部分)。通过在同一个特征提取器(使用不同的数据)上多次调用fit_transform(),您可以执行不同的拟合,例如您的TFIDF Vectorizer可能会为您的训练集学习一个词汇表,并为验证集学习一个完全不同的词汇表,从而产生不同数量的列(不同数量的唯一单词)。您必须先fit_transform() Xy 首先,然后分成训练和验证集 (一个适合,一个变换) )。或者,您可以调用fit_transform()来生成训练集,然后只调用transform()来生成验证集(一个拟合,多个变换)。