【问题标题】:Expected 2D array when try to predict new sentences尝试预测新句子时的预期二维数组
【发布时间】:2021-03-16 02:02:25
【问题描述】:

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

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'

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

【问题讨论】:

    标签: python numpy scikit-learn logistic-regression


    【解决方案1】:

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

    # 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

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 2019-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 2020-10-13
      相关资源
      最近更新 更多