简体   繁体   English

ValueError: 操作数无法与形状 (7,) (6,) (7,) 一起广播

[英]ValueError: operands could not be broadcast together with shapes (7,) (6,) (7,)

I'm getting an error message trying to run a script in Python for repeated irradiation-cooling times of gold foil.我收到一条错误消息,试图在 Python 中运行脚本以重复进行金箔的辐照冷却时间。 This is the error:这是错误:

Traceback (most recent call last):
  File "C:/Users/wolfk/PycharmProjects/Reactor Phys In-House/iterations.py", line 26, in <module>
    cr_tot /= math.e ** (-2.98256E-6 * out_times[l])
ValueError: operands could not be broadcast together with shapes (7,) (6,) (7,) 

The code is pretty messy since I used number values instead of variables for the most part, but the math isn't important, I can verify it later.代码非常混乱,因为我大部分使用数字值而不是变量,但数学并不重要,我可以稍后验证。 What I need to do is get this thing to run!我需要做的是让这个东西运行起来! So you know, the vectors for in- and out-times are different lengths by design, and I tried making them the same length so that wasn't the issue.所以你知道,输入和输出时间的向量在设计上是不同的长度,我试着让它们的长度相同,所以这不是问题。 Here's the code:这是代码:

import numpy as np
import math
import matplotlib.pyplot as plt

in_times = np.array([[697800, 603840, 74460, 161520, 428880, 75960, 62760]])
out_times = np.array([[248880, 3960, 9240, 5400, 5880, 8760]])

cr_tot = 0
cr_fast = 0
i = 1
j = 1
k = 0
l = 0

while i < 13:
    if j == 1:
        cr_tot /= math.e ** (-2.98256E-6 * in_times[k])
        cr_fast /= math.e ** (-2.98256E-6 * in_times[k])

        cr_tot += ((2.98256E-6 * 4950377) / 0.021554) / (1 - math.e ** (-2.98256E-6 * in_times[k]))
        cr_fast += ((2.98256E-6 * 2141114) / 0.043109) / (1 - math.e ** (-2.98256E-6 * in_times[k]))

        j -= 1
        k += 1
    else:
        cr_tot /= math.e ** (-2.98256E-6 * out_times[l])
        cr_fast /= math.e ** (-2.98256E-6 * out_times[l])

        j += 1
        l += 1

    i += 1

xi_tot = (2.98256E-6 * 4950377) / (0.021554 * cr_tot)
xi_fast = (2.98256E-6 * 2141114) / (0.043109 * cr_fast)

cr_tot *= (2.98256E-6 * 4950377) / (0.021554 * xi_tot * math.e ** (-2.98256E-6 * 660) * (1 - math.e ** (-2.98256E-6 * 1000)))
cr_fast *= (2.98256E-6 * 2141114) / (0.043109 * xi_fast * math.e ** (-2.98256E-6 * 660) * (1 - math.e ** (-2.98256E-6 * 2000)))

print(cr_tot)
print(cr_fast)

I'd appreciate any help I can get.我很感激我能得到的任何帮助。 :) :)

Removing the double brackets from your initial numpy arrays actually helps.从初始 numpy 数组中删除双括号实际上有帮助。

...
in_times = np.array([697800, 603840, 74460, 161520, 428880, 75960, 62760])
out_times = np.array([248880, 3960, 9240, 5400, 5880, 8760])
...

Output:输出:

0.476018036910266
0.9882585377315589
0.9728174262826453
0.9840231812760558
0.9826154332766018
0.9742111371275044
7480153046556.081
175166298775.0072

I do not understand your logic.我不明白你的逻辑。 In your code, at beginning, k=0 and in_times[k]in if branch will return array([697800, 603840, 74460, 161520, 428880, 75960, 62760]).在您的代码中,一开始,k=0 和 in_times[k]in if 分支将返回数组([697800, 603840, 74460, 161520, 428880, 75960, 62760])。 Thus, length of cr_tot is 7. And then j -= 1, j equals 0. Next iteration goes to else branch.因此,cr_tot 的长度是 7。然后 j -= 1,j 等于 0。下一次迭代转到 else 分支。 out_times[l] means out_times[0] and it returns array([248880, 3960, 9240, 5400, 5880, 8760]) with length 6. But your code is cr_tot /= math.e ** (-2.98256E-6 * out_times[l]), dividing cr_tot(length is 6 return from if branch) using array with length 7. That is reason you got the error. out_times[l] 表示 out_times[0] 并返回长度为 6 的 array([248880, 3960, 9240, 5400, 5880, 8760])。但是你的代码是 cr_tot /= math.e ** (-2.98256E-6 * out_times[l]),使用长度为 7 的数组除 cr_tot(length is 6 return from if branch)。这就是你得到错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas:ValueError - 操作数无法与形状一起广播 - Pandas: ValueError - operands could not be broadcast together with shapes Python ValueError:操作数无法与形状一起广播 - Python ValueError: operands could not be broadcast together with shapes ValueError: 操作数无法与形状一起广播 (7410,) (3,) - ValueError: operands could not be broadcast together with shapes (7410,) (3,) ValueError:操作数不能与形状(5,)(30,)一起广播 - ValueError: operands could not be broadcast together with shapes (5,) (30,) QuantileRegression ValueError:操作数无法与形状一起广播 - QuantileRegression ValueError: operands could not be broadcast together with shapes ValueError:操作数无法与形状一起广播 (3,5) (3,) - ValueError: operands could not be broadcast together with shapes (3,5) (3,) Python - ValueError:操作数无法与形状一起广播 - Python - ValueError: operands could not be broadcast together with shapes ValueError: 操作数无法与形状 (3,) (3000,) 一起广播 - ValueError: operands could not be broadcast together with shapes (3,) (3000,) ValueError:操作数不能与形状一起广播 - ValueError: operands could not be broadcast together with shapes ValueError:操作数不能与形状(3,)(100,)一起广播 - ValueError: operands could not be broadcast together with shapes (3,) (100,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM