我是使用 numpy 的新手,我想做一個簡單的任務,但我不知道該怎么做。
這是我的 numpy 陣列
values_points = np.arange(1,15)
但我想像這樣重塑它:
[1,2,3,4,5]
[2,3,4,5,6]
[3,4,5,6,7]
[4,5,6,7,8]
[5,6,7,8,9]
[6,7,8,9,10]
[7,8,9,10,11]
[8,9,10,11,12]
[9,10,11,12,13]
[10,11,12,13,14]
所以它將從前面的方括號中取出第二個數字,然后再取接下來的 4 個數字。我怎么能那樣做?
uj5u.com熱心網友回復:
如果我理解正確,這里可以使用一個大步技巧:
>>> values_points = np.arange(1,15)
>>> from numpy.lib.stride_tricks import as_strided
>>> as_strided(values_points, shape=(10, 5), strides=values_points.strides*2)
array([[ 1, 2, 3, 4, 5],
[ 2, 3, 4, 5, 6],
[ 3, 4, 5, 6, 7],
[ 4, 5, 6, 7, 8],
[ 5, 6, 7, 8, 9],
[ 6, 7, 8, 9, 10],
[ 7, 8, 9, 10, 11],
[ 8, 9, 10, 11, 12],
[ 9, 10, 11, 12, 13],
[10, 11, 12, 13, 14]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516063.html
標籤:Python麻木的
