python - 连接不同结果的 2 个值

标签 python k-means

我在应用Kmeans后计算了数据集中不同点之间的欧氏距离,但无法获得与最小值相关的点。我的代码是

def ClusterIndicesComp(clustNum, labels_array): #list comprehension
    return np.array([i for i, x in enumerate(labels_array) if x == clustNum])

def newsol(max_gen,population,data):
    Slist = []
    #print('VAlue of NewSol Population is',population)
    for i in range(max_gen):
        cluster1=5
        K1.insert(i,cluster1)
        print('value of K1',K1)
        u,label,t,l=Kmeans_clu(cluster1, population)
        k2=Counter(l.labels_)
        print("Before addition values are\n",k2)#Count number of elements in each cluster
        k1=[t for (t, v) in k2.items() if v == 1]#Checking cluster of length one
        t1= np.array(k1)
        for b in range(len(t1)):iterating through the cluster with one point associated
            print("Value in NEW_SOL is of 1 length cluster\n",t1[b])
            plot1=data[ClusterIndicesComp(t1[b], l.labels_)] # Extract features from that cluster and store in plot1 
            print("Values are in sol of plot1",plot1)
            z=[t for (t, v) in k2.items() if v >2]#getting the cluster which have more than one point associated only than the distance is calculated 
            for d in range(len(z)):
                print("Value in NEW_SOL is of more than 2 length cluster\n", z[d])
                plot2 = data[ClusterIndicesComp(z[d], l.labels_)]# Extracting the features of the cluster of length more than one

现在从这里计算plot1和plotk之间的欧氏距离

                 for i in range(len(plot2)):  # To get one element at a time from plot2
                    plotk = plot2[i]
                    S = np.linalg.norm(np.array(plot1) - np.array(plotk))
                    print("Distance between plot1 and plotk is", S))  # euclidian distance is calculated
                    Slist.append(S) # List is appended with the distance 
                    Smin=min(Slist) #Min value from distance is selected 
                print("VAlues of Slist with min  \n",plotk,Smin)
                Slist=[] #Empty the list to move through next iteration 

最佳答案

我已经尝试了以下解决方案,它似乎有效。我相信可能有多个具有最小欧几里得距离的索引。

import numpy as np


plot1 = [1.0, 2.0, 3.0]
plot2 = [(1.0, 4.0, 5.0),
         (4.0, 7.0, 90.0),
         (1.0, 4.0, 5.0),
         (-1.0, -4.0, -5.0)]



indexes = []
for i in range(len(plot2)):  # To get one element at a time from plot2
    plotk = plot2[i]
    S = np.linalg.norm(np.array(plot1) - np.array(plotk))
    print("Distance between plot1 and plotk is %f"  %(S))  # euclidian distance is calculated
    if (i == 0):
        Smin = S
        Sminant = S
        indexes.append(i)
    else:
        if (S < Sminant):
            Smin = S
            indexes = []
            indexes.append(i)
        elif (S == Sminant):
            indexes.append(i)

print('indexes:')
print(indexes)

for i in range(len(indexes)):
   print("VAlues of Slist with min  \n",indexes[i], plot2[indexes[i]],Smin)

结果如下:

plot1和plotk之间的距离是2.828427 plot1和plotk之间的距离是87.195183 plot1和plotk之间的距离是2.828427 plot1和plotk之间的距离是10.198039 指标: [0, 2] Slist 的值与 min
0(1.0、4.0、5.0)2.8284271247461903 Slist 的值与 min
2(1.0、4.0、5.0)2.8284271247461903

关于python - 连接不同结果的 2 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50760855/

相关文章:

opencv:如何使用 kmeans() 按角度进行聚类

c++ - OpenCV,通过BOWKMeansTrainer得到的词汇矩阵

python - 如何在Python(而不是R)中使用igraph读取加权边缘列表?

python - 如何从 python 中没有 __init__.py 的文件夹导入文件?

python - pygame.display.update和pygame.display.flip之间的区别

python - 如何根据 python 中最近的集群质心逻辑将新观察分配给现有的 Kmeans 集群?

python - 是否可以在Python(Scikit-Learn)中的KMeans中对非 float 据进行聚类?

Python Pandas GroupBy 返回空白行

python - 在Python中打印文本树

matplotlib - 为什么Kmeans聚类中同一组簇数据点会落得很远或者很分散?