Hashingvectorizer 和多项式朴素贝叶斯不能一起工作
Hashingvectorizer and Multinomial naive bayes are not working together
我正在尝试用 python 2.7 中的 Scikit-learn
编写一个 Twitter 情绪分析程序。 OS 是 Linux Ubuntu 14.04。
在向量化步骤中,我想使用Hashingvectorizer()
。为了测试分类器的准确性,它与 LinearSVC
、NuSVC
、GaussianNB
、BernoulliNB
和 LogisticRegression
分类器一起工作正常,但对于 MultinomialNB
,它 returns 这个错误
Traceback (most recent call last):
File "/media/test.py", line 310, in <module>
classifier_rbf.fit(train_vectors, y_trainTweets)
File "/home/.local/lib/python2.7/site-packages/sklearn/naive_bayes.py", line 552, in fit
self._count(X, Y)
File "/home/.local/lib/python2.7/site-packages/sklearn/naive_bayes.py", line 655, in _count
raise ValueError("Input X must be non-negative")
ValueError: Input X must be non-negative
[Finished in 16.4s with exit code 1]
这是与此错误相关的块代码
vectorizer = HashingVectorizer()
train_vectors = vectorizer.fit_transform(x_trainTweets)
test_vectors = vectorizer.transform(x_testTweets)
classifier_rbf = MultinomialNB()
classifier_rbf.fit(train_vectors, y_trainTweets)
prediction_rbf = classifier_rbf.predict(test_vectors)
为什么会这样,我该如何解决?
初始化矢量化器时,您需要将 non_negative
参数设置为 True
vectorizer = HashingVectorizer(non_negative=True)
如果 non_negative
参数不可用(就像我的版本一样)
尝试放置:
vectorizer = HashingVectorizer(alternate_sign=False)
我正在尝试用 python 2.7 中的 Scikit-learn
编写一个 Twitter 情绪分析程序。 OS 是 Linux Ubuntu 14.04。
在向量化步骤中,我想使用Hashingvectorizer()
。为了测试分类器的准确性,它与 LinearSVC
、NuSVC
、GaussianNB
、BernoulliNB
和 LogisticRegression
分类器一起工作正常,但对于 MultinomialNB
,它 returns 这个错误
Traceback (most recent call last):
File "/media/test.py", line 310, in <module>
classifier_rbf.fit(train_vectors, y_trainTweets)
File "/home/.local/lib/python2.7/site-packages/sklearn/naive_bayes.py", line 552, in fit
self._count(X, Y)
File "/home/.local/lib/python2.7/site-packages/sklearn/naive_bayes.py", line 655, in _count
raise ValueError("Input X must be non-negative")
ValueError: Input X must be non-negative
[Finished in 16.4s with exit code 1]
这是与此错误相关的块代码
vectorizer = HashingVectorizer()
train_vectors = vectorizer.fit_transform(x_trainTweets)
test_vectors = vectorizer.transform(x_testTweets)
classifier_rbf = MultinomialNB()
classifier_rbf.fit(train_vectors, y_trainTweets)
prediction_rbf = classifier_rbf.predict(test_vectors)
为什么会这样,我该如何解决?
初始化矢量化器时,您需要将 non_negative
参数设置为 True
vectorizer = HashingVectorizer(non_negative=True)
如果 non_negative
参数不可用(就像我的版本一样)
尝试放置:
vectorizer = HashingVectorizer(alternate_sign=False)