如何将 float 类型的 numpy 数组转换为 int 类型的 numpy 数组?
How can I convert a numpy array of float type into a numpy array of int type?
我有以下源代码:
npW_x = np.array(my_np_array)
npW_round_111 = np.around(npW_x, decimals=0)
sum_x_111 = np.sum(npW_round_111, axis=1)
np.savetxt("file1.txt", sum_x_111)
文件输出
3.200000000000000000e+01
5.500000000000000000e+01
3.300000000000000000e+01
4.900000000000000000e+01
5.200000000000000000e+01
5.500000000000000000e+01
3.800000000000000000e+01
5.200000000000000000e+01
5.100000000000000000e+01
3.100000000000000000e+01
3.100000000000000000e+01
3.200000000000000000e+01
5.100000000000000000e+01
... ... ... ... ... ...
预期输出如下:
3
6
3
5
5
6
4
5
5
3
3
3
5
... ... ... ... ... ...
我该怎么做?
您可以为此使用 astype()
函数
我正在考虑 npW_x
是需要转换为 int 数组的数组。
npW_x = np.array(my_np_array)
npW_x = npW_x.astype('int32')
现在,它应该被转换成一个整数 numpy 数组。
编辑:
我已经添加了完整的代码,你可以试试
npW_x = np.array(my_np_array)
npW_x = npW_x.astype('int32')
sum_x_111 = np.sum(npW_x, axis=1)
np.savetxt("file1.txt", sum_x_111)
希望它能解决您的问题。
arr = np.asarray([3.200000000000000000e+01,
5.500000000000000000e+01,
3.300000000000000000e+01,
4.900000000000000000e+01,
5.200000000000000000e+01,
5.500000000000000000e+01,
3.800000000000000000e+01,
5.200000000000000000e+01,
5.100000000000000000e+01,
3.100000000000000000e+01,
3.100000000000000000e+01,
3.200000000000000000e+01,
5.100000000000000000e+01])
对
array([32. , 55. , 33. , 49. , 52. , 55. , 38. , 52. , 51. , 31. , 31. ,
32. , 5.1])
np.around(np.where(arr//10, arr/10, arr)).astype('int')
得到
array([3, 6, 3, 5, 5, 6, 4, 5, 5, 3, 3, 3, 5])
In [385]: npW_x = np.random.rand(4,5)*10
...: npW_round_111 = np.around(npW_x, decimals=0)
...: sum_x_111 = np.sum(npW_round_111, axis=1)
...:
In [386]: sum_x_111
Out[386]: array([38., 25., 24., 30.])
使用默认值 fmt
保存(反复阅读 np.savetxt
文档!)
In [387]: np.savetxt('test',sum_x_111)
In [388]: cat test
3.800000000000000000e+01
2.500000000000000000e+01
2.400000000000000000e+01
3.000000000000000000e+01
以整数格式保存:
In [389]: np.savetxt('test',sum_x_111, fmt='%10d')
In [390]: cat test
38
25
24
30
在内存中转换为 int:
In [391]: In [391]: sum_x_111.astype(int)
Out[391]: array([38, 25, 24, 30])
我有以下源代码:
npW_x = np.array(my_np_array)
npW_round_111 = np.around(npW_x, decimals=0)
sum_x_111 = np.sum(npW_round_111, axis=1)
np.savetxt("file1.txt", sum_x_111)
文件输出
3.200000000000000000e+01
5.500000000000000000e+01
3.300000000000000000e+01
4.900000000000000000e+01
5.200000000000000000e+01
5.500000000000000000e+01
3.800000000000000000e+01
5.200000000000000000e+01
5.100000000000000000e+01
3.100000000000000000e+01
3.100000000000000000e+01
3.200000000000000000e+01
5.100000000000000000e+01
... ... ... ... ... ...
预期输出如下:
3
6
3
5
5
6
4
5
5
3
3
3
5
... ... ... ... ... ...
我该怎么做?
您可以为此使用 astype()
函数
我正在考虑 npW_x
是需要转换为 int 数组的数组。
npW_x = np.array(my_np_array)
npW_x = npW_x.astype('int32')
现在,它应该被转换成一个整数 numpy 数组。
编辑:
我已经添加了完整的代码,你可以试试
npW_x = np.array(my_np_array)
npW_x = npW_x.astype('int32')
sum_x_111 = np.sum(npW_x, axis=1)
np.savetxt("file1.txt", sum_x_111)
希望它能解决您的问题。
arr = np.asarray([3.200000000000000000e+01,
5.500000000000000000e+01,
3.300000000000000000e+01,
4.900000000000000000e+01,
5.200000000000000000e+01,
5.500000000000000000e+01,
3.800000000000000000e+01,
5.200000000000000000e+01,
5.100000000000000000e+01,
3.100000000000000000e+01,
3.100000000000000000e+01,
3.200000000000000000e+01,
5.100000000000000000e+01])
对
array([32. , 55. , 33. , 49. , 52. , 55. , 38. , 52. , 51. , 31. , 31. ,
32. , 5.1])
np.around(np.where(arr//10, arr/10, arr)).astype('int')
得到
array([3, 6, 3, 5, 5, 6, 4, 5, 5, 3, 3, 3, 5])
In [385]: npW_x = np.random.rand(4,5)*10
...: npW_round_111 = np.around(npW_x, decimals=0)
...: sum_x_111 = np.sum(npW_round_111, axis=1)
...:
In [386]: sum_x_111
Out[386]: array([38., 25., 24., 30.])
使用默认值 fmt
保存(反复阅读 np.savetxt
文档!)
In [387]: np.savetxt('test',sum_x_111)
In [388]: cat test
3.800000000000000000e+01
2.500000000000000000e+01
2.400000000000000000e+01
3.000000000000000000e+01
以整数格式保存:
In [389]: np.savetxt('test',sum_x_111, fmt='%10d')
In [390]: cat test
38
25
24
30
在内存中转换为 int:
In [391]: In [391]: sum_x_111.astype(int)
Out[391]: array([38, 25, 24, 30])