PREDICT error in Scikit-Earn

Asked

Viewed 188 times

0

I recently started to learn a little about Machine Learning and grading, through a course at Alura. Well, I tried to perform the first exercise, but I couldn’t due to an error that I don’t know what it is. Below is the code I have and the error presented.

Note: I tried to run the code again and now does not recognize the SKLEARN package I tried to import into the python file.

from sklearn.naive_bayes import MultinomialNB

pig1 = [1, 1, 0]
pig2 = [1, 1, 0]
pig3 = [1, 1, 0]
dog1 = [1, 1, 1]
dog2 = [0, 1, 1]
dog3 = [1, 0, 1]

dados = [pig1, pig2, pig3, dog1, dog2, dog3]

marcacoes = [1, 1, 1, -1, -1, -1]

misterioso = [1, 1, 1] 

modelo = MultinomialNB()

modelo.fit(dados, marcacoes)
print (modelo.predict(misterioso))

And here’s what the terminal returned:

Traceback (most recent call last):
  File "classificacao.py", line 19, in <module>
    print (modelo.predict(misterioso))
  File "/Users/josecarlosferreira/machinelearning/lib/python3.6/site-packages/sklearn/naive_bayes.py", line 66, in predict
    jll = self._joint_log_likelihood(X)
  File "/Users/josecarlosferreira/machinelearning/lib/python3.6/site-packages/sklearn/naive_bayes.py", line 724, in _joint_log_likelihood
    X = check_array(X, accept_sparse='csr')
  File "/Users/josecarlosferreira/machinelearning/lib/python3.6/site-packages/sklearn/utils/validation.py", line 441, 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.

I have been almost 4 days trying to solve this problem and nothing can. If anyone can help me to continue with my studies.

  • 2

    the mysterious list needs to be 2d, try to do mysterious = [[1, 1, 1]]

  • Carefully read the errors, in your case the answer is in error: "... print (model.Predict(mysterious)) ... Expected 2D array, got 1D array Instead" .

1 answer

0


As described by the comments, just use a two-dimensional array:

from sklearn.naive_bayes import MultinomialNB

pig1 = [1, 1, 0]
pig2 = [1, 1, 0]
pig3 = [1, 1, 0]
dog1 = [1, 1, 1]
dog2 = [0, 1, 1]
dog3 = [1, 0, 1]

dados = [pig1, pig2, pig3, dog1, dog2, dog3]

marcacoes = [1, 1, 1, -1, -1, -1]

misterioso = [
    [1, 1, 1]
]

modelo = MultinomialNB()

modelo.fit(dados, marcacoes)
print (modelo.predict(misterioso))
  • Excellent response. + 1

Browser other questions tagged

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