gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:50:33 27 4
gpt4 key购买 nike

我在应用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.828427plot1和plotk之间的距离是87.195183plot1和plotk之间的距离是2.828427plot1和plotk之间的距离是10.198039指标:[0, 2]Slist 的值与 min
0(1.0、4.0、5.0)2.8284271247461903Slist 的值与 min
2(1.0、4.0、5.0)2.8284271247461903

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com