Skip to content

Commit 4d9a7ca

Browse files
author
leksono
committed
add z_score_for_detect_outliers
1 parent f23c48d commit 4d9a7ca

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Python/z_score_for_detect_outliers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
import numpy as np
3+
4+
def find_anomalies_z_score(list_data):
5+
outliers = []
6+
7+
std_data = np.std(list_data); print('std:', std_data)
8+
mean_data = np.mean(list_data); print('mean:', mean_data)
9+
anomaly_cut_off = std_data * 3; print('anomaly_cut_off:', anomaly_cut_off)
10+
11+
lower_limit = mean_data - anomaly_cut_off; print('lower_limit', lower_limit)
12+
upper_limit = mean_data + anomaly_cut_off; print('upper_limit', upper_limit)
13+
14+
for outlier in list_data:
15+
if outlier > upper_limit or outlier < lower_limit:
16+
outliers.append(outlier)
17+
18+
if len(outliers) == 0:
19+
print('outlier is not found')
20+
21+
return outliers
22+
23+
if __name__ == '__main__':
24+
data = 5 * np.random.randn(1000) + 50
25+
result = find_anomalies_z_score(data)
26+
print('outliers : ', result)

0 commit comments

Comments
 (0)