gpt4 book ai didi

python - 将列表的 numpy 数组展平为每行 4 个值

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:16 25 4
gpt4 key购买 nike

我创建了一个每行有 4 个值的数组

我的代码:

import numpy as np
import itertools as itertools
import timeit
aL = np.random.randint(0,100,size=750000) # 10 random ints
bL = np.random.randint(0,100,size=750000) # 10 random ints


xL = np.arange(1000)
yL = np.arange(750)

lenx = len(xL) # 5
leny = len(yL) # 2

arr = np.ndarray(shape=(leny, lenx, 4)) # create a 3-d array

for x in range(leny):
arr[x,:,0] = xL


for y in range(lenx):
arr[:,y,1] = yL

a_reshaped = aL.reshape(leny,lenx)
b_reshaped = bL.reshape(leny,lenx)

arr[:,:,2] = a_reshaped
arr[:,:,3] = b_reshaped

print(arr)

我的输出现在看起来像这样:

[[[  0.00000000e+00   0.00000000e+00   4.00000000e+01   2.30000000e+01]
[ 1.00000000e+00 0.00000000e+00 8.50000000e+01 1.40000000e+01]
[ 2.00000000e+00 0.00000000e+00 7.20000000e+01 2.00000000e+00]
...,
[ 1.44600000e+03 0.00000000e+00 9.20000000e+01 4.60000000e+01]
[ 1.44700000e+03 0.00000000e+00 5.00000000e+01 6.10000000e+01]
[ 1.44800000e+03 0.00000000e+00 8.40000000e+01 9.40000000e+01]]]

我想每行输出 4 个值。例如

1 0 2.5 3.5

我已经尝试过这个:

np.fromiter(itertools.chain.from_iterable(arr), dtype='int')

但这对我来说没有用。

我正在寻找的结果是这样的:

1   1   0.1   11
1 2 0.2 12
1 3 0.3 13
1 4 0.4 14
.....
1000 745 0.986 86
1000 746 0.987 97
1000 747 0.989 98
1000 748 0.990 99
1000 749 0.991 100
1000 750 0.992 110

编辑

假设我有一个包含 5 个 x 点和 3 个 y 点的列表,每个 x,y 对有 2 个随机数。我希望每个 x 值与每个 y 值配对,反之亦然。这意味着我将得到 15 行输出。问题是我有一个包含 1449 个 x 值和 638 个值的列表来完成工作并迭代其中的每个值需要几个小时。

有人向我提供了上面的代码,以便更快地完成这项工作,而且确实如此。现在我得到了带有列表列表的数组。我想将它们压平以获得与此类似的东西:

0.   0.    8.  12.
1. 0. 17. 8.
2. 0. 1. 18.
3. 0. 17. 12.
4. 0. 3. 12.
0. 1. 6. 13.
1. 1. 16. 14.
2. 1. 8. 0.
3. 1. 12. 8.
4. 1. 2. 12.
0. 2. 4. 8.
1. 2. 2. 3.
2. 2. 4. 3.
3. 2. 2. 3.
4. 2. 13. 13.

我希望这能更澄清。

编辑2

我的输出现在看起来像这样:

[[  0   0  48  69]
[ 0 1 37 12]
[ 0 2 15 30]
...,
[749 997 64 14]
[749 998 61 7]
[749 999 1 74]]

是否可以切换前两行,使上面的内容变成这样:

[[  0   0  48  69]
[ 1 0 37 12]
[ 2 0 15 30]
...,
[997 749 64 14]
[998 749 61 7]
[999 749 1 74]]

感谢您的帮助

最佳答案

您可以通过 numpy reshape 和广播轻松做到这一点

import numpy as np
import itertools as itertools
import timeit
aL = np.random.randint(0,100,size=750000) # 10 random ints
bL = np.random.randint(0,100,size=750000) # 10 random ints


xL = np.arange(1000)
yL = np.arange(750)


yL, xL = np.ix_(yL, xL) # this prepares x and y for broadcasting such
# that all possible combinations are generated

out = np.empty((750,1000,4), dtype=int)

out[..., 0] = yL # y is 750x1
out[..., 1] = xL # x is 1x1000
out.shape = -1, 4 # this flattens the array to shape 750,000x4
out[..., 2] = aL # so a
out[..., 3] = bL # and b can be inserted

print(out)

要交换列(请确保不要错过 .copy):

out[..., 1], out[...., 0] = out[..., 0], out[..., 1].copy()

要打印每一行,您可以尝试类似的操作

print('\n'.join([(2*"{: 8.0f}" + 2*"{: 12.04f}").format(*l) for l in out]))

关于python - 将列表的 numpy 数组展平为每行 4 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42566539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com