Home python ValueError: The truth value of an array with more than one element...

ValueError: The truth value of an array with more than one element is ambiguous.Use a.any () or a.all ()

Author

Date

Category

What’s the problem? The model is trained on the same data, but predicts nothing. An error appears

ValueError: The truth value of an array with more than one element is ambiguous.Use a.any () or a.all ()

Was saving data to DataFrame, but still error.

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool (), a.item (), a.any () or a.all ().


Answer 1, authority 100%

The dt.predict(X, check_input=True)method expects two arguments – a matrix of values ​​from which predictions will be made and a check_inputboolean flag. You passed an array of valid values ​​as the second parameter instead of a scalar value. In the code dt.predictthere is a check if check_input:– an exception will be thrown on this line of code, because the construction if np.array([1, 0, 0, 2])– cannot be unambiguously checked.

It makes no sense to pass the well-known answer to dt.predict()y_test.

The correct way to do this:

predicted = dt.predict(X_test)

If you want to check the accuracy of predictions, you can use the .score():

In [5]: dt.score(X_test, y_test)
Out[5]: 0.9473684210526315

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions