【问题标题】:Reshaping dataset in Python在 Python 中重塑数据集
【发布时间】:2018-11-21 16:33:41
【问题描述】:

我有一个数据集:

[[0.08007146 1.         0.96428571 0.02050692 0.        ]
 [0.01779764 0.85714286 0.85714286 0.0176427  0.        ]
 [0.02669778 0.64285714 0.5        0.03108454 1.        ]
 ...
 [0.01552716 0.45454545 1.         0.01019869 0.        ]
 [0.00931678 1.         0.25       0.0136772  1.        ]
 [0.03105702 0.83333333 1.         0.02045807 0.33333333]]

每当我将它重塑为 (5,5) 时,我都会得到预期的结果:

[[[0.08007146 1.         0.96428571 0.02050692 0.        ]
  [0.01779764 0.85714286 0.85714286 0.0176427  0.        ]
  [0.02669778 0.64285714 0.5        0.03108454 1.        ]
  [0.02966641 0.83333333 1.         0.01141099 0.33333333]
  [0.00889919 0.5        1.         0.00837062 1.        ]]

 [[0.01483161 0.83333333 1.         0.00847276 0.33333333]
  [0.0148321  0.83333333 0.83333333 0.01239681 0.33333333]
  [0.00593259 0.66666667 1.         0.00833658 0.33333333]
  [0.00296632 1.         0.16666667 0.00900119 0.        ]
  [0.04449483 1.         0.9375     0.00967617 1.        ]]

 [[0.04450035 0.9375     1.         0.01646444 0.33333333]
  [0.04449446 0.88235294 0.9375     0.01299926 1.        ]
  [0.05042079 0.73913043 0.94444444 0.02087993 1.        ]
  [0.10085577 0.97142857 1.         0.02407424 1.        ]
  [0.00296554 1.         1.         0.00803905 1.        ]]

如何将其重塑为在第二个序列中包含第一个序列的第二个元素?我的意思是这样的:

[[[0,1,2,3,4][1,2,3,4,5][2,3,4,5,6][3,4,5,6,7][ 4,5,6,7,8]] ... ]]]

我试图通过这个循环重塑/切片:

x = np.ndarray

for i in range(0,len(X)):
    a = X[i:i+5]
    x = np.concatenate((a,x))

但我遇到了错误。

【问题讨论】:

  • 您需要写入结果数组,还是仅根据它进行计算?您可以使用as_strided 轻松创建视图
  • 另外,请提供minimal reproducible example,您现在所拥有的实际上并不起作用。 “重塑”不会那样拆分数组。

标签: python numpy dataset reshape numpy-ndarray


【解决方案1】:

使用我的as_strided 配方window_nd 来自here

input = np.random.rand(15, 5)
current_output = input.reshape(-1, 5, 5)  #I think?
expected_output = window_nd(input, 5, steps = 1, axis = 0)

stepsaxis 参数在这种情况下在技术上不是必需的,但为了清楚起见,将其包含在内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    相关资源
    最近更新 更多