Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix the problem model.fit(df[['size2']]) NameError: name 'df' is not defined

I’m trying to detect an animal but I have the following problem:
NameError: name ‘df’ is not defined

 import numpy as np
 import pandas as pd
 import seaborn as sns
 from sklearn.ensemble import IsolationForest


 data = pd.read_csv('P.csv',encoding='latin-1', on_bad_lines='skip',index_col=0, header=0, 
 engine='python')

 model = IsolationForest(n_estimators = 100, max_samples = 'auto', contamination = 'auto')
 model.fit(df[['P']])
 df['size2'] = model.decision_function(df[['P']])
 df['anomaly']= model.predict(df[['P']])
 df = func_nb()
 print(df.head(50))
 anomaly = df.loc[df['anomaly']==-1]
 df = func_nb()
 anomaly_index = list(anomaly.index)
 print(anomaly)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As the error suggests, it seems that you defined data when you actually wanted to define df. Therefore, changing this line from:

data = pd.read_csv('P.csv',encoding='latin-1', on_bad_lines='skip',index_col=0, header=0, 

To:

df = pd.read_csv('P.csv',encoding='latin-1', on_bad_lines='skip',index_col=0, header=0, 

Would probably solve your issue.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading