0
$\begingroup$

enter image description here

I got ValueError: Found array with dim 3. None expected <= 2. I dont know which array has dim 3?

DecisionTreeClassifier cannot take one-hot encoded classes?

But from this page it should support? https://scikit-learn.org/stable/modules/multiclass.html

label is constructed by

from sklearn.preprocessing import OneHotEncoder
onehot_encoder = OneHotEncoder(sparse=False)
label = onehot_encoder.fit_transform(y.values.reshape(-1,1))

where y is like enter image description here

$\endgroup$
3
  • $\begingroup$ label_train should have dimensions of (1490,1) where each value is the class label. The DecisionTreeClassifier has the capability to return probabilities for each class or predicted multiclass labels it finds in the training labels. $\endgroup$
    – m13op22
    Jan 18, 2023 at 22:29
  • $\begingroup$ @m13op22 thank you! but this is not mentioned in the sklearn docs? $\endgroup$
    – user900476
    Jan 18, 2023 at 22:36
  • 1
    $\begingroup$ You're right, it's not super clear in the fit() method docs. It's mentioned here and in the Iris example (with 3 classes). If you copy & paste the Iris code you can see the outputs and how the data should be structured. $\endgroup$
    – m13op22
    Jan 18, 2023 at 22:40

1 Answer 1

2
$\begingroup$

You need to integer encode your labels instead of one-hot encoding them.

[1, 0, 0] -> 0

[0, 1, 0] -> 1

[0, 0, 1] -> 2

so that the labels for multiclass classification (with K classes) that you provide to sklearn are just integers in the set $\{0,1,...,K-1 \}$

$\endgroup$
4
  • $\begingroup$ thank you! but this is not mentioned in the sklearn docs? $\endgroup$
    – user900476
    Jan 18, 2023 at 22:35
  • $\begingroup$ The sklearn docs for most classifiers look like this: y {array-like, sparse matrix} : of shape (n_samples,) or (n_samples, n_outputs) Target values. Note that the (n_samples,) shape applies to multi-class classification tasks. n_outputs is for something completely different: multi-label tasks. scikit-learn.org/stable/modules/generated/… $\endgroup$ Jan 18, 2023 at 23:17
  • $\begingroup$ n_outputs means it can accept one-hot encoding labels? $\endgroup$
    – user900476
    Jan 18, 2023 at 23:18
  • $\begingroup$ No n_outputs is not for standard multi-class classification tasks. It is for multi-label prediction tasks: scikit-learn.org/stable/modules/generated/… $\endgroup$ Jan 18, 2023 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.