gushitong
gushitong

Reputation: 2006

AttributeError: 'MultiOutputClassifier' object has no attribute 'classes_'

I want to get prediction probabilities for each class of each output. But the classes_ attribute does not exist on the MultiOutputClassifier.

How can I relate the classes to the output ?

from sklearn.datasets import make_classification
from sklearn.multioutput import MultiOutputClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.utils import shuffle
import numpy as np

X, y1 = make_classification(n_samples=16, n_features=8, n_informative=4, n_classes=4, random_state=1)
y2 = shuffle(y1, random_state=1)
Y = np.vstack((y1, y2)).T

forest = RandomForestClassifier(n_estimators=16, random_state=1)
multi_target_forest = MultiOutputClassifier(forest, n_jobs=-1)
multi_target_forest.fit(X, Y).predict(X)

multi_target_forest.predict_proba(X)
multi_target_forest.classes_

AttributeError: 'MultiOutputClassifier' object has no attribute 'classes_'

Upvotes: 1

Views: 1824

Answers (1)

Venkatachalam
Venkatachalam

Reputation: 16966

Use estimators_ attribute of MultiOutputClassifier to access the estimator attributes.

Try this!

['y_{} class_{}'.format(idx,klass) for idx,forest in enumerate(multi_target_forest.estimators_) \
                                    for klass in forest.classes_]


#output:
['y_0 class_0',
 'y_0 class_1',
 'y_0 class_2',
 'y_0 class_3',
 'y_1 class_0',
 'y_1 class_1',
 'y_1 class_2',
 'y_1 class_3']

Upvotes: 1

Related Questions