Jan-12-2020, 03:58 AM
(This post was last modified: Jan-12-2020, 07:34 AM by Gribouillis.)
my problem is to list the smallest Cosine distance in Graph
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)] # these are the list of x & y axix
P= (3,-4) # so here i have to find the distance between each tuple in S with P & find the top 5 lowest distance axis
below is the code i have written to find the cosine distance between each Tuple in S & P .
import math
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
for i in range (0,len(S)):
#print(S[i])
#print(np.ndim(S[i]))
J=np.reshape(S[i],(1,2))
#print(J)
#print(np.ndim(J))
P=[(3,-4)]
#print(np.ndim(P))
A=cosine_similarity(J,P)
#print(A)
C=1-A # Cosine Distance = 1-cosine similarity
#print©
#print(np.ndim©)
D=np.reshape(C,(1))
#print(np.ndim(D))
print(D)
# if i print D , i am getting the below Cosine distance
[1.4472136]
[1.28]
[1.98994949]
[0.00211989]
[1.8]
[0.63960072]
[0.85857864]
[0.4]
[0.01005051]
now i want to Sort 5 distance in ascending order (Less distance) & to print the respective X & Y axis
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)] # these are the list of x & y axix
P= (3,-4) # so here i have to find the distance between each tuple in S with P & find the top 5 lowest distance axis
below is the code i have written to find the cosine distance between each Tuple in S & P .
import math
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
S= [(1,2),(3,4),(-1,1),(6,-7),(0, 6),(-5,-8),(-1,-1),(6,0),(1,-1)]
for i in range (0,len(S)):
#print(S[i])
#print(np.ndim(S[i]))
J=np.reshape(S[i],(1,2))
#print(J)
#print(np.ndim(J))
P=[(3,-4)]
#print(np.ndim(P))
A=cosine_similarity(J,P)
#print(A)
C=1-A # Cosine Distance = 1-cosine similarity
#print©
#print(np.ndim©)
D=np.reshape(C,(1))
#print(np.ndim(D))
print(D)
# if i print D , i am getting the below Cosine distance
[1.4472136]
[1.28]
[1.98994949]
[0.00211989]
[1.8]
[0.63960072]
[0.85857864]
[0.4]
[0.01005051]
now i want to Sort 5 distance in ascending order (Less distance) & to print the respective X & Y axis