Python Forum
smallest Cosine distance in Graph
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
smallest Cosine distance in Graph
#1
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
#2
Im new here so maybe I don't know but it looks like you are missing your tab spacing. Maybe try posting the code in code tags to preserve tab spacing. Do that first before I try to guess what is part of the for loop and what is after the for loop.
Reply
#3
(Jan-12-2020, 04:49 AM)rmspacedashrf Wrote: Im new here so maybe I don't know but it looks like you are missing your tab spacing. Maybe try posting the code in code tags to preserve tab spacing. Do that first before I try to guess what is part of the for loop and what is after the for loop.

I tried but its not getting indented , FYI.. in the Code all the lines are indented after for loop
Reply
#4
https://python-forum.io/misc.php?action=help&hid=25

you need two dimensions so that you can store data about your data and it will remain correlated even after a sort.

https://snakify.org/en/lessons/two_dimen...ts_arrays/

https://docs.python.org/3.3/howto/sorting.html

I never learned a way to do S[][].sort with two dimensional arrays so the only way I know with a built in function is sorted()
if it breaks because of the two dimensional array I would just write a sort function from scratch that is orthogonal to my two dimensional array. I hope this helps get you in the right direction. sorry I don't have all the answers. I am a first year student.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting numbers from smallest to biggest Dokugan 2 2,217 Apr-14-2020, 09:24 PM
Last Post: Larz60+
  Visualize Geo Map/Calculate distance zarize 1 1,891 Dec-05-2019, 08:36 PM
Last Post: Larz60+
  how to show the distance between two curves in a graph termo 6 6,992 Oct-21-2019, 09:08 AM
Last Post: DeaD_EyE
  I want to check for the smallest value entered by the user Shilpa 2 1,801 Aug-13-2019, 12:06 PM
Last Post: ThomasL
  euclidean distance jenya56 3 2,801 Mar-29-2019, 02:56 AM
Last Post: scidam
  How do I calculate the smallest value that is recognized as a difference when compari AFoeee 1 2,779 Oct-28-2018, 10:48 PM
Last Post: Gribouillis
  organizing by distance gonzo620 7 3,894 Oct-16-2018, 01:41 AM
Last Post: stullis
  Python Ble Distance Problem aleynasarcanli 10 12,495 Feb-09-2018, 10:48 PM
Last Post: aleynasarcanli
  How to find the cosine of an angle sylas 6 5,197 May-25-2017, 04:29 PM
Last Post: sylas

Forum Jump:

User Panel Messages

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