您可以使用 XBOS、HBOS、IsolationForest 等无监督异常值检测来实现此目的:
#create the dataframe
import pandas as pd
import numpy as np
data = {
'month': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
'Units_Sold': [23178.78, 23547.41,17720.51,25837.56,20375.98,16260.64,22881.59 ,25202.29 ,17255.29 ,20495.58,21253.27,20145.73]
}
df = pd.DataFrame(data)
df
#apply XBOS from this [Github](https://github.com/Kanatoko/XBOS-anomaly-detection)
from xbos import XBOS
xbos = XBOS(n_clusters=3)
result = xbos.fit_predict(df)
for i in result:
print(round(i,2))
#results
#-0.74, -0.74, -0.94, -0.74, -0.81, -1.12, -0.91, -0.91, -1.03, -0.9, -0.9, -0.9
# ^^^^ ^^^^
#include results in dataframe
df['outlier_score']= result
df
#Visualization of outliers
import matplotlib.pyplot as plt
OD = df.query('outlier_score < -1')
plt.scatter(df.month, df.Units_Sold)
# set x-axis label and specific size
plt.xlabel('month',size=16)
# set y-axis label and specific size
plt.ylabel('Units_Sold',size=16)
plt.title('Visulization of unsupervised outlier Detection',size=14)
plt.scatter(OD.month, OD.Units_Sold, color="red")
异常点用红色突出显示: