Last active
July 25, 2021 14:36
-
-
Save wmlba/89bc2f4556b8ee397ca7a5017b497657 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
seed(1) | |
# multiply and add by random numbers to get some real values | |
data = np.random.randn(50000) * 20 + 20 | |
# Function to Detection Outlier on one-dimentional datasets. | |
def find_anomalies(data): | |
#define a list to accumlate anomalies | |
anomalies = [] | |
# Set upper and lower limit to 3 standard deviation | |
random_data_std = std(random_data) | |
random_data_mean = mean(random_data) | |
anomaly_cut_off = random_data_std * 3 | |
lower_limit = random_data_mean - anomaly_cut_off | |
upper_limit = random_data_mean + anomaly_cut_off | |
print(lower_limit) | |
# Generate outliers | |
for outlier in random_data: | |
if outlier > upper_limit or outlier < lower_limit: | |
anomalies.append(outlier) | |
return anomalies | |
find_anomalies(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
'random_data' in the definition of the function should be replaced by 'data'