![](/img/trans.png)
[英]x and y must have same first dimension, but have shapes (50,) and (10,)
[英]ValueError: x and y must have same first dimension, but have shapes (50,) and (1, 50)/ Multiprocessing
我是多处理和绘图的新手,所以我希望编写一个可以多处理一系列高度的代码,以通过方程获得每个高度的最佳发射角度,然后是 plot 结果。
我的主要问题是在尝试 plot 结果时遇到错误。 我的次要问题是通过调查我的代码一段时间,现在我开始认为多进程是多余的? 我仍然希望使用多进程。 因此,对此的任何帮助也将不胜感激。
当我不希望 plot 结果时,该等式似乎工作正常。 但是当我尝试 plot 时,我得到“ValueError:x 和 y 必须具有相同的第一维,但具有形状 (50,) 和 (1, 50)”,我并不完全理解。 我以为他们都只是50的范围?
我已经搞砸了一段时间试图让它工作。 我相信它在“list_h = np.array(range(1))”行中,因为它看起来不正确。 但如果我增加它,这个范围似乎只是重复结果。 导致我认为我对这个多进程的结构很差。
我的代码如下
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()
任何帮助是极大的赞赏:)。
您快到了!! pool.map()
方法通过将f
应用于给定输入(在本例中为list_h
)以列表形式返回结果。 由于list_h
只有一项,因此结果将是一个只有一个值的列表。
因此,您需要做的就是从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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.