In [ ]:
from IPython.display import display_pretty
import numpy as np

Numpy Reference

  • array object
  • ndarray
  • np.transpose permute the dimensions of an array
  • np.broadcast
  • np.expand_dims expand shape of an array
  • np.squeeze
  • np.asarray
    • np.asanyarray
    • np.asfarray parse array to float
    • `np.asfortranarray
  • np.asmatrix
  • np.asscalar
  • np.require
  • np.column_stack
  • np.concatenate join a sequence of array together
  • np.dstack
  • np.hstack
  • np.vstack
  • np.array_split
    • np.dsplit
    • np.hsplit
    • np.split
    • np.vsplit
  • np.tile
  • np.repeat
  • np.delete
  • np.insert
  • np.append
  • np.resize
  • np.trim_zeros
  • np.unique
  • np.in1d
  • np.intersec1d
  • np.setdiff1d
  • np.setxor1d
  • np.union1d

sorting

  • np.sort
  • np.lexsort
  • np.argsort
  • np.ndarray.sort
  • np.msort
  • np.sort_complex
  • np.partition
  • np.argpartition
  • np.argmax
  • np.nanargmax
  • np.argmin
  • np.nanargmin

Indexing

  • np.argwhere
  • np.nonzero
  • np.flatnonzero
  • np.where
  • np.searchsorted return the index where element should be inserted in maintain order
  • np.extract
  • np.count_nonzero

np.column_stack


In [ ]:
a = [1, 2, 3]
b = [4, 5, 6]
np.column_stack((a, b))

np.concatenate


In [ ]:
a = np.array([[1,2], [3,4]])
b = np.array([[5,6]])
x1 = np.concatenate((a, b), axis=0)
x2 = np.concatenate((a, b.T), axis=1)
display_pretty(x1, x2)

np.dstack


In [ ]:
a = np.array((1, 2, 3))
b = np.array((4, 5, 6))
np.dstack((a, b))

np.tile


In [ ]:
a = np.array((0, 1, 2))
np.tile(a, 2)

np.sort


In [ ]:
a = np.array([[1, 5, 3], [2, 3, 1]])
np.sort(a, axis=0)

np.where


In [ ]:
cond = [[True, False], [True, True]]
x = [[1, 2], [3, 4]]
y = [[9, 8], [7, 6]]
a = np.where(cond, x, y)
display_pretty(a)

# equal to blow
for c, xv, yv in zip(cond, x, y):
    for c2, xv2, yv2 in zip(c, xv, yv):
        print(xv2 if c2 else yv2, end=', ')
print()

x = np.arange(9).reshape((3, 3))
display_pretty(x > 5)
b = np.where(x > 5)
display_pretty(b)

# frequently use
c = np.where(x > 3, x, -1)
display_pretty(c)