1

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.

0

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
2

It has nothing to do with the array. 1. means 1.0. 1. is a float, 1 is an int.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.