简体   繁体   中英

normalization of the same vector gives different values at two cases?

 r_capr
Out[148]: array([[-0.42300825,  0.90516059,  0.04181294]])

 r_capr
 np.linalg.norm(r_capr.T)
Out[149]: 0.99999999760432712

 a.T
Out[150]: array([[-0.42300825,  0.90516059,  0.04181294]])

 a.T
 np.linalg.norm(a.T)
Out[151]: 1.0

In the above we can see for the same vector we have different norm? Why is it happening?

Machines are not 100% precise with numbers seeing as they are stored with finite precision (depending on architecture it could be 16 to 128 bits float point) so numbers that are very precise such as getting close to the limit of a float point mantissa are more prone to errors. Given the machine precision error, you can safely assume those numbers are actually the same. When computing norms it may make more sense to scale or otherwise modify your numbers to get less error prone results.

Also using dot(x,x) instead of an l2 norm can be much more accurate since it avoids the square root.

See http://en.wikipedia.org/wiki/Machine_epsilon for a better discussion since this is actually a fairly complex topic.

Your exact error is caused by machine errors but since your vectors are not actually equal (you are showing two logically equivalent vectors but their internal representation will be different) the calculation of the norm is probably being processed with different precision numbers.

See this:

a = mat('-0.42300825 ; 0.90516059 ; 0.04181294', np.float32)
r = mat('-0.42300825 ; 0.90516059 ; 0.04181294', np.float64)
print linalg.norm(a)
print linalg.norm(r)

and compare the results. It will get the exact results you are seeing. You can also verify this by checking the dtype property of your matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM