本文将为您介绍NumPy中最常用的5种数组转换方法。
import numpy as np
arr = np.array([1, 2, 3])
float_arr = arr.astype(np.float64)
import numpy as np
arr = np.arange(9)
arr_2d = arr.reshape((3, 3))
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
arr_transpose = np.transpose(arr)
# 或者使用.T属性
arr_transpose = arr.T
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
arr_flat = arr.flatten()
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
arr_ravel = arr.ravel()
import numpy as np
arr = np.array([[[1], [2], [3]]])
arr_squeeze = np.squeeze(arr)
除了上文提到的常用的转换方法外,NumPy中还有其他一些不常用的转换方法:
本文链接:http://task.lmcjl.com/news/4357.html