Ссылка 2 значения разного результата - PullRequest
0 голосов
/ 08 июня 2018

Я вычислил евклидово расстояние между различными точками в наборе данных после применения 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 

Ответы [ 2 ]

0 голосов
/ 18 июня 2018

В приведенном выше ответе была ошибка с небольшими изменениями, она бы работала нормально

  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
            Sminat=Smin  #This part should be there
            indexes = []
            indexes.append(i)
        elif (S == Sminant):
             indexes=[]  # If you want single index 
            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)
0 голосов
/ 08 июня 2018

Я пробовал следующее решение, и оно, похоже, работает.Я считаю, что, возможно, несколько индексов с минимальным евклидовым расстоянием.

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 и plotk2.828427 Расстояние между plot1 и plotk составляет 10.198039 индексов: [0, 2] VAlues of Slist с min
0 (1.0, 4.0, 5.0) 2.8284271247461903 VAlues of Slist с min
2 (1.0, 4.0, 5.0) 2.8284271247461903

...