Numpy: Slice an array

20 August 2020

Code

# Import library
import numpy as np

# Create an array
x = np.array([[1,2,3,4],
             [11,12,13,14],
             [21,22,23,24],
             [31,32,33,34]]
            )
print('x:\n',x)
print("Shape:", x.shape)
x:
 [[ 1  2  3  4]
 [11 12 13 14]
 [21 22 23 24]
 [31 32 33 34]]
Shape: (4, 4)

.

# Slice
y = x[0:2, 0:2]

# Output
print(y)
print('Shape:',y.shape)
[[ 1  2]
 [11 12]]
Shape: (2, 2)

.

# Slice
y = x[1:3,1:3]

# Output
print(y)
print('Shape:', y.shape)
[[12 13]
 [22 23]]
Shape: (2, 2)






Any errors in code above?
Please send a message.