Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Undefined
#1
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
Reply
#2
(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?
Reply
#3
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.
buran likes this post
Reply
#4
I use my dog rather than a rubber duck, but I suppose that would do...
buran likes this post
Reply
#5
rubber duck debugging
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  undefined function error JonWayn 5 1,403 Sep-11-2022, 03:38 AM
Last Post: JonWayn
  ImportError: /home/pybind11_example.cpython-37m-x86_64-linux-gnu.so: undefined symbol chaitra 2 5,029 Feb-03-2021, 05:14 AM
Last Post: chaitra
  name "MyVariable" can be undefined Advisability 1 2,391 Aug-20-2020, 02:05 PM
Last Post: Gribouillis
  function error: undefined variable Atlantic_3000 3 6,866 Apr-12-2020, 02:53 AM
Last Post: bowlofred
  name undefined Coding Error Ewilliam51 2 2,100 Feb-06-2020, 12:19 AM
Last Post: Ewilliam51
  Timestamp is undefined ErnestTBass 7 7,840 Feb-16-2019, 08:27 PM
Last Post: snippsat
  python charmap codec can't decode byte X in position Y character maps to < undefined> owais 9 38,914 Apr-28-2018, 10:52 PM
Last Post: abadawi
  Python code with serial port and global undefined marciokoko 13 14,326 Jan-17-2017, 06:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020