import numpy as np
a = np.arange(5)
a
array([0, 1, 2, 3, 4])
print(a*2)
print(a+3)
print(a-2)
[0 2 4 6 8] [3 4 5 6 7] [-2 -1 0 1 2]
b = np.ones(5)
b
array([ 1., 1., 1., 1., 1.])
print(a-b*2)
print(a+b)
[-2. -1. 0. 1. 2.] [ 1. 2. 3. 4. 5.]
# these operations are faster in numpy than pure python
a = np.arange(10000)
%timeit a+2
The slowest run took 6.27 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 3: 11.4 µs per loop
# in native python
a = range(10000)
%timeit [i+2 for i in a]
1000 loops, best of 3: 1.03 ms per loop
a = np.random.randint(1, 10, 5)
a
array([2, 8, 3, 4, 2])
b =np.random.randint(1, 10, 5)
b
array([6, 3, 9, 3, 4])
a * b # elementwise operations
array([12, 24, 27, 12, 8])
a = np.random.randint(1, 10, (2,3))
a
array([[9, 5, 5], [7, 2, 4]])
b = np.random.randint(1, 10, (2,3))
b
array([[2, 1, 5], [5, 2, 7]])
a * b # element wise multiplication
array([[18, 5, 25], [35, 4, 28]])
# a.dot(b) this will throw an error as Matix multiplication is not possible here
a.dot(b.T) # b.T will transpose the 2,3 matrix to 3,2 matrix
array([[48, 90], [36, 67]])
a = np.array([1, 3, 7, 6, 5])
b = np.array([1, 2, 7, 8, 5])
print(a,b)
[1 3 7 6 5] [1 2 7 8 5]
# Comparison
a == b
array([ True, False, True, False, True], dtype=bool)
a > b
array([False, True, False, False, False], dtype=bool)
a = np.array([1,2,3,4])
b = np.array([3,4, 9, 8])
c = np.array([1,2,3,4])
np.array_equal(a, b)
False
np.array_equal(a, c)
True
a = np.array([1,1,0,0], dtype='bool')
b = np.array([1,0,1,0], dtype='bool')
print(a, b)
[ True True False False] [ True False True False]
np.logical_or(a,b)
array([ True, True, True, False], dtype=bool)
np.logical_and(a,b)
array([ True, False, False, False], dtype=bool)
a = np.array([1, 0.5, 0, 3])
a
array([ 1. , 0.5, 0. , 3. ])
np.sin(a)
array([ 0.84147098, 0.47942554, 0. , 0.14112001])
np.log(a)
C:\tools\Anaconda3\lib\site-packages\ipykernel\__main__.py:1: RuntimeWarning: divide by zero encountered in log if __name__ == '__main__':
array([ 0. , -0.69314718, -inf, 1.09861229])
np.exp(a)
array([ 2.71828183, 1.64872127, 1. , 20.08553692])
a = np.triu(np.ones((3, 3)), 1)
a
array([[ 0., 1., 1.], [ 0., 0., 1.], [ 0., 0., 0.]])
a.T # transposition of array is just a view and stored in memory
array([[ 0., 0., 0.], [ 1., 0., 0.], [ 1., 1., 0.]])
x = np.array([1,2,3,4])
x.sum()
10
x = np.array([[1,2,3],[3,4,5]])
x
array([[1, 2, 3], [3, 4, 5]])
x.sum(axis=0) # column sum
array([4, 6, 8])
x.sum(axis=1) # row sum
array([ 6, 12])
x.min()
1
x.min(axis=1)
array([1, 3])
x.max(axis=0)
array([3, 4, 5])
x.argmin() # index of min element
0
x.argmax() # index of max element
5
np.all([True, False, True])
False
np.any([True, False, True])
True
a = np.zeros((10,10))
np.any(a)
False
np.all(a)
False
np.all(a==0)
True
np.any(a!=0)
False
x = np.array([1, 2, 3, 1])
y = np.array([[1, 2, 3], [5, 6, 1]])
print(x, "\n\n",y)
[1 2 3 1] [[1 2 3] [5 6 1]]
x.mean()
1.75
np.median(x)
1.5
np.median(y, axis=-1) # last axis
array([ 2., 5.])
x.std()
0.82915619758884995
a = np.ones((3,4))
a
array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]])
a[2:,2:]=0
a
array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 0., 0.]])
a = np.arange(0, 40, 10)
a.shape
(4,)
a = a[:, np.newaxis] # newaxis is a nice feature supported by numpy
a
array([[ 0], [10], [20], [30]])
a.shape
(4, 1)
mileposts = np.array([0, 198, 303, 736, 871, 1175, 1475, 1544,1913, 2448])
distance = np.abs(mileposts - mileposts[:, np.newaxis])
distance
array([[ 0, 198, 303, 736, 871, 1175, 1475, 1544, 1913, 2448], [ 198, 0, 105, 538, 673, 977, 1277, 1346, 1715, 2250], [ 303, 105, 0, 433, 568, 872, 1172, 1241, 1610, 2145], [ 736, 538, 433, 0, 135, 439, 739, 808, 1177, 1712], [ 871, 673, 568, 135, 0, 304, 604, 673, 1042, 1577], [1175, 977, 872, 439, 304, 0, 300, 369, 738, 1273], [1475, 1277, 1172, 739, 604, 300, 0, 69, 438, 973], [1544, 1346, 1241, 808, 673, 369, 69, 0, 369, 904], [1913, 1715, 1610, 1177, 1042, 738, 438, 369, 0, 535], [2448, 2250, 2145, 1712, 1577, 1273, 973, 904, 535, 0]])
x, y = np.arange(5), np.arange(5)[:, np.newaxis]
print(x, "\n\n", y)
[0 1 2 3 4] [[0] [1] [2] [3] [4]]
distance = np.sqrt(x ** 2 + y ** 2)
distance
array([[ 0. , 1. , 2. , 3. , 4. ], [ 1. , 1.41421356, 2.23606798, 3.16227766, 4.12310563], [ 2. , 2.23606798, 2.82842712, 3.60555128, 4.47213595], [ 3. , 3.16227766, 3.60555128, 4.24264069, 5. ], [ 4. , 4.12310563, 4.47213595, 5. , 5.65685425]])
the numpy.ogrid function allows to directly create vectors x and y of the previous example, with two "significant dimensions":
x, y = np.ogrid[0:5, 0:5]
print(x, "\n\n", y)
[[0] [1] [2] [3] [4]] [[0 1 2 3 4]]
print(x.shape, y.shape)
(5, 1) (1, 5)
distance = np.sqrt(x ** 2 + y ** 2)
distance
array([[ 0. , 1. , 2. , 3. , 4. ], [ 1. , 1.41421356, 2.23606798, 3.16227766, 4.12310563], [ 2. , 2.23606798, 2.82842712, 3.60555128, 4.47213595], [ 3. , 3.16227766, 3.60555128, 4.24264069, 5. ], [ 4. , 4.12310563, 4.47213595, 5. , 5.65685425]])
np.mgrid directly providesmatrices full of indices for cases where we can’t (or don’t want to) benefit frombroadcasting
x, y = np.mgrid[0:4, 0:4]
print(x, "\n\n", y)
[[0 0 0 0] [1 1 1 1] [2 2 2 2] [3 3 3 3]] [[0 1 2 3] [0 1 2 3] [0 1 2 3] [0 1 2 3]]
Higher dimensions: last dimensions ravel out “first”.
a = np.array([[1, 2, 3], [4, 5, 6]])
a
array([[1, 2, 3], [4, 5, 6]])
a.ravel()
array([1, 2, 3, 4, 5, 6])
a.T.ravel()
array([1, 4, 2, 5, 3, 6])
reshaping may return view or copy, so use caustiously
a.shape
(2, 3)
a.ravel()
array([1, 2, 3, 4, 5, 6])
a.ravel().reshape(3,2)
array([[1, 2], [3, 4], [5, 6]])
a.reshape((2, -1)) # # unspecified (-1) value is inferred
array([[1, 2, 3], [4, 5, 6]])
a.reshape((3, -1))
array([[1, 2], [3, 4], [5, 6]])
a = np.arange(4)
a
array([0, 1, 2, 3])
a[:, np.newaxis]
array([[0], [1], [2], [3]])
a[np.newaxis,:]
array([[0, 1, 2, 3]])
a = np.arange(4*3*2).reshape(4, 3, 2)
a
array([[[ 0, 1], [ 2, 3], [ 4, 5]], [[ 6, 7], [ 8, 9], [10, 11]], [[12, 13], [14, 15], [16, 17]], [[18, 19], [20, 21], [22, 23]]])
a.shape
(4, 3, 2)
a[0,1,0]
2
a.ravel().reshape(2,3,4)
array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
o = np.arange(4)
o.resize((8,))
o
# It will throw an error
# ValueError: cannot resize an array that references or is referenced
# by another array in this way. Use the resize function
array([0, 1, 2, 3, 0, 0, 0, 0])
a = np.array([[4, 3, 5], [1, 2, 1]])
a
array([[4, 3, 5], [1, 2, 1]])
b = np.sort(a, axis=1) # Sorts each row separately!
b
array([[3, 4, 5], [1, 1, 2]])
a.sort(axis=1) # inplace sort
a
array([[3, 4, 5], [1, 1, 2]])
# Sorting with fancy indexing:
a = np.array([4, 3, 1, 2])
j = np.argsort(a)
j
array([2, 3, 1, 0], dtype=int64)
a[j]
array([1, 2, 3, 4])
# Finding minima and maxima
j_max = np.argmax(a)
j_min = np.argmin(a)
print(j_max, j_min) # indexes
print(a[j_max], a[j_min])
0 2 4 1