How to convert real numpy array to int numpy array? Tried using map directly to array but it did not work.
如何将真正的numpy数组转换为int numpy数组?尝试直接使用映射到数组,但它不起作用。
4 个解决方案
#1
251
Use the astype
method.
使用astype方法。
>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> x.astype(int)
array([[1, 2],
[1, 2]])
#2
51
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.
关于如何控制四舍五入的一些numpy函数:rint, floor,trunc, ceil。取决于你希望如何围绕浮动,向上,向下,或到最近的整数。
>>> x = np.array([[1.0,2.3],[1.3,2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> y = np.trunc(x)
>>> y
array([[ 1., 2.],
[ 1., 2.]])
>>> z = np.ceil(x)
>>> z
array([[ 1., 3.],
[ 2., 3.]])
>>> t = np.floor(x)
>>> t
array([[ 1., 2.],
[ 1., 2.]])
>>> a = np.rint(x)
>>> a
array([[ 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):
让其中一个进入int,或其他类型的numpy, astype(由BrenBern回答):
a.astype(int)
array([[1, 2],
[1, 3]])
>>> y.astype(int)
array([[1, 2],
[1, 2]])
#3
6
you can use np.int_
:
您可以使用np.int_:
>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> np.int_(x)
array([[1, 2],
[1, 2]])
#4
6
If you're not sure your input is going to be a Numpy array, you can use asarray
with dtype=int
instead of astype
:
如果您不确定输入是否为Numpy数组,则可以使用dtype=int而不是astype来使用asarray:
>>> np.asarray([1,2,3,4], dtype=int)
array([1, 2, 3, 4])
If the input array already has the correct dtype, asarray
avoids the array copy while astype
does not (unless you specify copy=False
):
如果输入数组已经有了正确的dtype,那么asarray将避免数组复制,而astype不会(除非您指定copy=False):
>>> a = np.array([1,2,3,4])
>>> a is np.asarray(a) # no copy :)
True
>>> a is a.astype(int) # copy :(
False
>>> a is a.astype(int, copy=False) # no copy :)
True
#1
251
Use the astype
method.
使用astype方法。
>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> x.astype(int)
array([[1, 2],
[1, 2]])
#2
51
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.
关于如何控制四舍五入的一些numpy函数:rint, floor,trunc, ceil。取决于你希望如何围绕浮动,向上,向下,或到最近的整数。
>>> x = np.array([[1.0,2.3],[1.3,2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> y = np.trunc(x)
>>> y
array([[ 1., 2.],
[ 1., 2.]])
>>> z = np.ceil(x)
>>> z
array([[ 1., 3.],
[ 2., 3.]])
>>> t = np.floor(x)
>>> t
array([[ 1., 2.],
[ 1., 2.]])
>>> a = np.rint(x)
>>> a
array([[ 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):
让其中一个进入int,或其他类型的numpy, astype(由BrenBern回答):
a.astype(int)
array([[1, 2],
[1, 3]])
>>> y.astype(int)
array([[1, 2],
[1, 2]])
#3
6
you can use np.int_
:
您可以使用np.int_:
>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> np.int_(x)
array([[1, 2],
[1, 2]])
#4
6
If you're not sure your input is going to be a Numpy array, you can use asarray
with dtype=int
instead of astype
:
如果您不确定输入是否为Numpy数组,则可以使用dtype=int而不是astype来使用asarray:
>>> np.asarray([1,2,3,4], dtype=int)
array([1, 2, 3, 4])
If the input array already has the correct dtype, asarray
avoids the array copy while astype
does not (unless you specify copy=False
):
如果输入数组已经有了正确的dtype,那么asarray将避免数组复制,而astype不会(除非您指定copy=False):
>>> a = np.array([1,2,3,4])
>>> a is np.asarray(a) # no copy :)
True
>>> a is a.astype(int) # copy :(
False
>>> a is a.astype(int, copy=False) # no copy :)
True