Why I am getting 2 different outputs for same calculation using Python function and Numpy inbuilt dot() function

Viewed 24

I am trying to calculate dot.product using Python function and Numpy inbuilt dot() function. However i am getting two different outputs for same values.

# Python lists
arr1 = list(range(1000000))
arr2 = list(range(1000000, 2000000))

# Numpy arrays
arr1_np = np.array(arr1)
arr2_np = np.array(arr2)

result = 0
for x1, x2 in zip(arr1, arr2):
    result += x1*x2
result

O/P :- 833332333333500000


np.dot(arr1_np, arr2_np)

O/P:- -1942957984
1 Answers

It's because you didn't use your np_arrays in the loop. This way you get the same results:

arr1 = list(range(1000000))
arr2 = list(range(1000000, 2000000))

# Numpy arrays
arr1_np = np.array(arr1)
arr2_np = np.array(arr2)

result = 0
for x1, x2 in zip(arr1_np, arr2_np): #your issue was here
    result += x1*x2


np.dot(arr1_np, arr2_np)
Related