How to convert 2D float numpy array to 2D int numpy array? How to convert 2D float numpy array to 2D int numpy array? python python

# How to convert 2D float numpy array to 2D int numpy array?

Use the `astype` method.

``>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])>>> xarray([[ 1. ,  2.3],       [ 1.3,  2.9]])>>> x.astype(int)array([[1, 2],       [1, 2]])``

Some numpy functions for how to control the rounding: rint, floor,trunc, ceil. depending how u wish to round the floats, up, down, or to the nearest int.

``>>> x = np.array([[1.0,2.3],[1.3,2.9]])>>> xarray([[ 1. ,  2.3],       [ 1.3,  2.9]])>>> y = np.trunc(x)>>> yarray([[ 1.,  2.],       [ 1.,  2.]])>>> z = np.ceil(x)>>> zarray([[ 1.,  3.],       [ 2.,  3.]])>>> t = np.floor(x)>>> tarray([[ 1.,  2.],       [ 1.,  2.]])>>> a = np.rint(x)>>> aarray([[ 1.,  2.],       [ 1.,  3.]])``

To make one of this in to int, or one of the other types in numpy, astype (as answered by BrenBern):

``a.astype(int)array([[1, 2],       [1, 3]])>>> y.astype(int)array([[1, 2],       [1, 2]])``

you can use `np.int_`:

``>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])>>> xarray([[ 1. ,  2.3],       [ 1.3,  2.9]])>>> np.int_(x)array([[1, 2],       [1, 2]])``