Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Arrays in Ascending
#7
Thanks
Actually 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
Reply


Messages In This Thread
Sorting Arrays in Ascending - by vino689 - Jan-10-2020, 05:40 PM
RE: Sorting Arrays in Ascending - by ndc85430 - Jan-10-2020, 06:35 PM
RE: Sorting Arrays in Ascending - by micseydel - Jan-10-2020, 06:45 PM
RE: Sorting Arrays in Ascending - by Marbelous - Jan-10-2020, 06:47 PM
RE: Sorting Arrays in Ascending - by vino689 - Jan-12-2020, 07:42 AM
RE: Sorting Arrays in Ascending - by perfringo - Jan-10-2020, 07:35 PM
RE: Sorting Arrays in Ascending - by micseydel - Jan-10-2020, 09:25 PM
RE: Sorting Arrays in Ascending - by vino689 - Jan-11-2020, 06:21 PM
RE: Sorting Arrays in Ascending - by vino689 - Jan-11-2020, 08:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding an ascending number [SOLVED] AlphaInc 3 2,382 Jul-11-2021, 10:13 AM
Last Post: perfringo
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,329 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020