Learn Python

Learn Data Structure & Algorithm

Numpy Introduction

Numpy Array

Numpy Attributes

Numpy generate random numbers

Numpy Slicing

Numpy Concatenate & split

Numpy Insert & delete

Numpy Math

Numpy Condition

Numpy Reshaping

Learn Pandas

Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

How to perform array slicing in numpy?

Slicing means get a specific part of an array. Here we will see how to slice 1D, 2D and 3D array. To slice the array we will use the index number

1D array slicing

Example:

#Get a single value using index number
Input
import numpy as np
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[0]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
1

#Get a single value using index number from the end
Example 2:
Input
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[-2]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
16

#Get a set of element using index number
Example:3
Input
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[0:7]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
[1 2 3 4 5 6 7]
Example:4
Input
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[0:15:2]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
[1 3 5 7 9 12 14 16]

Example:5
Input
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[:-5]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
[1 2 3 4 5 6 7 8 9 10 11 12]

Example:6
Input
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[7:]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
[ 8 9 10 11 12 13 14 15 16 17]

2D array slicing

If we see a 2D array then we will see that, in a 2D array contains some 1D array. So using slicing we can get the 1D array present in the 2D array and also get specific elements present in the 1D array of the 2D array.

In the examples, first third bracket is to get the 1D array present in the 2D array and the second third bracket second third bracket is to get the 1D array element.

Example:

Input
import numpy as np
x=np.array([[1,2,3,4],[3,4,4,7],[5,6,5,9],[7,8,9,9],[10,11,12,15]])
print(x)
y=x[0][1]
print(y)
Output
[[ 1 2 3 4]
[ 3 4 4 7]
[ 5 6 5 9]
[ 7 8 9 9]
[10 11 12 15]]
2

Example:2
Input
x=np.array([[1,2,3,4],[3,4,4,7],[5,6,5,9],[7,8,9,9],[10,11,12,15]])
print(x)
y=x[2,0]
print(y)
Output
[[ 1 2 3 4]
[ 3 4 4 7]
[ 5 6 5 9]
[ 7 8 9 9]
[10 11 12 15]]
5

Example:3
Input
x=np.array([[1,2,3,4],[3,4,4,7],[5,6,5,9],[7,8,9,9],[10,11,12,15]])
print(x)
y=x[2]
print(y)
Output
[[ 1 2 3 4]
[ 3 4 4 7]
[ 5 6 5 9]
[ 7 8 9 9]
[10 11 12 15]]
[5 6 5 9]
Example:4
Input
x=np.array([[1,2,3,4],[3,4,4,7],[5,6,5,9],[7,8,9,9],[10,11,12,15]])
print(x)
y=x[3:,:-1]
print(y)
Output
[[ 1 2 3 4]
[ 3 4 4 7]
[ 5 6 5 9]
[ 7 8 9 9]
[10 11 12 15]]
[[ 7 8 9]

[10 11 12]]

Example:5
Input
x=np.array([[1,2,3,4],[3,4,4,7],[5,6,5,9],[7,8,9,9],[10,11,12,15]])
print(x)
y=x[0:3,0:2]
print(y)
Output
[[ 1 2 3 4]
[ 3 4 4 7]
[ 5 6 5 9]
[ 7 8 9 9]
[10 11 12 15]]
[[1 2]
[3 4]
[5 6]]

3D array slicing

Input: import numpy as np

# create a 3D array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])

# slicing the 3D array along the first axis (depth)
sliced_1 = arr_3d[0]#returns the first 2D array
print(sliced_1)

sliced_2 = arr_3d[1:] #returns the remaining 2D arrays
print(sliced_2)

# slicing the 3D array along the second axis (rows)
sliced_3 = arr_3d[:, 0] #returns the first column of all 2D arrays
print(sliced_3)
sliced_4 = arr_3d[:, :, 0] #returns the first element of all 2D arrays
print(sliced_4)

# slicing the 3D array along the third axis (columns)
sliced_5 = arr_3d[:, :, ::-1] # returns the 3D array with columns reversed
print(sliced_5)

sliced_6 = arr_3d[0][0]#Returns first 1D array of first 2D array
print(sliced_6)

sliced_7 = arr_3d[0][0][0] #Returns first element of first 1D array of first 2D array
print(sliced_7)


Output: array([[1, 2],
    [3, 4]])

array([[[ 5, 6],
    [ 7, 8]],
    [[ 9, 10],
    [11, 12]]])

array([[ 1, 2],
    [ 5, 6],
    [ 9, 10]])

array([[ 1, 3],
    [ 5, 7],
    [ 9, 11]])

array([[[ 2, 1],
    [ 4, 3]],
    [[ 6, 5],
    [ 8, 7]],
    [[10, 9],
    [12, 11]]])

array([1, 2])

1

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.