what is difference between numpy.array([[1., 2], [3, 4], [5, 6]]) and numpy.array([[1, 2], [3, 4], [5, 6]]). I came across one code using two different type of declaration but could not find its meaning.
2 Answers
1. is a floating point number, 1 is an integer. In the case of numpy, this seems to affect the entire array, i.e. if one number is a floating point number, all the numbers are converted accordingly.
In [3]: numpy.array([[1., 2], [3, 4], [5, 6]])
Out[3]:
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
In [4]: numpy.array([[1, 2], [3, 4], [5, 6]])
Out[4]:
array([[1, 2],
[3, 4],
[5, 6]])
In the first case, all the numbers in the array get the type numpy.float64, while in the latter case they get numpy.int64.
1 Comment
uhoh
NumPy arrays are designed and optimized around the idea that every element has the same number of bytes in memory. NumPy detects that one element is a float and upcasts the rest of the values accordingly when it allocates memory.