Shear a numpy array

后端 未结 5 1666
执念已碎
执念已碎 2020-12-18 00:40

I\'d like to \'shear\' a numpy array. I\'m not sure I\'m using the term \'shear\' correctly; by shear, I mean something like:

Shift the first column by 0 places

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 01:13

    Here is a cleaned-up version of your own approach:

    def shear2(a, strength=1, shift_axis=0, increase_axis=1, edges='clip'):
        indices = numpy.indices(a.shape)
        indices[shift_axis] -= strength * indices[increase_axis]
        indices[shift_axis] %= a.shape[shift_axis]
        res = a[tuple(indices)]
        if edges == 'clip':
            res[indices[shift_axis] < 0] = 0
            res[indices[shift_axis] >= a.shape[shift_axis]] = 0
        return res
    

    The main difference is that it uses numpy.indices() instead of rolling your own version of this.

提交回复
热议问题