简体   繁体   English

ValueError: x 和 y 必须具有相同的第一维,但具有形状 (50,) 和 (1, 50)/ 多处理

[英]ValueError: x and y must have same first dimension, but have shapes (50,) and (1, 50)/ Multiprocessing

I am new to multiprocessing and plotting so I wished to make a code that could multi-process a range of heights to get the optimum launch angle for each height through an equation, then plot the results.我是多处理和绘图的新手,所以我希望编写一个可以多处理一系列高度的代码,以通过方程获得每个高度的最佳发射角度,然后是 plot 结果。

My main issue is when attempting to plot the results I run into an error.我的主要问题是在尝试 plot 结果时遇到错误。 My secondary issue is by investigating my code for a while, now I'm beginning to think the multi-process is redundant?我的次要问题是通过调查我的代码一段时间,现在我开始认为多进程是多余的? I still desire to use a multi-process.我仍然希望使用多进程。 So any help in regards to that is also appreciated.因此,对此的任何帮助也将不胜感激。

The equation seems to work fine when I don't wish to plot the results.当我不希望 plot 结果时,该等式似乎工作正常。 But when I attempt to plot them I get "ValueError: x and y must have same first dimension, but have shapes (50,) and (1, 50)" which I don't exactly understand.但是当我尝试 plot 时,我得到“ValueError:x 和 y 必须具有相同的第一维,但具有形状 (50,) 和 (1, 50)”,我并不完全理解。 I thought they were both just ranges to 50?我以为他们都只是50的范围?

I've been messing around with it for a while attempting to get it to work.我已经搞砸了一段时间试图让它工作。 I believe its in the "list_h = np.array(range(1))" line since it just doesn't seem right.我相信它在“list_h = np.array(range(1))”行中,因为它看起来不正确。 But that range seems to just repeat the results if I increase it.但如果我增加它,这个范围似乎只是重复结果。 leading me to think I've structured this multi-process poorly.导致我认为我对这个多进程的结构很差。

My code is as follows我的代码如下

import numpy as np
from multiprocessing import Pool
import matplotlib.pyplot as plt

def f(x):
    speed = 5
    ran0 = (speed*speed)/9.8
    array_hght = np.array(range(50))
    return ((np.arccos(array_hght/(array_hght+ran0)))/2)*(180/np.pi)

if __name__ == '__main__':
    with Pool(5) as p:
        list_h = np.array(range(1))
        list_angles = p.map(f, list_h)
        print(list_angles)
        array_h = np.array(range(50))
        plt.plot(array_h, list_angles)
        plt.show()

Any help is greatly appreciated:).任何帮助是极大的赞赏:)。

You're almost there!!您快到了!! The pool.map() method returns the result in a list-form by applying f over the given input which is list_h in this case. pool.map()方法通过将f应用于给定输入(在本例中为list_h )以列表形式返回结果。 Since list_h has only one item, then the result will be a list of just one value.由于list_h只有一项,因此结果将是一个只有一个值的列表。

So, all you need to do is to get the first element from list_angles like so:因此,您需要做的就是从list_angles中获取第一个元素,如下所示:

if __name__ == '__main__':
    with Pool(5) as p:
        list_h = np.array(range(1))
        list_angles = p.map(f, list_h)[0]    #<--- HERE
        array_h = np.array(range(50))
        print(array_h.shape)
        plt.plot(array_h, list_angles)
        plt.show()

And running the previous code will results in the following graph:运行前面的代码会得到下图: 在此处输入图像描述

暂无
暂无

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

相关问题 x 和 y 必须具有相同的第一维,但具有形状 (50,) 和 (10,) - x and y must have same first dimension, but have shapes (50,) and (10,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5) - Linear regression model shapes - ValueError: x and y must have same first dimension, but have shapes (5,) and (1, 5) ValueError:x和y必须具有相同的第一尺寸,但形状为(4200,)和(16800,1) - ValueError: x and y must have same first dimension, but have shapes (4200,) and (16800, 1) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (41,) 和 (1, 41) - ValueError: x and y must have same first dimension, but have shapes (41,) and (1, 41) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (101,) 和 (100,) - ValueError: x and y must have same first dimension, but have shapes (101,) and (100,) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (1,) 和 (224, 224, 3) - ValueError: x and y must have same first dimension, but have shapes (1,) and (224, 224, 3) 谁能帮我? ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10,) 和 (0,) - Can anyone help me? ValueError: x and y must have same first dimension, but have shapes (10,) and (0,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM