Skip to content

Instantly share code, notes, and snippets.

@wmlba
Last active July 25, 2021 14:36
Show Gist options
  • Save wmlba/89bc2f4556b8ee397ca7a5017b497657 to your computer and use it in GitHub Desktop.
Save wmlba/89bc2f4556b8ee397ca7a5017b497657 to your computer and use it in GitHub Desktop.
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)
@xwhuaduo
Copy link

xwhuaduo commented Apr 3, 2019

'random_data' in the definition of the function should be replaced by 'data'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment