【发布时间】:2017-12-10 05:40:08
【问题描述】:
假设一个数据框包含 1000 行。每行代表一个时间序列。
然后我构建了一个 DTW 算法来计算 2 行之间的距离。
我不知道接下来要做什么来为数据帧完成一个无监督的分类任务。
如何标注数据框的所有行?
【问题讨论】:
-
没有knn聚类。
标签: python time-series cluster-analysis
假设一个数据框包含 1000 行。每行代表一个时间序列。
然后我构建了一个 DTW 算法来计算 2 行之间的距离。
我不知道接下来要做什么来为数据帧完成一个无监督的分类任务。
如何标注数据框的所有行?
【问题讨论】:
标签: python time-series cluster-analysis
定义
KNN 算法 = K-最近邻分类算法
K-means = 基于质心的聚类算法
DTW = Dynamic Time Warping 一种用于时间序列的相似性测量算法
我将在下面逐步展示如何构建两个时间序列以及如何计算动态时间规整 (DTW) 算法。您可以使用 scikit-learn 构建无监督 k-means 聚类,而无需指定质心的数量,然后 scikit-learn 就会知道使用名为 auto 的算法。
你有两个时间序列,你计算 DTW 使得
import pandas as pd
import numpy as np
import random
from dtw import dtw
from matplotlib.pyplot import plot
from matplotlib.pyplot import imshow
from matplotlib.pyplot import cm
from sklearn.cluster import KMeans
from sklearn.preprocessing import MultiLabelBinarizer
#About classification, read the tutorial
#http://scikit-learn.org/stable/tutorial/basic/tutorial.html
def createTs(myStart, myLength):
index = pd.date_range(myStart, periods=myLength, freq='H');
values= [random.random() for _ in range(myLength)];
series = pd.Series(values, index=index);
return(series)
#Time series of length 30, start from 1/1/2000 & 1/2/2000 so overlap
myStart='1/1/2000'
myLength=30
timeS1=createTs(myStart, myLength)
myStart='1/2/2000'
timeS2=createTs(myStart, myLength)
#This could be your dataframe but unnecessary here
#myDF = pd.DataFrame([x for x in timeS1.data], [x for x in timeS2.data])#, columns=['data1', 'data2'])
x=[xxx*100 for xxx in sorted(timeS1.data)]
y=[xx for xx in timeS2.data]
choice="dtw"
if (choice="timeseries"):
print(timeS1)
print(timeS2)
if (choice=="drawingPlots"):
plot(x)
plot(y)
if (choice=="dtw"):
#DTW with the 1st order norm
myDiff=[xx-yy for xx,yy in zip(x,y)]
dist, cost, acc, path = dtw(x, y, dist=lambda x, y: np.linalg.norm(myDiff, ord=1))
imshow(acc.T, origin='lower', cmap=cm.gray, interpolation='nearest')
plot(path[0], path[1], 'w')
关于应该标记什么以及使用哪些标签的问题并不明显?所以请提供以下详细信息
之后我们可以决定我们的分类算法,可能是所谓的KNN算法。它的工作原理是你有两个独立的数据集:训练集和测试集。通过训练集,您可以教算法标记时间序列,而测试集是一种工具,我们可以通过它来衡量模型与 AUC 等模型选择工具的配合情况。
小谜题在提供有关问题的详细信息之前一直打开
#PUZZLE
#from tutorial (#http://scikit-learn.org/stable/tutorial/basic/tutorial.html)
newX = [[1, 2], [2, 4], [4, 5], [3, 2], [3, 1]]
newY = [[0, 1], [0, 2], [1, 3], [0, 2, 3], [2, 4]]
newY = MultiLabelBinarizer().fit_transform(newY)
#Continue to the article.
关于分类器的 Scikit-learn 比较文章在下面的第二个枚举项中提供。
K-means 是聚类算法及其无监督版本,您可以这样使用
#Unsupervised version "auto" of the KMeans as no assignment for the n_clusters
myClusters=KMeans(path)
#myClusters.fit(YourDataHere)
这是与 KNN 算法非常不同的算法:这里我们不需要任何标签。我在第一个枚举项中为您提供了有关以下主题的更多材料。
scikit learn中分类器的比较here
【讨论】:
您可以使用DTW。事实上,我的一个项目也遇到了同样的问题,我用 Python 编写了自己的类。
这是逻辑;
n! / k! / (n-k)!。这些可能类似于潜在的中心。还有代码;
import numpy as np
import pandas as pd
from itertools import combinations
import time
def dtw_distance(x, y, d=lambda x,y: abs(x-y), scaled=False, fill=True):
"""Finds the distance of two arrays by dynamic time warping method
source: https://en.wikipedia.org/wiki/Dynamic_time_warping
Dependencies:
import numpy as np
Args:
x, y: arrays
d: distance function, default is absolute difference
scaled: boolean, should arrays be scaled before calculation
fill: boolean, should NA values be filled with 0
returns:
distance as float, 0.0 means series are exactly same, upper limit is infinite
"""
if fill:
x = np.nan_to_num(x)
y = np.nan_to_num(y)
if scaled:
x = array_scaler(x)
y = array_scaler(y)
n = len(x) + 1
m = len(y) + 1
DTW = np.zeros((n, m))
DTW[:, 0] = float('Inf')
DTW[0, :] = float('Inf')
DTW[0, 0] = 0
for i in range(1, n):
for j in range(1, m):
cost = d(x[i-1], y[j-1])
DTW[i, j] = cost + min(DTW[i-1, j], DTW[i, j-1], DTW[i-1, j-1])
return DTW[n-1, m-1]
def array_scaler(x):
"""Scales array to 0-1
Dependencies:
import numpy as np
Args:
x: mutable iterable array of float
returns:
scaled x
"""
arr_min = min(x)
x = np.array(x) - float(arr_min)
arr_max = max(x)
x = x/float(arr_max)
return x
class TrendCluster():
def __init__(self):
self.clusters = None
self.centers = None
self.scale = None
def fit(self, series, n=2, scale=True):
'''
Work-flow
1 - make series combination with size n, initial clusters
2 - assign closest series to each cluster
3 - calculate total distance for each combinations
4 - choose the minimum
Args:
series: dict, keys can be anything, values are time series as list, assumes no nulls
n: int, cluster size
scale: bool, if scale needed
'''
assert isinstance(series, dict) and isinstance(n, int) and isinstance(scale, bool), 'wrong argument type'
assert n < len(series.keys()), 'n is too big'
assert len(set([len(s) for s in series.values()])) == 1, 'series length not same'
self.scale = scale
combs = combinations(series.keys(), n)
combs = [[c, -1] for c in combs]
series_keys = pd.Series(series.keys())
dtw_matrix = pd.DataFrame(series_keys.apply(lambda x: series_keys.apply(lambda y: dtw_distance(series[x], series[y], scaled=scale))))
dtw_matrix.columns, dtw_matrix.index = series_keys, series_keys
for c in combs:
c[1] = dtw_matrix.loc[c[0], :].min(axis=0).sum()
combs.sort(key=lambda x: x[1])
self.centers = {c:series[c] for c in combs[0][0]}
self.clusters = {c:[] for c in self.centers.keys()}
for k, _ in series.items():
tmp = [[c, dtw_matrix.loc[k, c]] for c in self.centers.keys()]
tmp.sort(key=lambda x: x[1])
cluster = tmp[0][0]
self.clusters[cluster].append(k)
return None
def assign(self, serie, save=False):
'''
Assigns the serie to appropriate cluster
Args:
serie, dict: 1 element dict
save, bool: if new serie is stored to clusters
Return:
str, assigned cluster key
'''
assert isinstance(serie, dict) and isinstance(save, bool), 'wrong argument type'
assert len(serie) == 1, 'serie\'s length is not exactly 1'
tmp = [[c, dtw_distance(serie.values()[0], self.centers[c], scaled=self.scale)] for c in self.centers.keys()]
tmp.sort(key=lambda x: x[1])
cluster = tmp[0][0]
if save:
self.clusters[cluster].append(serie.keys()[0])
return cluster
如果你想看到它的实际效果,你可以参考我关于Time Series Clustering的存储库。
【讨论】:
fit 方法中使用dtw_distance 计算每个样本对的距离,nxn 次。这就是为什么有一个方阵。距离函数只是一个工具,fit 方法是使用该工具的地方。对于第二个问题,我认为它更接近于分区。我在样本中选择最好的中心(而不是 K-means 的随机创建和迭代)。然后,我只是通过选择最接近的来分配其余部分。最佳的定义是组合的最小总距离。这也是它有点慢的原因。