python - 尝试预测新句子时的预期二维数组
问题描述
建立模型后,我现在想用新数据检查结果。我用过以下
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'
你能解释一下如何检查我的模型中的新句子吗?
解决方案
似乎您使用错误的方式来生成预测。通常,您需要'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
推荐阅读
- javascript - 如何将变量值放在进度条中?
- asp.net-mvc - 将 Quill 绑定到 asp net core 模型
- javascript - 使用 prependTo() 时缺少按钮之间的间距
- flutter - 从 DatePicker 中删除时间
- pdf - 如何让 VoiceOver 朗读 InDesign 创建的 PDF 中的段落而不在每一行暂停?
- php - 使用 SSL 时不再存储 PHP 会话
- python-3.x - 重新排序每个数据框行的值组?
- python - 如何在跨度标签 XPATH Python 之间获取文本
- android - 如何针对特定设备测试我的应用程序?
- javascript - Firebase 总是返回“由于异常活动,我们已阻止来自此设备的所有请求。请稍后再试。”