Skip to content

Instantly share code, notes, and snippets.

@gautamdudeja90
Created July 9, 2020 07:49
Show Gist options
  • Save gautamdudeja90/990d0a675557a2484b4b0d365a260491 to your computer and use it in GitHub Desktop.
Save gautamdudeja90/990d0a675557a2484b4b0d365a260491 to your computer and use it in GitHub Desktop.
isolation_forest = IsolationForest(n_estimators=100)
isolation_forest.fit(df['Sales'].values.reshape(-1, 1))
xx = np.linspace(df['Sales'].min(), df['Sales'].max(), len(df)).reshape(-1,1)
anomaly_score = isolation_forest.decision_function(xx)
outlier = isolation_forest.predict(xx)
plt.figure(figsize=(10,4))
plt.plot(xx, anomaly_score, label='anomaly score')
plt.fill_between(xx.T[0], np.min(anomaly_score), np.max(anomaly_score),
where=outlier==-1, color='r',
alpha=.4, label='outlier region')
plt.legend()
plt.ylabel('anomaly score')
plt.xlabel('Sales')
plt.show();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment