Skip to main content
 首页 » 编程设计

python之Numpy之数组如何 reshape 它

2024年12月31日6arxive

我在数组中存储了类 [0,1] 是负值类,[1,0] 是正值类,现在我想 reshape 它。

我的输入数组是这样的

[[0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0], [0, 1], [1, 0]] 

我正在尝试像这样 reshape 它

ponlabel=np.array(ponlabel) 
PositiveorNegativeLabel = ponlabel.reshape(24, 1) 
print(PositiveorNegativeLabel) 

获取错误无法将大小 48 reshape 为 (24,1)

请您参考如下方法:

如果您只想提取第一个值,请使用:

ponlabel[:, 0] 
#array([1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]) 

同样适用于第二个值:

ponlabel[:, 1] 
#array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1])