python - 在 Python 中拆分数组

标签 python arrays numpy

我有一个数组 (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/

相关文章:

python - 如何在检测到我的蓝牙设备时播放声音

Python 多处理(joblib)参数传递的最佳方式

python - 使用索引数组返回 'ndarray' 的值

python - 如何使用 OpenCV 在 Plone 站点中显示网络摄像头捕获?

python - 理解 Python 属性的工作

python - 将 n 列重新堆叠为 3 列 Python

javascript - 带有嵌套数组的微模板

java - 数组中的奇怪问题

arrays - 在vb.net中将base64解码为图像

python-3.x - 如何进行线性拟合,其中我的变量 X 是 3d 向量?