Python Forum

Full Version: Undefined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I get the following error when I run some python code.

Error:
NameError Traceback (most recent call last) Input In [115], in <cell line: 2>() 1 # Plotting a Bar Graph to compare models ----> 2 plt.bar(X.columns, feature_importances_) 3 plt.xlabel( "Feature Labels") 4 plt.ylabel( "Feature Importance" ) NameError: name 'feature_importances_' is not defined
It says that feature_importances_ is not defined. In the code one can see that it is.

model = ExtraTreesClassifier()
model.fit(X, y)
print(model.feature_importances_)

ext=pd.DataFrame(model.feature_importances_,columns=["extratrees"])
ext
ext.sort_values(['extra-trees'], ascending=True)

# Plotting a Bar Graph to compare models
plt.bar(X.columns, feature_importances_)
plt.xlabel( "Feature Labels")
plt.ylabel( "Feature Importance" )
plt.title( "Comparison of Different Feature Importances" )
plt.shw()

What is wrong? The variable is defined.

Thanks in advance.

Respectfully,

LZ
(Aug-01-2022, 04:20 PM)Led_Zeppelin Wrote: [ -> ]It says that feature_importances_ is not defined. In the code one can see that it is.
Is it perhaps defined in an if or else block?
feature_importances_ is not defined, model.feature_importances_ is defined. feature_importances_ must be an attribute of whatever "model" is.

You need a rubber duck. After describing your problem to the rubber duck it would reply "How are those two different? Quack!" That is when you notice you forgot to reference model in the plt.bar(X.columns, feature_importances_) function call.
I use my dog rather than a rubber duck, but I suppose that would do...