from sklearn.datasets import load_diabetes a = load_diabetes() X = a.data
from sklearn.ensemble import IsolationForest
clf = IsolationForest(n_estimators=100, contamination=0.1, random_state=42) clf.fit(X)
y_pred = clf.predict(X)
import matplotlib.pyplot as plt
plt.scatter(X[:, 0], X[:, 1], c=y_pred, cmap='viridis') plt.title("Isolation Forest Outlier Detection on DIABETES Dataset") plt.xlabel("Sepal Length") plt.ylabel("Sepal Width") plt.show()
from sklearn.neighbors import LocalOutlierFactor
clf = LocalOutlierFactor(n_neighbors=20, contamination=0.1) y_pred = clf.fit_predict(X)
plt.scatter(X[:, 0], X[:, 1], c=y_pred, cmap='viridis') plt.title("Local Outlier Factor Outlier Detection on DIABETES Dataset") plt.xlabel("Sepal Length") plt.ylabel("Sepal Width") plt.show()
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_iris from sklearn.ensemble import IsolationForest from sklearn.neighbors import LocalOutlierFactor
a = load_diabetes() X = a.data y = a.target
clf_iso = IsolationForest(contamination=0.1, random_state=42) y_pred_iso = clf_iso.fit_predict(X)
clf_lof = LocalOutlierFactor(contamination=0.1) y_pred_lof = clf_lof.fit_predict(X)
plt.scatter(X[:, 0], X[:, 1], c=np.where(y_pred_iso == -1, 'red', 'blue'), label='Isolation Forest') plt.title("Outlier Detection using Isolation Forest on DIABETES Dataset") plt.xlabel("Sepal Length") plt.ylabel("Sepal Width") plt.legend() plt.show()
plt.scatter(X[:, 0], X[:, 1], c=np.where(y_pred_lof == -1, 'red', 'blue'), label='Local Outlier Factor') plt.title("Outlier Detection using Local Outlier Factor on DIABETES Dataset") plt.xlabel("Sepal Length") plt.ylabel("Sepal Width") plt.legend() plt.show()