Shear a numpy array

后端 未结 5 1665
执念已碎
执念已碎 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:26

    The approach in tom10's answer can be extended to arbitrary dimensions:

    def shear3(a, strength=1, shift_axis=0, increase_axis=1):
        if shift_axis > increase_axis:
            shift_axis -= 1
        res = numpy.empty_like(a)
        index = numpy.index_exp[:] * increase_axis
        roll = numpy.roll
        for i in range(0, a.shape[increase_axis]):
            index_i = index + (i,)
            res[index_i] = roll(a[index_i], -i * strength, shift_axis)
        return res
    

提交回复
热议问题