📜  Python| numpy matrix.astype()

📅  最后修改于: 2022-05-13 01:54:40.427000             🧑  作者: Mango

Python| numpy matrix.astype()

借助Numpy matrix.astype()方法,我们可以转换矩阵的类型,但问题是如果我们想将 float 转换为 int 会丢失数据,那么一些数据会丢失。此方法有助于矩阵的类型转换

示例 #1:
在这个例子中,我们可以看到我们如何使用matrix.astype()方法将浮点型矩阵转换为 int 型矩阵。

# import the important module in python
import numpy as np
            
# make a matrix with numpy
gfg = np.matrix('[1.2, 2.8, 3.1, 4.5]')
            
# applying matrix.astype() method
geeks = gfg.astype(int)
      
print(geeks)
输出:
[[1 2 3 4]]

示例 #2:

# import the important module in python
import numpy as np
            
# make a matrix with numpy
gfg = np.matrix('[1.1, 2, 3.5; 4.2, 5.5, 6; 7, 8, 9.3]')
            
# applying matrix.astype() method
geeks = gfg.astype(int)
      
print(geeks)
输出:
[[1 2 3]
 [4 5 6]
 [7 8 9]]