我有一个关于计算机程序每日使用情况的时间序列数据,下面是一个例子
正如你所看到的那样,11-06的峰值是102。由于我们收集这些数据的方式,我们知道数据可能是错误的,而且我们确信,根据其他值,102是不正确的。
所以我们需要清理这些肮脏的价值观。
有数学方法可以做到这一点吗?有蟒蛇库来帮助我们吗?
发布于 2017-11-13 04:15:00
我想你有几个选择:
x = [e for e in x if e < 100]这样简单的事情就可以了。x = [0,14,0,6,102,0,0] from sklearn.cluster import KMeans kmeans = KMeans(n_clusters=2).fit(np.array(x).reshape(-1, 1)) #First cluster: np.array(x)[np.where(kmeans.labels_ == 0)] #Second cluster (outliers): np.array(x)[np.where(kmeans.labels_ == 1)]希望这能有所帮助!
发布于 2017-11-12 20:39:34
一种解决方案是使用mean和variance来检测时间序列中的异常值。例如:
>> data=np.array([0,0,102,6,0,14,0])
>> c = 1
>> abs(data - np.mean(data)) < c * np.std(data)
Output: array([ True, True, False, True, True, True, True], dtype=bool)
>> clean_data= data[abs(data - np.mean(data)) < c * np.std(data)]
Output: array([ 0, 0, 6, 0, 14, 0])您可以根据您的需求使用c。
此外,与其使用所有数据的均值和方差,还可以对时间序列的每个部分(例如每30天)分别使用此方法。因为在不同的时间间隔内可能会有不同的行为。
发布于 2017-11-12 21:17:38
下面是我正在使用的内容:
import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth
x = [0,14,0,6,102,0,0]
X = list(zip(x,np.zeros(len(x))))
bandwidth = estimate_bandwidth(X, quantile=0.2)
ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
ms.fit(X)
labels = ms.labels_
cluster_centers = ms.cluster_centers_
labels_unique = np.unique(labels)
n_clusters_ = len(labels_unique)
X = np.array(X)
for k in range(n_clusters_):
my_members = labels == k
print(k, X[my_members, 0])来源:http://scikit-learn.org/stable/auto_示例/群集/情节_均值_shift.html
https://datascience.stackexchange.com/questions/24637
复制相似问题