python 使用序列设置数组元素时出错

python error setting an array element with a sequence

我试图在 scikit-learn 中为这个例子探索不同的分类器 网站 http://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html。但是,下面的代码产生了一个错误:ValueError: setting an array element with a sequence.

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
import tensorflow.contrib.learn as skflow

data = ["I so handsome. I just broke the mirror!","I am a normal guy."]
label = np.array([0,1])

#CountVectoriser
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(data)

#TfidfTransformer
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)

#Classifier
clf = skflow.TensorFlowLinearClassifier(n_classes=2)
clf.fit(X_train_tfidf, label)

TensorFlowLinearClassifier不处理CSR矩阵作为输入,你可以关注that issue中的进度。


你现在可以做的是在将 X_train_tfidf 提供给 clf.fit() 之前将其转换为 numpy 矩阵:

clf.fit(X_train_tfidf.toarray(), label)