首页 > 解决方案 > 尝试预测新句子时的预期二维数组

问题描述

建立模型后,我现在想用新数据检查结果。我用过以下

count_vectorizer = CountVectorizer()

X_train= np.asarray(X_train)
y_train= np.asarray(y_train)
X_test = np.asarray(X_test)

score_log = clf.fit(X_train, y_train).predict(['Hello World'])

使用逻辑回归模型。

不幸的是我有

ValueError: Expected 2D array, got 1D array instead:
array=['Hello World'].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

当我尝试使用

top=['Hello World'].reshape(-1,1)

['Hello World'].to_numpy().reshape(-1,1)

我收到了这个新错误:

AttributeError: 'list' object has no attribute 'reshape'

你能解释一下如何检查我的模型中的新句子吗?

标签: pythonnumpyscikit-learnlogistic-regression

解决方案


似乎您使用错误的方式来生成预测。通常,您需要'Hello World'使用特定的转换器嵌入到 2D 向量空间。示例如下:

# transform text to vector
corpus = ['Hello World']
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
word_vectors = X.toarray()

# Apply your prediction model with word_vectors below
## CODE

有关 CountVectorizer 的更多信息,您可以阅读https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html

有关整个管道的更多信息,您可以阅读https://www.kaggle.com/catris25/logistic-regression-with-countvectorizer


推荐阅读