【发布时间】:2019-07-17 09:49:05
【问题描述】:
我正在使用KMeans 对具有不同特征的三个时间序列数据集进行聚类。出于可重复性的原因,我正在分享数据here。
这是我的代码
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
protocols = {}
types = {"data1": "data1.csv", "data2": "data2.csv", "data3": "data3.csv"}
for protname, fname in types.items():
col_time,col_window = np.loadtxt(fname,delimiter=',').T
trailing_window = col_window[:-1] # "past" values at a given index
leading_window = col_window[1:] # "current values at a given index
decreasing_inds = np.where(leading_window < trailing_window)[0]
quotient = leading_window[decreasing_inds]/trailing_window[decreasing_inds]
quotient_times = col_time[decreasing_inds]
protocols[protname] = {
"col_time": col_time,
"col_window": col_window,
"quotient_times": quotient_times,
"quotient": quotient,
}
k_means = KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
n_clusters=3, n_init=10, n_jobs=None, precompute_distances='auto',
random_state=0, tol=0.0001, verbose=0)
k_means.fit(quotient.reshape(-1,1))
这样,给定一个新的数据点(quotient 和 quotient_times),我想知道它属于哪个 cluster,方法是构建每个数据集,将这两个转换后的特征 quotient 和 quotient_times 与KMeans。
k_means.labels_ 给出此输出 array([1, 1, 0, 1, 2, 1, 0, 0, 2, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0], dtype=int32)
最后,我想使用plt.plot(k_means, ".",color="blue") 可视化集群,但出现此错误:TypeError: float() argument must be a string or a number, not 'KMeans'。我们如何绘制KMeans 集群?
【问题讨论】:
-
您不想绘制
KMeans类,对吧?但取而代之的是一些数字。但是你想绘制什么数字?预测?集群中心? -
我想要两个图 1)预测和 2)
KMeans类。
标签: python matplotlib machine-learning scikit-learn k-means