在 Python 中重塑数据集
Reshaping dataset in Python
我有一个数据集:
[[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]] ... ]]]
我试图通过这个循环 reshape/slice:
x = np.ndarray
for i in range(0,len(X)):
a = X[i:i+5]
x = np.concatenate((a,x))
但是我遇到了错误。
使用来自 的 as_strided
食谱 window_nd
:
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)
steps
和 axis
参数在这种情况下在技术上不是必需的,但为了清楚起见包含在内。
我有一个数据集:
[[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]] ... ]]]
我试图通过这个循环 reshape/slice:
x = np.ndarray
for i in range(0,len(X)):
a = X[i:i+5]
x = np.concatenate((a,x))
但是我遇到了错误。
使用来自 as_strided
食谱 window_nd
:
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)
steps
和 axis
参数在这种情况下在技术上不是必需的,但为了清楚起见包含在内。