error 'RandomForestClassifier' object has no attribute 'target_type_'

2.2k Views Asked by At

when I run this piece of code:

from yellowbrick.classifier import ROCAUC
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(**{"max_features": 0.4, "n_estimators":15,"min_samples_leaf": 0.1,"random_state":42})
rf.fit(X_train, y_train)
roc_viz = ROCAUC(rf)
roc_viz.score(X_test, y_test)

I have this error

'RandomForestClassifier' object has no attribute 'target_type_'

Somebody has an idea ? Thank you

And when I debug, at the instruction roc_viz = ROCAUC(rf)

I get the error:

unable to get repr for <class 'yellowbrick.classifier.rocauc.ROCAUC'

2

There are 2 best solutions below

2
larrywgray On

A couple of things

  1. You always need to fit the visualizer

  2. If you have already fitted your model then you must set the param is_fitted to True

    rf = RandomForestClassifier(**{"max_features": 0.4, "n_estimators":15,"min_samples_leaf": 0.1,"random_state":42})

    rf.fit(X_train, y_train)

    roc_viz = ROCAUC(rf, is_fitted=True)

    roc_viz.fit(X_train, y_train)

    roc_viz.score(X_test, y_test)

0
GG-Delta On

Your code within your original question above needs only one additional line and it will work smoothly. Please just add the following line at the correct position:

roc_viz.fit(X_train, y_train)

You can see this added line within your original code here below which will work in this way without error:

from yellowbrick.classifier import ROCAUC
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(**{"max_features": 0.4, "n_estimators":15,"min_samples_leaf": 0.1,"random_state":42})
rf.fit(X_train, y_train)
roc_viz = ROCAUC(rf) 
roc_viz.fit(X_train, y_train)
roc_viz.score(X_test, y_test)

Hope this helps!