我尝试在scikit-learn中为这个示例探索不同的分类器网站http://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html。但是,下面的代码产生了一个错误:ValueError:用序列设置数组元素。
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矩阵作为输入,您可以跟踪该问题的进展。
你现在可以做的是将X_train_tfidf
转换为numpy矩阵,然后再将其馈送给clf.fit()
:
clf.fit(X_train_tfidf.toarray(), label)