我正在尝试提取一列并排列成多行。 我的输入:数据
-2.74889,1.585,223.60
-2.74889,1.553,228.60
-2.74889,1.423,246.00
-2.74889,1.236,249.10
-2.74889,0.928,243.80
-2.74889,0.710,242.20
-2.74889,0.558,243.50
...
...
...
k = np.reshape(data[:,2], (2,10))
输出:
[[ 223.6 228.6 246. 249.1 243.8 242.2 243.5 244. 244.8
245.2 ]
[ 224.6 230. 250.7 249.3 244.4 242.1 242.8 243.8 244.7
245.1 ]]
我的问题是如何为每个数字添加方括号(例如223.6)并将其保持在一行中?
谢谢, 普拉萨德。
答案 0 :(得分:1)
您的意思还不清楚,但是也许是这样的吗?
>>> import numpy as np
>>> data = np.arange(30).reshape(10,3)
>>> data
array([[ 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]])
>>> data[:, 2, None]
array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17],
[20],
[23],
[26],
[29]])
答案 1 :(得分:0)
整形时需要扩展数组的尺寸。
设置
x = np.arange(60).reshape(20, 3)
reshape
(带有附加尺寸)
x[:, 2].reshape((-1, 10, 1))
expand_dims
和 axis=2
np.expand_dims(x[:, 2].reshape(-1, 10), axis=2)
atleast_3d
np.atleast_3d(x[:, 2].reshape(-1, 10))
所有三种产品:
array([[[ 2],
[ 5],
[ 8],
[11],
[14],
[17],
[20],
[23],
[26],
[29]],
[[32],
[35],
[38],
[41],
[44],
[47],
[50],
[53],
[56],
[59]]])