def chi2_distance(a,b):
d=1-(np.dot(a,b)/(norm(a,axis=1)*norm(b)))
return d
i get the variable display in 8 decimals in both two codes, although i forcely put the np dtype to float64 in below.
a.shape is r*n and b shape is (n,)
i did this :
def chi2_distance(a,b):
a = a.astype(np.float64)
b = b.astype(np.float64)
d=1-(np.dot(a,b)/(norm(a,axis=1)*norm(b)))
return d
i still get 8 decimal accuray in results
@hpaulj answered correct also. Python and numpy use float64 for accuracy and calculation in fact. But numpy shows the numbers and variables in 8 decimals by its default, meanwhile python itself like to.list() as an instance shows the numbers 15 to 17 deciamals. This is just diplay, but both of them use float64 accuracy in calculation. Here in below code if you even delete (.float64) for two lines, again you get the same answer, as numpy and python by default calculate in float64. I got my problem and mistake and i wanted to share it with you. Maybe you had same mistake like me.
import numpy as np
from numpy.linalg import norm
def chi2_distance(a,b):
a = a.astype(np.float64)
b = b.astype(np.float64)
d=(1-(np.dot(a,b)/(norm(a,axis=1)*norm(b)))).tolist()
return d
k=np.array([[8.34567,2,4],[10000.99887,6,7]])
kk=np.array([100.3456,200,300])
print(k.shape)
print(kk.shape)
hh=chi2_distance(k,kk)
print(hh)