Error in Python2

0

I'm practicing using Python 2, but I do not know the reason for this error. Below is my code with the error. And numpy and scipy are installed, because when I give the import no more error appears.

vaca1=    [1,1,0]    
vaca2=    [1,1,0]    
vaca3=    [1,1,0]    
cavalo4= [1,1,1]    
cavalo5= [0,1,1]    
cavalo6= [0,1,1]    
dados=[vaca1,vaca2,vaca3,cavalo4,cavalo5,cavalo6]    
marcacoes= [1, 1, 1, -1, -1, -1]    
misterioso= [1, 1, 1]

...

from sklearn.naive_bayes import MultinomialNB

modelo= MultinomialNB()
modelo.fit(dados, marcacoes)
MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True)
print(modelo.predict(misterioso))

Here is the error:

  

Traceback (most recent call last):         File "", line 1, in         File "C: \ tools \ Anaconda2 \ lib \ site-packages \ sklearn \ naive_bayes.py", line   66, in predict           jll = self._joint_log_likelihood (X)         File "C: \ tools \ Anaconda2 \ lib \ site-packages \ sklearn \ naive_bayes.py", line   724, in _joint_log_likelihood           X = check_array (X, accept_sparse = 'csr')         File "C: \ tools \ Anaconda2 \ lib \ site-packages \ sklearn \ utils \ validation.py",   line 410, in check_array           "if it contains a single sample." format (array))       ValueError: Expected 2D array, got 1D array instead:       array = [1 1 1].       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.

    
asked by anonymous 15.09.2017 / 04:43

1 answer

0

[TL; DR]

Try this:

from sklearn.naive_bayes import MultinomialNB
import numpy as np

vaca1=    [1,1,0]    
vaca2=    [1,1,0]    
vaca3=    [1,1,0]    
cavalo4= [1,1,1]    
cavalo5= [0,1,1]    
cavalo6= [0,1,1]   

dados = np.array([vaca1,vaca2,vaca3,cavalo4,cavalo5,cavalo6])
marcacoes= np.array([1, 1, 1, -1, -1, -1])
misterioso = np.array([[1, 1, 1]])

modelo= MultinomialNB()
modelo.fit(dados, marcacoes)
MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True)
print(modelo.predict(misterioso))
[-1]

See working on repl.it.

    
15.09.2017 / 13:34