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 :
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.