无法在逻辑回归中使用decision_function()计算分数

2024-04-29 02:30:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做华盛顿大学的作业,我要用逻辑回归中的decision_function()来预测样本检验矩阵(最后几行)的分数。但我得到的错误是

    ValueError: X has 145 features per sample; expecting 113092

代码如下:

^{pr2}$

以下是产品数据:

          Name                                                         Review                                       Rating  

  0       Planetwise Flannel Wipes                              These flannel wipes are OK, but in my opinion ...       3  


  1       Planetwise Wipe Pouch                                 it came early and was not disappointed. i love...       5  


  2       Annas Dream Full Quilt with 2 Shams                   Very soft and comfortable and warmer than it l...       5  

  3       Stop Pacifier Sucking without tears with Thumb...     This is a product well worth the purchase.  I ...       5

  4       Stop Pacifier Sucking without tears with Thumb...      All of my kids have cried non-stop when I trie...       5 

Tags: andmywith作业itfunction逻辑without
1条回答
网友
1楼 · 发布于 2024-04-29 02:30:50

此行导致后续行出错:

test_matrix = vectorizer.fit_transform(test_data['review_clean'])

将上述内容改为:

^{pr2}$

解释:将使用转换函数对数据进行拟合。因此,所有关于训练数据的信息都会丢失,词汇表只能从测试数据中计算出来。在

{{cd2>你要用这个对象来转换。因此,其中的特性将仅是从test_data中学到的那些特性。在

但是sentiment_model是从train_data开始训练词汇的。因此,这些特征是不同的。在

总是在测试数据上使用transform(),而不是fit_transform()。在

相关问题 更多 >