Numpy Examples: Indexing, Reshaping and Performing Vectorized Operations on ndarrays

Last updated:
Table of Contents

Build a matrix from a list of ndarrays

This is useful if you want to build a matrix row by row.

import numpy as np

lst = []

lst.append(np.array([1,2,3]))
lst.append(np.array([4,4,4]))

# vstack stands for "vertical stacking"
matrix = np.vstack(lst)

print(matrix)
# array([[1, 2, 3],
#        [4, 4, 4]])

print(matrix.shape)
# (2,3)

Reverse an array

This is frequently done using a double colon: myarray[::-1]

Use np.flip()


Dialogue & Discussion