Numpy Array (ndarray) Examples

Last updated:
Table of Contents

Examples use numpy v.14.5 unless specified

See online at queirozfcom/python-sandbox

Copy array by value

use .copy()

import numpy as np

x = np.array([
    [1,2,3],
    [4,5,6]
])

y = x.copy()

# fill y with 1's
y.fill(1)

# x is unchanged
x
# array([[1, 2, 3],
#        [4, 5, 6]])

y 
# array([[1, 1, 1],
#        [1, 1, 1]])
Twitter Linkedin YC Hacker News Reddit

Access row/column of multidimensional array

Keep in mind: 0 means rows, 1 means columns, : means everything in that dimension

import numpy as np

myarray = np.array([
    [1,2,3],
    [4,5,6],
    [7,8,9]
])

# select all rows for column 1
myarray[:,1]
# array([2, 5, 8])

# select all columns for row 0
myarray[0,:]
# array([1, 2, 3])

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

Use the [::-1] notation:

import numpy as np

myarray = np.array([1,2,3])
myarray[::-1]
# array([3, 2, 1])

it also works for higher dimensions:

myarray = np.array([
    [1,2,3],
    [4,5,6],
    [7,8,9]
])
myarray[::-1]
# array([[7, 8, 9],
#       [4, 5, 6],
#       [1, 2, 3]])

You can also use np.flip()

Add row to array

Use np.vstack([array1,array2]) to add a row to a multimensional ndarray.

Vstack means vertical stacking.

import numpy as np

myarray = np.array([
    [1,2,3],
    [4,5,6]
])

newrow = np.array([9,9,9])

np.vstack([myarray,newrow])
# array([[1, 2, 3],
#       [4, 5, 6],
#       [9, 9, 9]])

Add column to array

np.hstack([array1, array2]) means horizontal stacking:

import numpy as np

myarray = np.array([
    [1,2,3],
    [4,5,6]
])

newcol = np.array([[9],[9]])

np.hstack([myarray,newcol])
# array([[1, 2, 3, 9],
#        [4, 5, 6, 9]])

Compare two arrays for equality

For approximate matches, use np.allclose() instead

np.array_equal(a,b) returns True is all elements from a and b are strictly equal to one another (element-wise)

import numpy as np

a = np.array([1,2,3])
b = np.array([0,0,0])

np.array_equal(a,b)
# False

Heads up! np.array_equal will return False if either array has NaNs!

import numpy as np

a = np.array([1,np.nan,3])
b = np.array([1,np.nan,3])

# nan is not equal to anything, not even itself
np.array_equal(a,b)
# False
import numpy as np

np.set_printoptions(threshold=np.inf)

To reset the original print options, call set_printoptions() with the default arguments

To disable scientific notation, use suppress=True

import numpy as np

y=np.array([1.0e-10,1.0,100,1000])
# array([1.e-10, 1.e+00, 1.e+02, 1.e+03])

np.set_printoptions(suppress=True)
y
# array([   0.,    1.,  100., 1000.])

Reset print options

To reset the original print options, call np.set_printoptions() with the default arguments:

import numpy as np

np.set_printoptions(edgeitems=3,infstr='inf',
linewidth=75, nanstr='nan', precision=8,
suppress=False, threshold=1000, formatter=None)

Dialogue & Discussion