首页 > 解决方案 > Python Naive Bayes 训练问题 - cannoy 使用灵活类型执行 reduce

问题描述

我的目标是训练一个贝叶斯模型来审查正面和负面的句子。我有 6000 个单词来训练模型,想用诸如“我感到很高兴”之类的句子来测试它。

我目前正在将 numpy 与 sklearn 一起使用,但遇到类型错误的问题:“无法使用灵活类型执行 reduce”。

我有一个单词列表 likewords = ["good", "happy", "bad", "sad"]和一个相应的列表wordTypes = ["positive", "positive", "negative", "negative"]。我试图简化它以得到一些工作,所以结果是下面的代码。我还使用了“str”而不是正负,X以及括号而不是方括号。

from sklearn.naive_bayes import GaussianNB
import numpy as np
nv = GaussianNB()

X = [["happy","positive"], ["good","positive"], ["bad","negative"], ["poor", "negative"]]
y = ["positive","positive", "negative","negative"]
     
nv.fit(X,y)

我也尝试过如下,但得到以下内容:ValueError: Expected 2D array, got 1D array instead: array=['happy' 'good' 'bad' 'poor']. 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.

X = ["happy", "good", "bad", "poor"]
y = ["positive","positive", "negative","negative"]         

我是否误解了它们的结构?

标签: pythonlistnumpysentiment-analysisnaivebayes

解决方案


推荐阅读