Categories: NumpyPython

Indexing and slicing Numpy arrays in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

When an Numpy array is created, its elements can be returned by indexing. Indexing of array elements is implemented by using brackets. If there are several non contiguous elements to be indexed, a list of indices should be appended. Similar as for Python list, negative indexing represents indexing number from the end of an Numpy array. If you want to return parts of an array and assign to a new array, this is called slicing. While slicing a Python list is a copy of the corresponding part of a list, slicing of Numpy array is the view of the original array, which is quite different. Next code examples show working with indexing and slicing of Numpy arrays.

#Import Numpy module
import numpy as np
#create an one-dimensional array of length 10
x = np.arange(1, 11)
x
#output
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
#return the 9th element
x[8]
#Out 
9
#Indexing using negative symbol
#the last element of Numpy array
x[-1]
#Out
10
#the third last element of an array
x[-3]
#Out 
8 
#Using colon for contiguous indexing
#return second to fourth elements of array
x[1:4]
#Output
array([2, 3, 4])
#non contiguous indexing of array
x[[5, 7, 9]]
#Out
array([ 6,  8, 10])
#a two-dimensional array of shape (2,4)
B = np.arange(1, 9).reshape((2, 4))
B
#output
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
#indexing the element at second row, fourth column of array
B[1, 3]
#Output
8
#Create an array of shape(3,3)
C = np.arange(1, 10).reshape((3, 3))
C
#Output 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
#return second row all column of array
C[1, :]
#result
array([4, 5, 6])
#return all rows, third column of array
C[:, 2]
#Output
array([3, 6, 9])
#return first three rows, and first and second columns
C[0:3, 0:2]
#Output
array([[1, 2],
       [4, 5],
       [7, 8]])
#return first and second rows, first and second columns of array
C[[1, 2], 1:3]
#Output 
array([[5, 6],
       [8, 9]])

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

what are Transformer, LLM , and GPT

In the field of AI and Natural Language Processing (NLP), The Transformer is the engine…

1 day ago

Main framework for PyTorch music generation model

If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…

4 days ago

affine_grid() in pytorch

According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…

2 months ago

How to create an Android mobile app with a deep learning AI model ?

Creating an Android mobile app with a deep learning AI model involves several key steps:…

11 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 year ago

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

1 year ago