Wrap a slice around the edges of a two-dimensional array in numpy

Suppose I work with numpy in Python, and I have a two-dimensional array of arbitrary size. For convenience, suppose I have a 5 x 5 array. The specific numbers are not particularly important for my question; this is just an example.

a = numpy.arrange(25).reshape(5,5)

This gives:

[[0, 1, 2, 3, 4 ],
 [5, 6, 7, 8, 9 ],
 [10,11,12,13,14],
 [15,16,17,18,19],
 [20,21,22,23,24]]

Now, let's say I wanted to take a two-dimensional slice of this array. Under normal conditions, that would be easy. To get cells directly adjacent to 2.2, I would simply use a[1:4,1,4]that would give the expected

[[6, 7,   8 ],
 [11, 12, 13],
 [16, 17, 18]]

But what if I want to take a fragment that wraps around the edges of an array? For example, it a[-1:2,-1:2]will give:

[24, 20, 21],
[4, 0,  1 ],
[9, 5,  6 ] 

, , , . , , , , .

, : qaru.site/questions/378386/..., , , .

, : 2D- numpy, ?

, .

+4
5

numpy >= 1.7.

a = np.arange(25).reshape(5,5)

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

'wrap'...

b = np.pad(a, 1, mode='wrap')

array([[24, 20, 21, 22, 23, 24, 20],
       [ 4,  0,  1,  2,  3,  4,  0],
       [ 9,  5,  6,  7,  8,  9,  5],
       [14, 10, 11, 12, 13, 14, 10],
       [19, 15, 16, 17, 18, 19, 15],
       [24, 20, 21, 22, 23, 24, 20],
       [ 4,  0,  1,  2,  3,  4,  0]])

1 , b.

+10

, ndarray.take. , :

a.take(range(-1,2),mode='wrap', axis=0).take(range(-1,2),mode='wrap',axis=1)

[[24 20 21]
 [4  0   1]
 [9  5  6]]

, , . , .

, take, , - , 2D- , , - .

+7

roll, , :

b = np.roll(np.roll(a, 1, axis=0), 1, axis=1)[:3,:3]

array([[24, 20, 21],
       [ 4,  0,  1],
       [ 9,  5,  6]])
+1

, , . " " meshgrid:

A = arange(25).reshape((5,5)) # destinatoin matrix
print 'A:\n',A

k =-1* np.arange(9).reshape(3,3)# test kernel, all negative
print 'Kernel:\n', k
ix,iy = np.meshgrid(arange(3),arange(3)) # create x and y basis indices

pos = (0,-1) # insertion position

# create insertion indices
x = (ix+pos[0]) % A.shape[0]
y = (iy+pos[1]) % A.shape[1]
A[x,y] = k # set values
print 'Result:\n',A

:

A:
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
Kernel:
[[ 0 -1 -2]
 [-3 -4 -5]
 [-6 -7 -8]]
Result:
[[-3 -6  2  3  0]
 [-4 -7  7  8 -1]
 [-5 -8 12 13 -2]
 [15 16 17 18 19]
 [20 21 22 23 24]]
+1

, numpy ?

# First some setup
import numpy as np
A = np.arange(25).reshape((5, 5))
m, n = A.shape

A[np.arange(i-1, i+2)%m].reshape((3, -1))[:,np.arange(j-1, j+2)%n]

, . . ,

A.flat[np.array([np.arange(j-1,j+2)%n+a*n for a in xrange(i-1, i+2)]).ravel()].reshape((3,3))

To assign this, I would have to avoid the call to change the form and work directly with the flattened version returned by fancy indexing. Here is an example:

n = 7
A = np.zeros((n, n))
for i in xrange(n-2, 0, -1):
    A.flat[np.array([np.arange(i-1,i+2)%n+a*n for a in xrange(i-1, i+2)]).ravel()] = i+1
print A

which returns

[[ 2.  2.  2.  0.  0.  0.  0.]
 [ 2.  2.  2.  3.  0.  0.  0.]
 [ 2.  2.  2.  3.  4.  0.  0.]
 [ 0.  3.  3.  3.  4.  5.  0.]
 [ 0.  0.  4.  4.  4.  5.  6.]
 [ 0.  0.  0.  5.  5.  5.  6.]
 [ 0.  0.  0.  0.  6.  6.  6.]]
0
source

Source: https://habr.com/ru/post/1523991/


All Articles