Python 3.x 二维数据的numpy条件函数

Python 3.x 二维数据的numpy条件函数,python-3.x,numpy,k-means,Python 3.x,Numpy,K Means,我有一个由特性(X)和标签(y)组成的合成数据集,用于使用Python3.8、sklearn 0.22.2和numpy 1.19的KMeans集群 X.shape, y.shape # ((100, 2), (100,)) kmeans = KMeans(n_clusters = 3, init = 'random', n_init = 10, max_iter = 300) # Train model on scaled features- kmeans.fit(X) 在“X”上训练KM

我有一个由特性(X)和标签(y)组成的合成数据集,用于使用Python3.8、sklearn 0.22.2和numpy 1.19的KMeans集群

X.shape, y.shape
# ((100, 2), (100,))

kmeans = KMeans(n_clusters = 3, init = 'random', n_init = 10, max_iter = 300)

# Train model on scaled features-
kmeans.fit(X)
在“X”上训练KMeans之后,我想用使用KMeans获得的集群中心(离散)替换“X”的唯一(连续)值

for i in range(3):
    print("cluster number {0} has center = {1}".format(i + 1, kmeans.cluster_centers_[i, :]))
'''
cluster number 1 has center = [-0.7869159   1.14173859]
cluster number 2 has center = [ 1.28010442 -1.04663318]
cluster number 3 has center = [-0.54654735  0.0054752 ]
'''


set(kmeans.labels_)
# {0, 1, 2}
我的一种方法是:

X[np.where(clustered_labels == 0)] = val[0,:]
X[np.where(clustered_labels == 1)] = val[1,:]
X[np.where(clustered_labels == 2)] = val[2,:]
我可以用np.select()来做吗

但在执行代码时:

np.select(cond, val)  

             
我得到以下错误:

---------------------------------------------------------------------------ValueError回溯(最近的调用 最后)在 ---->1 np.选择(cond,val)

在select(*args,**kwargs)中

中的~/.local/lib/python3.8/site-packages/numpy/lib/function\u base.py 选择(条件列表、选项列表、默认值) 693结果\u形状=条件列表[0]。形状 694其他: -->695 result_shape=np.广播_数组(condlist[0],choicelist[0])[0]。shape 696 697结果=np.full(结果形状,选唱诗班[-1],数据类型)

在广播数组中(*args,**kwargs)

中的~/.local/lib/python3.8/site-packages/numpy/lib/stride\u.py 广播_数组(subok,*args) 256 args=[np.array(_m,copy=False,subok=subok)表示args中的_m] 257 -->258形状=_广播_形状(*args) 259 260如果全部(array.shape==参数中数组的形状):

中的~/.local/lib/python3.8/site-packages/numpy/lib/stride\u.py _广播形状(*args) 187#使用旧的迭代器,因为np.nditer不处理大小为0的数组 188#始终如一 -->189 b=np.广播(*args[:32]) 190#不幸的是,它无法直接处理32个或更多参数 范围内的位置为191(32,透镜(args),31):

ValueError:形状不匹配:无法将对象广播到单个 形状

建议


谢谢

比较干净的方法(但与您的方法非常相似)如下所示。下面是一个简单的例子:

from sklearn.cluster import KMeans
import numpy as np

x1 = np.random.normal(0, 2, 100)
y1 = np.random.normal(0, 1, 100)
label1 = np.ones(100)
d1 = np.column_stack([x1, y1, label1])

x2 = np.random.normal(3, 1, 100)
y2 = np.random.normal(1, 2, 100)
label2 = np.ones(100) * 2
d2 = np.column_stack([x2, y2, label2])

x3 = np.random.normal(-3, 0.5, 100)
y3 = np.random.normal(0.5, 0.25, 100)
label3 = np.ones(100) * 3
d3 = np.column_stack([x3, y3, label3])

D = np.row_stack([d1, d2, d3])
np.random.shuffle(D)
X = D[:, :2]
y = D[:, 2]

print(f'X.shape = {X.shape}, y.shape = {y.shape}')
# X.shape = (300, 2), y.shape = (300,)

kmeans = KMeans(n_clusters = 3, init = 'random', n_init = 10, max_iter = 300)

# Train model on scaled features-
kmeans.fit(X)

preds = kmeans.predict(X)
X[preds==0] = kmeans.cluster_centers_[0]
X[preds==1] = kmeans.cluster_centers_[1]
X[preds==2] = kmeans.cluster_centers_[2]
完成任务的另一种方法是使用
np.put
方法,而不是如下所示的赋值:

np.put(X, preds==0, kmeans.cluster_centers_[0])
np.put(X, preds==1, kmeans.cluster_centers_[1])
np.put(X, preds==2, kmeans.cluster_centers_[2])
坦白地说,我看不到通过
np.select
函数来完成任务的方法,我猜你这样做是最好的方法


干杯。

您能解释一下您想要的结果吗?是否X中的每个点都将其所属的簇作为其坐标(即(1,1)如果该点属于簇1,(2,2)如果它属于簇2等),或者您想为X中的每个点生成一个带有预测簇的新数组?@MichaelSidorov我想为每个数据点创建一个新的np数组,每个数据点的预测簇中心根据它所属的簇。这里,predict()指定簇标签,其中我想用簇形心值替换原始数据。好的,我给我的答案增加了另一种方式。干杯
np.put(X, preds==0, kmeans.cluster_centers_[0])
np.put(X, preds==1, kmeans.cluster_centers_[1])
np.put(X, preds==2, kmeans.cluster_centers_[2])