Numpy PDF
Numpy PDF
Ananda Dasgupta
import numpy as np
import numpy as np
Once this is done, you can use the goodies provided by numpy
using the np.goody_name format!
import numpy as np
Once this is done, you can use the goodies provided by numpy
using the np.goody_name format!
import numpy as np
Once this is done, you can use the goodies provided by numpy
using the np.goody_name format!
aa = np.array([[1,2],[3,4],[5,6]])
import numpy as np
Once this is done, you can use the goodies provided by numpy
using the np.goody_name format!
aa = np.array([[1,2],[3,4],[5,6]])
np.array([1,2,3],dtype=int) [1,2,3]
np.array([1,2,3],dtype=int) [1,2,3]
np.array([1,2,3],dtype=oat) [1.,2.,3.]
np.array([1,2,3],dtype=int) [1,2,3]
np.array([1,2,3],dtype=oat) [1.,2.,3.]
np.array([1,2,3],dtype=complex) [1.+0.j,2.+0.j,3.+0.j]
li = [1,2,3,4]
li = [1,2,3,4]
aa = np.array(li)
array([1,2,3,4])
li = [1,2,3,4]
aa = np.array(li)
array([1,2,3,4])
aa.ndim
1
li = [1,2,3,4]
aa = np.array(li)
array([1,2,3,4])
aa.ndim
1
aa.shape
(4,)
li = [[1,2],[3,4]]
li = [[1,2],[3,4]]
aa = np.array(li)
array([[1,2],
[3,4]])
li = [[1,2],[3,4]]
aa = np.array(li)
array([[1,2],
[3,4]])
aa.ndim
2
li = [[1,2],[3,4]]
aa = np.array(li)
array([[1,2],
[3,4]])
aa.ndim
2
aa.shape
(2,2)
np.zeros((2, 3))
array([[ 0., 0., 0.],
[ 0., 0., 0.]])
np.zeros((2, 3))
array([[ 0., 0., 0.],
[ 0., 0., 0.]])
np.ones(4)
array([ 1., 1., 1., 1.])
np.zeros((2, 3))
array([[ 0., 0., 0.],
[ 0., 0., 0.]])
np.ones(4)
array([ 1., 1., 1., 1.])
np.full(4,2.5)
array([ 2.5, 2.5, 2.5, 2.5])
np.zeros((2, 3))
array([[ 0., 0., 0.],
[ 0., 0., 0.]])
np.ones(4)
array([ 1., 1., 1., 1.])
np.full(4,2.5)
array([ 2.5, 2.5, 2.5, 2.5])
a = np.empty(4)
a.ll(2.5)
array([ 2.5, 2.5, 2.5, 2.5])
np.zeros((2, 3))
array([[ 0., 0., 0.],
[ 0., 0., 0.]])
np.ones(4)
array([ 1., 1., 1., 1.])
np.full(4,2.5)
array([ 2.5, 2.5, 2.5, 2.5])
a = np.empty(4)
a.ll(2.5)
array([ 2.5, 2.5, 2.5, 2.5])
np.zeros((2, 3),dtype=int)
array([[ 0, 0, 0],
[ 0, 0, 0]])
Numpy has special methods for creating special arrays that share
properties like shape and data type with existing arrays.
Numpy has special methods for creating special arrays that share
properties like shape and data type with existing arrays.
a = np.array([[1,2,3],[4,5,6]])
Numpy has special methods for creating special arrays that share
properties like shape and data type with existing arrays.
a = np.array([[1,2,3],[4,5,6]])
Numpy has special methods for creating special arrays that share
properties like shape and data type with existing arrays.
a = np.array([[1,2,3],[4,5,6]])
Numpy has special methods for creating special arrays that share
properties like shape and data type with existing arrays.
a = np.array([[1,2,3],[4,5,6]])
Numpy has special methods for creating special arrays that share
properties like shape and data type with existing arrays.
a = np.array([[1,2,3],[4,5,6]])
A A[0,:]
(0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3)
A A[0,:] A[1,:]
(0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3)
(0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3)
A[:2,:2]
(0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3)
A[:2,:2] A[:2,2:]
(0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3)
(0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3)
(0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3)
(0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3) (0, 0) (0, 1) (0, 2) (0, 3)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3) (1, 0) (1, 1) (1, 2) (1, 3)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3) (2, 0) (2, 1) (2, 2) (2, 3)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3) (3, 0) (3, 1) (3, 2) (3, 3)
A[:,[0,2]]
A[[1,2],[0,2]]
Will produce 1D array!
A[:,np.array([True,False,False,True])]
A[:,B]
True, False, False, False,
False, True, True, False,
where B =
False, True, False, True,
False, False, False, True,
Will produce 1D array!
Ananda Dasgupta The Numpy Array 31 / 57
Reshaping arrays
3 3 1
2 -5 5
+ 1
-1 4 1
1 3 7
3 3 1 1 1 1
2 -5 5 1 1 1
+
-1 4 1 1 1 1
1 3 7 1 1 1
4 4 2
3 -4 6
0 5 2
2 4 8
3 3 1 1
2 -5 5 2
+
-1 4 1 -3
1 3 7 2
3 3 1 1 1 1
2 -5 5 2 2 2
+
-1 4 1 -3 -3 -3
1 3 7 2 2 2
4 4 2
4 -3 7
-4 1 -2
3 5 9
3 3 1
2 -5 5
+ 2 -3 2
-1 4 1
1 3 7
3 3 1 2 -3 2
2 -5 5 2 -3 2
+
-1 4 1 2 -3 2
1 3 7 2 -3 2
5 0 3
4 -8 7
1 1 3
3 0 9
np.sum, np.prod
np.sum, np.prod
np.mean
np.sum, np.prod
np.mean
np.std, np.var
np.sum, np.prod
np.mean
np.std, np.var
np.cumsum, np.cumprod
np.sum, np.prod
np.mean
np.std, np.var
np.cumsum, np.cumprod
np.min, np.max
np.sum, np.prod
np.mean
np.std, np.var
np.cumsum, np.cumprod
np.min, np.max
np.argmin, np.argmax
np.sum, np.prod
np.mean
np.std, np.var
np.cumsum, np.cumprod
np.min, np.max
np.argmin, np.argmax
np.all, np.any
np.sum, np.prod
np.mean
np.std, np.var
np.cumsum, np.cumprod
np.min, np.max
np.argmin, np.argmax
np.all, np.any
We can use the keyword argument axis to control the axis over
which the aggregation is carried out!
However, math with matrices and vectors are one of the most
common applications of arrays.
However, math with matrices and vectors are one of the most
common applications of arrays.
However, math with matrices and vectors are one of the most
common applications of arrays.
However, math with matrices and vectors are one of the most
common applications of arrays.
x +2y −3z −t =0
−3y +2z +6t = −8
−3x −y +3z +t =0
2x +3y +2z −t = −8
x +2y −3z −t =0
−3y +2z +6t = −8
−3x −y +3z +t =0
2x +3y +2z −t = −8
1 +2 −3 −1 0
0
−3 +2 +6 −8
−3 −1 3 1 0
2 +3 +2 −1 −8
x +2y −3z −t =0
−3y +2z +6t = −8
−3x −y +3z +t =0
2x +3y +2z −t = −8
1 +2 −3 −1 0 1 +2 −3 −1 0
0 −3 +2 +6 −8 0 −3 +2 +6 −8
−3
−→
−1 3 1 0 0 5 −6 −2 0
2 +3 +2 −1 −8 0 −1 8 1 −8
x +2y −3z −t =0
−3y +2z +6t = −8
−3x −y +3z +t =0
2x +3y +2z −t = −8
1 +2 −3 −1 0 1 +2 −3 −1 0
0 −3 +2 +6 −8 −→
0 −3 +2 +6 −8
−3
−→
−1 3 1 0 0 5 −6 −2 0
2 +3 +2 −1 −8 0 −1 8 1 −8
1 +2 −3 −1 0
0 −3 +2 +6 −8
− 38 8 − 40
0 0
3
22 16
0 0
3 −1 − 3
x +2y −3z −t =0
−3y +2z +6t = −8
−3x −y +3z +t =0
2x +3y +2z −t = −8
1 +2 −3 −1 0 1 +2 −3 −1 0
0 −3 +2 +6 −8 −→
0 −3 +2 +6 −8
−3
−→
−1 3 1 0 0 5 −6 −2 0
2 +3 +2 −1 −8 0 −1 8 1 −8
1 +2 −3 −1 0 1 +2 −3 −1 0
0 −3 +2 +6 −8 −→
0 −3 +2 +6 −8
− 38 8 − 40 − 38 8 − 40
0 0 0 0
3 3
22 16
0 0
3 −1 − 3 0 0 0 21 −42
1 2 -3 -1 0
0 -3 2 6 -8
0 5 -6 -2 0
0 -1 8 1 -8
1 2 -3 -1 0
0 -3 2 6 -8
0 5 -6 -2 0
0 -1 8 1 -8
1 2 -3 -1 0
0 -3 2 6 -8
0 5 -6 -2 0
0 -1 8 1 -8
A(2, 1) A(3, 1)
The multipliers are and
A(1, 1) A(1, 1)
1 2 -3 -1 0
0 -3 2 6 -8
0 5 -6 -2 0
0 -1 8 1 -8
A(2, 1) A(3, 1)
The multipliers are and
A(1, 1) A(1, 1)
- or the array A[2: ,1]/A[1,1]
1 2 -3 -1 0
0 -3 2 6 -8
0 5 -6 -2 0
0 -1 8 1 -8
1 2 -3 -1 0
0 -3 2 6 -8
0 5 -6 -2 0
0 -1 8 1 -8
1 2 -3 -1 0
0 -3 2 6 -8
0 5 -6 -2 0
0 -1 8 1 -8