我有一个数组 (219812,2)
但我需要拆分为 2 (219812)
。
我一直收到错误 ValueError: operands could not be broadcast together with shapes (219812,2) (219812)
我怎样才能完成?
如您所见,我需要从 u = odeint 中提取两个单独的解决方案并将它们相乘。
def deriv(u, t):
return array([ u[1], u[0] - np.sqrt(u[0]) ])
time = np.arange(0.01, 7 * np.pi, 0.0001)
uinit = array([ 1.49907, 0])
u = odeint(deriv, uinit, time)
x = 1 / u * np.cos(time)
y = 1 / u * np.sin(time)
plot(x, y)
plt.show()
最佳答案
要提取二维数组的第 i 列,请使用 arr[:, i]
。
您还可以使用 u1, u2 = u.T
.
顺便说一句,star imports 不是很好(除了可能在终端中用于交互使用),所以我添加了几个 np.
和 plt.
到你的代码,变成:
def deriv(u, t):
return np.array([ u[1], u[0] - np.sqrt(u[0]) ])
time = np.arange(0.01, 7 * np.pi, 0.0001)
uinit = np.array([ 1.49907, 0])
u = odeint(deriv, uinit, time)
x = 1 / u[:, 0] * np.cos(time)
y = 1 / u[:, 1] * np.sin(time)
plt.plot(x, y)
plt.show()
看起来对数图看起来也更好。
关于python - 在 Python 中拆分数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15953337/