-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdet-noise.py
37 lines (26 loc) · 1005 Bytes
/
det-noise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor
def main():
x, y = load_breast_cancer(return_X_y=True)
x = StandardScaler().fit_transform(x)
# local outlier factor
clf = LocalOutlierFactor(n_neighbors=20, contamination=0.02)
y_pred = clf.fit_predict(x)
x_scores = clf.negative_outlier_factor_
outlier_num = (y_pred == -1).sum()
idx = np.where(y_pred == -1)[0]
print(f"outlier num: {outlier_num}")
print(f"LOF, outlier idx: {idx}, score: {x_scores[idx]}")
# isolation forest
clf2 = IsolationForest(max_samples='auto', contamination=0.02)
clf2.fit(x)
y_pred = clf2.predict(x)
idx = np.where(y_pred == -1)[0]
print(f"Isolation forest, outlier idx: {idx}")
if __name__ == "__main__":
main()
# Reference
# https://scikit-learn.org/stable/modules/outlier_detection.html