我一直在使用scikit-learn库。我正在尝试在scikit-learn库下使用高斯朴素贝叶斯模块,但我遇到了以下错误。 TypeError:无法使用灵活类型执行reduce
以下是代码段。
training = GaussianNB()
training = training.fit(trainData, target)
prediction = training.predict(testData)
这是目标
['ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'ALL', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML', 'AML']
这是trainData
[['-214' '-153' '-58' ..., '36' '191' '-37']
['-139' '-73' '-1' ..., '11' '76' '-14']
['-76' '-49' '-307' ..., '41' '228' '-41']
...,
['-32' '-49' '49' ..., '-26' '133' '-32']
['-124' '-79' '-37' ..., '39' '298' '-3']
['-135' '-186' '-70' ..., '-12' '790' '-10']]
下面是堆栈跟踪
Traceback (most recent call last):
File "prediction.py", line 90, in <module>
gaussianNaiveBayes()
File "prediction.py", line 76, in gaussianNaiveBayes
training = training.fit(trainData, target)
File "/Library/Python/2.7/site-packages/sklearn/naive_bayes.py", line 163, in fit
self.theta_[i, :] = np.mean(Xi, axis=0)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/ core/fromnumeric.py", line 2716, in mean
out=out, keepdims=keepdims)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/_methods.py", line 62, in _mean
ret = um.add.reduce(arr, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
TypeError: cannot perform reduce with flexible type
答案 0 :(得分:96)
看起来像你的火车数据&#39;是一个字符串列表:
['-214' '-153' '-58' ..., '36' '191' '-37']
更改您的#train;&#39;到数字类型。
import numpy as np
np.array(['1','2','3']).astype(np.float)
答案 1 :(得分:3)
当您尝试对字符串类型的值应用 prod 时,例如:
['-214' '-153' '-58' ..., '36' '191' '-37']
你会得到错误。
解决办法:
仅附加整数值,如 [1,2,3]
,您将获得预期的输出。
如果值在追加之前是字符串格式,则在数组中您可以将类型转换为 int
类型并将其存储在 list
中。