gpt4 book ai didi

python - ValueError:操作数无法与形状 (5,) (30,) 一起广播

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:09 27 4
gpt4 key购买 nike

我正在尝试像这样合并数组:

如果:

a = [1.2, 1, 3, 4]
b = [0.0 , 0.0]
c = [0.0 , 0.0]
a = a + b + c

那么结果应该是:

[0.0 , 0.0 , 1.2 , 1 ,3 ,4 , 0.0 ,0.0]

我所做的是提取数组的直方图 并将其与普通数组合并。

x1, bins, patch = plt.hist(array1, bins = round(max(array1) - min(array1)))
x1 = b + x1 + c

but the form of x1 is
x1 = [ 2. 0. 0. 1. 0. 2. 5. 0. 1. 1. 0. 1. 5.]

也许这会导致这样的错误

ValueError: operands could not be broadcast together with shapes (5,) (30,)

请帮帮我。 我不知道该怎么办

最佳答案

您可以使用 np.concatenate 来执行此操作,但您也可以通过将数组转换为列表来执行此操作。

import numpy as np

a = list(np.array([1.2, 1, 3, 4]))
b = list(np.array([0.0 , 0.0]))
c = list(np.array([0.0 , 0.0]))
D= a + b + c

所以在你的代码中尝试:

x1 = list(b) + list(x1) + list(c)

#Put it back into a numpy array
x1 = np.array(x1)

关于python - ValueError:操作数无法与形状 (5,) (30,) 一起广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47257427/

27 4 0
文章推荐: python - 如何正确取消引用应该包含日语符号的网址
文章推荐: python - 使用 beautiful soup - 提取
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com