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

import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris


iris = load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

dt = DecisionTreeClassifier()

dt = dt.fit(X_train, y_train)

predicted = dt.predict(X_test, y_test)

What is the problem? The model is trained on the same data, but it doesn't predict anything. Error {[2] pops up]}

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

Saved the data in a DataFrame, but still an error.

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

Author: MaxU, 2020-02-08

1 answers

The dt.predict(X, check_input=True) method expects two arguments - a matrix of values, based on which predictions will be made, and a boolean flag check_input. You passed an array of correct values as the second parameter instead of a scalar value. In the code dt.predict there is a check if check_input: - on this line of code an exceptional situation will fall out, because the construction if np.array([1, 0, 0, 2]) - can not be uniquely checked.

There is no point in passing a previously known answer to dt.predict() - y_test.

The right thing to do so:

predicted = dt.predict(X_test)

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

In [5]: dt.score(X_test, y_test)
Out[5]: 0.9473684210526315
 1
Author: MaxU, 2020-02-08 12:05:59