NumPy is the fundamental package for scientific computing with Python. It contains among other things:
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.
Library documentation: http://www.numpy.org/
from numpy import *
# declare a vector using a list as the argument
v = array([1,2,3,4])
v
array([1, 2, 3, 4])
# declare a matrix using a nested list as the argument
M = array([[1,2],[3,4]])
M
array([[1, 2], [3, 4]])
# still the same core type with different shapes
type(v), type(M)
(numpy.ndarray, numpy.ndarray)
M.size
4
# arguments: start, stop, step
x = arange(0, 10, 1)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
linspace(0, 10, 25)
array([ 0. , 0.41666667, 0.83333333, 1.25 , 1.66666667, 2.08333333, 2.5 , 2.91666667, 3.33333333, 3.75 , 4.16666667, 4.58333333, 5. , 5.41666667, 5.83333333, 6.25 , 6.66666667, 7.08333333, 7.5 , 7.91666667, 8.33333333, 8.75 , 9.16666667, 9.58333333, 10. ])
logspace(0, 10, 10, base=e)
array([ 1.00000000e+00, 3.03773178e+00, 9.22781435e+00, 2.80316249e+01, 8.51525577e+01, 2.58670631e+02, 7.85771994e+02, 2.38696456e+03, 7.25095809e+03, 2.20264658e+04])
x, y = mgrid[0:5, 0:5]
x
array([[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4]])
y
array([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]])
from numpy import random
random.rand(5,5)
array([[ 0.88096372, 0.53238822, 0.17775764, 0.76591586, 0.6127709 ], [ 0.51258827, 0.05731522, 0.05610599, 0.36338405, 0.29548536], [ 0.54649788, 0.60544106, 0.38081415, 0.5717322 , 0.2426889 ], [ 0.96448533, 0.22105112, 0.41292727, 0.40652867, 0.57179488], [ 0.55815745, 0.22049273, 0.30680923, 0.82881023, 0.36665264]])
# normal distribution
random.randn(5,5)
array([[ 0.40801047, -0.36738023, 0.0654462 , 0.16108406, 0.08391533], [-1.31495404, -1.31773965, 1.01225524, 0.28113264, -1.32523908], [ 1.09106398, -0.37571802, 2.01780085, 0.16072945, 1.0688331 ], [ 0.54306468, 0.9436181 , -2.60779314, 0.27348637, 0.60950091], [-1.0055051 , 1.77771874, 0.33209667, -0.10772336, -0.66501805]])
diag([1,2,3])
array([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
M.itemsize
4
M.nbytes
16
M.ndim
2
v[0], M[1,1]
(1, 4)
M[1]
array([3, 4])
# assign new value
M[0,0] = 7
M
array([[7, 2], [3, 4]])
M[0,:] = 0
M
array([[0, 0], [3, 4]])
# slicing works just like with lists
A = array([1,2,3,4,5])
A[1:3]
array([2, 3])
A = array([[n+m*10 for n in range(5)] for m in range(5)])
A
array([[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [20, 21, 22, 23, 24], [30, 31, 32, 33, 34], [40, 41, 42, 43, 44]])
row_indices = [1, 2, 3]
A[row_indices]
array([[10, 11, 12, 13, 14], [20, 21, 22, 23, 24], [30, 31, 32, 33, 34]])
# index masking
B = array([n for n in range(5)])
row_mask = array([True, False, True, False, False])
B[row_mask]
array([0, 2])
v1 = arange(0, 5)
v1 + 2
array([2, 3, 4, 5, 6])
v1 * 2
array([0, 2, 4, 6, 8])
v1 * v1
array([ 0, 1, 4, 9, 16])
dot(v1, v1)
30
dot(A, v1)
array([ 30, 130, 230, 330, 430])
# cast changes behavior of + - * etc. to use matrix algebra
M = matrix(A)
M * M
matrix([[ 300, 310, 320, 330, 340], [1300, 1360, 1420, 1480, 1540], [2300, 2410, 2520, 2630, 2740], [3300, 3460, 3620, 3780, 3940], [4300, 4510, 4720, 4930, 5140]])
# inner product
v.T * v
array([ 1, 4, 9, 16])
C = matrix([[1j, 2j], [3j, 4j]])
C
matrix([[ 0.+1.j, 0.+2.j], [ 0.+3.j, 0.+4.j]])
conjugate(C)
matrix([[ 0.-1.j, 0.-2.j], [ 0.-3.j, 0.-4.j]])
# inverse
C.I
matrix([[ 0.+2.j , 0.-1.j ], [ 0.-1.5j, 0.+0.5j]])
mean(A[:,3])
23.0
std(A[:,3]), var(A[:,3])
(14.142135623730951, 200.0)
A[:,3].min(), A[:,3].max()
(3, 43)
d = arange(1, 10)
sum(d), prod(d)
(45, 362880)
cumsum(d)
array([ 1, 3, 6, 10, 15, 21, 28, 36, 45])
cumprod(d)
array([ 1, 2, 6, 24, 120, 720, 5040, 40320, 362880])
# sum of diagonal
trace(A)
110
m = random.rand(3, 3)
m
array([[ 0.37938474, 0.93337301, 0.10864521], [ 0.144712 , 0.12270014, 0.622434 ], [ 0.16307745, 0.4850791 , 0.59703797]])
# use axis parameter to specify how function behaves
m.max(), m.max(axis=0)
(0.93337300979654614, array([ 0.37938474, 0.93337301, 0.622434 ]))
A
array([[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [20, 21, 22, 23, 24], [30, 31, 32, 33, 34], [40, 41, 42, 43, 44]])
# reshape without copying underlying data
n, m = A.shape
B = A.reshape((1,n*m))
B
array([[ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44]])
# modify the array
B[0,0:5] = 5
B
array([[ 5, 5, 5, 5, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44]])
# also changed
A
array([[ 5, 5, 5, 5, 5], [10, 11, 12, 13, 14], [20, 21, 22, 23, 24], [30, 31, 32, 33, 34], [40, 41, 42, 43, 44]])
# creates a copy
B = A.flatten()
B
array([ 5, 5, 5, 5, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44])
# can insert a dimension in an array
v = array([1,2,3])
v[:, newaxis], v[:,newaxis].shape, v[newaxis,:].shape
(array([[1], [2], [3]]), (3L, 1L), (1L, 3L))
repeat(v, 3)
array([1, 1, 1, 2, 2, 2, 3, 3, 3])
tile(v, 3)
array([1, 2, 3, 1, 2, 3, 1, 2, 3])
w = array([5, 6])
concatenate((v, w), axis=0)
array([1, 2, 3, 5, 6])
# deep copy
B = copy(A)