简体   繁体   English

'numpy.ndarray' 对象没有属性 'lower'

[英]'numpy.ndarray' object has no attribute 'lower'

I training model to classify articles by MultinomialNB我训练模型通过 MultinomialNB 对文章进行分类

this code这个代码

X_train , X_test , y_train ,y_test = train_test_split(X,Y,test_size=0.1, random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape


nb = Pipeline([('tfidf', TfidfVectorizer()),
               ('clf', MultinomialNB()),
              ])
nb.fit(X_train,y_train)

test_predict = nb.predict(X_test)

train_accuracy = round(nb.score(X_train,y_train)*100)
test_accuracy =round(accuracy_score(test_predict, y_test)*100)

the error:错误:

'numpy.ndarray' object has no attribute 'lower'

The solution is to make sure that the input data is in the correct format.解决方案是确保输入数据的格式正确。 The TfidfVectorizer expects the input data to be in the form of a string or a list of strings. TfidfVectorizer 期望输入数据采用字符串或字符串列表的形式。 Therefore, you need to convert the numpy array to a string before feeding it into the vectorizer.因此,您需要先将 numpy 数组转换为字符串,然后再将其送入矢量化器。 This can be done using the numpy.array.tostring() function.这可以使用 numpy.array.tostring() 函数来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM