Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python's Numpy dot function returning incorrect value, why?

Real simple, my code is:

import numpy as np


a = np.array([0.4, 0.3])
b = np.array([-0.15, 0.2])

print(np.dot(a,b))

The dot product of this should be 0, and instead i get:

3.3306690738754695e-18

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For something like that i’d suggest just using the rounding function to however many decimals you got, or using this:

dotproduct=0
for a,b in zip(a,b):
    dotproduct = dotproduct+a*b
print('Dot product is:', dotproduct)

Which does the dot product regardless of floats or integers.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading