web-dev-qa-db-fra.com

TypeError: impossible d'effectuer une réduction avec un type flexible

J'utilise la bibliothèque scikit-learn. J'essaie d'utiliser le module Gaussian Naive Bayes dans la bibliothèque scikit-learn mais je rencontre l'erreur suivante. TypeError: impossible d'effectuer une réduction avec un type flexible

Ci-dessous l'extrait de code.

training = GaussianNB()
training = training.fit(trainData, target)
prediction = training.predict(testData)

C'est la cible

['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']

C'est 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']]

Ci-dessous la trace de la pile

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
45
Arihant Jain

Il semble que votre 'trainData' soit une liste de chaînes:

['-214' '-153' '-58' ..., '36' '191' '-37']

Changez votre 'trainData' en un type numérique.

 import numpy as np
 np.array(['1','2','3']).astype(np.float)
102
JAB