Error in Python2

Asked

Viewed 95 times

1

I’m practicing using Python 2, but I don’t know the reason for this error. Follow below 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 mistake:

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.

  • The error msg says mysterious q should be a 2D matrix, so it should be: misterioso=[[1],[1],[1]], making this correction this error will no longer occur, but it is likely that another error will occur. I haven’t used scikit for a long time, but there’s something wrong with your data model, the ideal.

1 answer

0


[TL;DR]

Try it like 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.

  • Thanks worked, will you explain so on top of why of my mistake.

  • Beemm "Above": The function expects a 2D (two-dimensional) array but vc was sending a one-dimensional, the error msg itself says so. In these cases the best you can do is try to adapt your context to an example of documentation, see this one.

  • Thanks, I was also looking for this and it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.