Numerical Computing With Numpy

Running jupyter notebook on my laptop with conda. It’s giving wrong output for function np.dot()
and negative value for large range. Negative value is impossible…then how…plz answer.
sharing link of notebook: ssd7385/numpy-2 - Jovian

1 Like

Hey @ssd7385
I looked your notebook and tried some more examples and did some research…
And I came up with this solution and reason…

Now let me tell you that The operations are working properly and expectedly at my side as All the ouputs are correct
This can be because of different system specifications like 32-bit or 64-bit…but I am not sure…

The main reason behind the problem is that while creating numpy arrays…
like arr1_np, numpy is assinging the 32-bit integer value
You can see this by doing print(arr1_np.dtype)
In my case I am getting int64 but you should be getting int32
Now, in int32 the highest value you can store is 231 - 1 which is 2147483647
As you can guess our calculated value in case of first two array is greater than this…
So it is producing wrong result and same goes for second case…

Solution:
Now there is a solution to solve this problem…
While making numpy arrays you can specify the datatype you want for example, while making first two arrays we can do this…

arr1_np = np.array(arr1, dtype=float)
arr2_np = np.array(arr2, dtype=float)

This would produce the correct result but in decimal…
For integer solution, we need a 64-bit integer to get the right result…
*Note: This would also produce wrong result if the required result is greater than 263-1 which is 9,223,372,036,854,775,807

arr1_np = np.array(arr1, dtype=np.int64)
arr2_np = np.array(arr2, dtype=np.int64)

this creates the array with 64-bit integer…
I am not sure if this would work in a 32-bit system though…Maybe you can try and see if this worlk…
But nonetheless the float method would work no matter what

Hope it helps…
Thank You

2 Likes

Thank you sir, float method solved that problem. But my laptop has windows10-64-bit OS , also I have installed 64-bit version of python and anaconda, then why numpy assigning 32-bit integer value?
moreover, I am getting right output on binder jupyter notebook without float method and wrong on local notebook. Why is it so? please suggest solution.
I tried second method arr1_np = np.array(arr1, dtype=np.int64) also, but it is giving error saying int 64 is not defined.
sharing link of updated notebook: ssd7385/numpy-2 - Jovian

1 Like

Can you try arr1_np = np.array(arr1).astype('int64'), for the other question of why numpy is assigning 32-bit integer, it seems numpy assigns 32 bit integer by default(though I am not sure) but I have seen many 64 bit PC’s where numpy assigns 32 bit integer, I think it is done to save memory.

1 Like

Thank You sir, but why is numpy assigning 32-bit integer value even after having 64-bit OS and 64-bit software version? Plz suggest . Does arrays have some kind of limitations?

Okay sir ,Thank you .
does it have any solution or anyway that numpy will assign 64-bit integer as due to 32-bit integer value it will get restrictions for higher value calculations?