numpy array 转换格式
以数组名叫target为例,现在的格式是 {ndarray:(40,1)}, 想转换成 {ndarray:(40, )}, 如何操作。
import numpy as np
target = np.ones((40, 1))
target = target.ravel()
print(target.shape) # (40, )
可以试一试这个代码,应该可以满足你的要求。希望采纳。
表头 | 表头 |
---|---|
单元格 | 单元格 |
单元格 | 单元格 |
import numpy as np
target = np.array(range(40))
print(target, target.shape)
target1 = np.reshape(target, (40, 1))
print(target1, target1.shape)
tt = np.reshape(target1, (40, ))
print(tt, tt.shape)
--result
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39] (40,)
[[ 0]
[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]
[11]
[12]
[13]
[14]
[15]
[16]
[17]
[18]
[19]
[20]
[21]
[22]
[23]
[24]
[25]
[26]
[27]
[28]
[29]
[30]
[31]
[32]
[33]
[34]
[35]
[36]
[37]
[38]
[39]] (40, 1)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39] (40,)
x = np.ones((40,1))
x.reshape((40,))
介绍一种比较通用的方法,主要是把多维转换成一维,均可以使用.reshape(-1),具体示例如下:
import numpy as np
arr = np.random.randint(low=0, high=2, size=(40, 1))
arr = arr.reshape(-1)
print(arr.shape)