Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

When comparing an n-dimensional array to a 1d array, how do you return the row most similar to the 1d array?

I want to take an array new_arr:

[[ 0.  1.  4.  4. 25. 25.][ 1. 36. 36.  9.  4.  0.][ 4.  4. 16. 16. 36.  1.][ 0.  1. 16. 64. 64. 81.]]

and see which of the rows is most similar to the following array dot_prod:

[ 59.  86.  77. 226.]

to be clear I'm using numpy and pandas. Here is my code below:

k = np.array([["words words ", 1,1,3,4,6,7], ["blah blah", 2,8,7,5,3,2], [" please help me", 3,4, 5, 6, 7,1], [" What are you doing, man", 1,3,5,10,9,11]])
weights = np.array([1,1,1,1,1,1])

df = pd.DataFrame(k[:, 1:].astype(int), index=k[:, 0])
df = abs(df - probe ) # probe is a user input array of [1, 2, 3, 4, 5, 6]
df = df*df
print(df , "----- df -----")
print('')
dot_prod = np.dot(df,weights)
print(dot_prod , "hmm =-=-=-=")
print('')

new_arr = np.asarray(df)
print('')
print(new_arr)
print('')

close_val = (np.abs(new_arr - dot_prod)).argmin()
print(close_val, "close_val -=")

Comments