shift numpy array by row

霸气de小男生 提交于 2019-12-12 03:17:42

问题


Array:

arr = np.ones([4,4])

array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])

I use shift from scipy.ndimage.interpolation as follows:

shift(arr,1, cval=np.nan)

array([[ nan,  nan,  nan,  nan],
       [ nan,   1.,   1.,   1.],
       [ nan,   1.,   1.,   1.],
       [ nan,   1.,   1.,   1.]])

HOWEVER, I want:

array([[ nan,  nan,  nan,  nan],
       [ 1.,   1.,   1.,   1.],
       [ 1.,   1.,   1.,   1.],
       [ 1.,   1.,   1.,   1.]])

Basically, I want to SHIFT all columns data down the rows and boot the last row out of my data set. Pandas has the shift function that can do this but I'm not certain how it can be done in Numpy.


回答1:


You can change the shift parameter (second parameter) of the shift function from scipy.ndimage.interpolation as follows:

shift(arr, (1, 0), cval=np.nan)

Here, (1, 0) means a shift of 1 in the first dimension, and 0 in the second dimension.



来源:https://stackoverflow.com/questions/58616688/shift-numpy-array-by-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!