KMeans聚类 – 值错误:n_samples=1应该大于等于n_clusters

我在进行一个实验,使用了三个具有不同特征的时间序列数据集,其格式如下所示。

    0.086206438,10    0.086425551,12    0.089227066,20    0.089262508,24    0.089744425,30    0.090036815,40    0.090054172,28    0.090377569,28    0.090514071,28    0.090762872,28    0.090912691,27

第一列是timestamp。为了可重复性,我在这里分享了数据链接。从第二列开始,我想读取当前行并与前一行的值进行比较。如果当前值大于前一行的值,我继续比较。如果当前值小于前一行的值,我想将当前值(较小值)除以前一值(较大值)。相应的代码如下:

import numpy as npimport matplotlib.pyplot as pltprotocols = {}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,    }    plt.figure(); plt.clf()    plt.plot(quotient_times,quotient, ".", label=protname, color="blue")    plt.ylim(0, 1.0001)    plt.title(protname)    plt.xlabel("time")    plt.ylabel("quotient")    plt.legend()    plt.show()

这会产生以下三个点 – 每个我共享的数据集一个点。

enter image description hereenter image description hereenter image description here

从上述代码生成的图表中的点可以看出,data1的值非常一致,约为1,data2会有两个商值(值集中在0.5或0.8附近),而data3的值集中在两个值附近(0.5或0.7附近)。这样,给定一个新的数据点(具有quotientquotient_times),我想通过构建每个数据集来堆叠这两个转换后的特征quotientquotient_times,以了解它属于哪个cluster。我尝试使用KMeans聚类如下所示

from sklearn.cluster import KMeansk_means = KMeans(n_clusters=3, random_state=0)k_means.fit(quotient)

但这会给我一个错误:ValueError: n_samples=1 should be >= n_clusters=3。我们如何解决这个错误?

更新:样本商数据 = array([ 0.7 , 0.7 , 0.4973262 , 0.7008547 , 0.71287129, 0.704 , 0.49723757, 0.49723757, 0.70676692, 0.5 , 0.5 , 0.70754717, 0.5 , 0.49723757, 0.70322581, 0.5 , 0.49723757, 0.49723757, 0.5 , 0.49723757])


回答:

目前,你的quotient变量现在是一个单一的样本;我得到一个不同的错误消息,可能是因为不同的Python/scikit-learn版本,但本质上是相同的:

import numpy as npquotient = np.array([ 0.7 , 0.7 , 0.4973262 , 0.7008547 , 0.71287129, 0.704 , 0.49723757, 0.49723757, 0.70676692, 0.5 , 0.5 , 0.70754717, 0.5 , 0.49723757, 0.70322581, 0.5 , 0.49723757, 0.49723757, 0.5 , 0.49723757])quotient.shape# (20,)from sklearn.cluster import KMeansk_means = KMeans(n_clusters=3, random_state=0)k_means.fit(quotient)

这会产生以下错误:

ValueError: Expected 2D array, got 1D array instead:array=[0.7        0.7        0.4973262  0.7008547  0.71287129 0.704 0.49723757 0.49723757 0.70676692 0.5        0.5        0.70754717 0.5        0.49723757 0.70322581 0.5        0.49723757 0.49723757 0.5        0.49723757].Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

尽管措辞不同,但与你的错误并无不同 – 基本上它说你的数据看起来像一个单一的样本。

按照第一个建议(即认为quotient包含一个单一的特征(列))解决了这个问题:

k_means.fit(quotient.reshape(-1,1))# resultKMeans(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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注