[[Z-scoreによる異常検知]] [[3σ則|n-sigma rule]] ## ChatGPTによるコード # me Can you code anomaly detection of zscore + n-sigma rule in python? # ai Sure, I can certainly help with that. The Z-score is a statistical measurement that describes a value's relationship to the mean of a group of values. In the context of anomaly detection, it's used to find data points that are "far away" from the mean. In the following example, we'll use a simple 1-D dataset, but this method can be expanded to higher dimensions as well. The n-sigma rule is a rule of thumb stating that nearly all values lie within n standard deviations of the mean in a normal distribution. We'll use this to classify points that are far from the mean as anomalies. Here is how you might do it in Python using numpy: ```python import numpy as np # Sample data data = np.random.normal(size=100) # Function to detect anomalies def detect_anomalies(data, n_sigmas=2): mean = np.mean(data) std_dev = np.std(data) # Compute the z scores z_scores = (data - mean) / std_dev # Detect anomalies anomalies = data[np.abs(z_scores) > n_sigmas] return anomalies # Detect anomalies anomalies = detect_anomalies(data, n_sigmas=3) print(f"Anomalies: {anomalies}") ``` In this code, `n_sigmas` is the number of standard deviations away from the mean that a point must be for it to be considered an anomaly. You can adjust this parameter to make the anomaly detection more or less sensitive. A higher value will make it less sensitive (fewer points will be classified as anomalies), and a lower value will make it more sensitive (more points will be classified as anomalies).