k-shape.py

**k-shape聚类论文:**Technical Perspective: k-Shape: Efficient and Accurate Clustering of Time Series
Zachary G. IvesPublished in SIGMOD Record 2016
DOI:10.1145/2949741.2949757
Database research frequently cuts across many layers of abstraction (from formal foundations to algorithms to languages to systems) and the software stack (from data storage and distribution to runtime systems and query optimizers). It does this in a way that is specialized to a particular class of data and workloads. Over the decades, we have seen this pattern applied to enterprise data, persistent objects, Web data, sensor data, data streams, and so on. Each time, the community has developed… CONTINUE READING

import numpy
import matplotlib.pyplot as plt

from tslearn.clustering import KShape
from tslearn.datasets import CachedDatasets
from tslearn.preprocessing import TimeSeriesScalerMeanVariance

seed = 0
numpy.random.seed(seed)
X_train, y_train, X_test, y_test = CachedDatasets().load_dataset(“Trace”)
X_train = X_train[y_train < 4] # Keep first 3 classes
numpy.random.shuffle(X_train)
X_train = TimeSeriesScalerMeanVariance().fit_transform(X_train[:50]) # Keep only 50 time series
sz = X_train.shape[1]

Euclidean k-means

ks = KShape(n_clusters=3, verbose=True, random_state=seed)
y_pred = ks.fit_predict(X_train)

plt.figure()
for yi in range(3):
plt.subplot(3, 1, 1 + yi)
for xx in X_train[y_pred == yi]:
plt.plot(xx.ravel(), “k-”, alpha=.2)
plt.plot(ks.cluster_centers_[yi].ravel(), “r-”)
plt.xlim(0, sz)
plt.ylim(-4, 4)
plt.title(“Cluster %d” % (yi + 1))

plt.tight_layout()
plt.show()

猜你喜欢

转载自blog.csdn.net/JiangLongShen/article/details/88601722
py