【问题标题】:Convert 2D table to three 1D arrays in Python在 Python 中将二维表转换为三个一维数组
【发布时间】:2023-03-10 02:27:01
【问题描述】:

我在n x m 2D 表中有这样的数据:

在我的 Python 代码中,它看起来像这样:

import numpy as np

xData = np.array([-225,    -200,   -175])
yData = np.array([0.1,     1.0,    5.0])
zData = np.array([[749.36, 698.96, 471.88],
                  [1012.1, 987.87, 890.69],
                  [1283.9, 1270.1, 1217.1]])

为了进行一些曲线拟合,我想以三个一维数组的形式进行拟合,每个数组的大小为1 x (n x m)

xData = np.array([-225,   -225,   -225,   -200,   -200,   -200,   -175,   -175,   -175])
yData = np.array([0.1,    1.0,    5.0,    0.1,    1.0,    5.0,    0.1,    1.0,    5.0])   
zData = np.array([749.36, 698.96, 471.88, 1012.1, 987.87, 890.69, 1283.9, 1270.1, 1217.1])

什么是实现这一目标的好方法? 请注意,一般情况下,xDatayData 的间距并不均匀。

【问题讨论】:

  • np.repeatnp.tile 是你的朋友。
  • 谢谢!这就是我需要的。我将根据您的解决方案添加答案。

标签: python arrays numpy reshape


【解决方案1】:

这应该可以解决问题:

xData = np.repeat(xData, np.shape(zData)[1])
yData = np.tile(yData, np.shape(zData)[0])
zData = np.reshape(zData, (1, np.size(zData)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-26
    • 2021-05-13
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多