Professional Documents
Culture Documents
Vectors, Matrices
In [5]: print("A series of numbers:", np.arange(5,16)) # A series of numbers from low to high
In [7]: print("Numbers spaced apart by a float: ", np.arange(0,12,2.5)) # Numbers spaced apart by 2
In [8]: print("Every 5th number from 50 in reverse order\n", np.arange(50,-1,-5)) # -1 here is the
Vector of zeroes
---------------------
[0. 0. 0. 0. 0.]
Matrix of zeroes
--------------------
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Vector of ones
---------------------
[1. 1. 1. 1. 1.]
Matrix of ones
---------------------
[[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]]
In [12]: print("Empty matrix\n-------------\n", np.empty((3,5)))
Empty matrix
-------------
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
In [15]: print("Numbers from Normal distribution with zero mean and standard deviation 1 i.e. stand
print(np.random.randn(4,3))
Numbers from Normal distribution with zero mean and standard deviation 1 i.e. standard no
rmal
[[-0.8728734 0.74094813 -2.14250507]
[ 0.44377036 -0.77978949 -0.58962596]
[-0.31112299 -0.42895076 -0.81483648]
[ 0.21056997 1.71909533 0.31323054]]
c= a.reshape(6,5)
Shape of a: (30,)
Shape of b: (3, 2, 5)
Shape of c: (6, 5)
a looks like
--------------------
[81 51 2 18 27 83 18 75 8 40 29 10 34 95 60 69 49 48 21 19 15 15 94 23
82 14 25 25 38 3]
--------------------
b looks like
--------------------
[[[81 51 2 18 27]
[83 18 75 8 40]]
[[29 10 34 95 60]
[69 49 48 21 19]]
[[15 15 94 23 82]
[14 25 25 38 3]]]
--------------------
c looks like
--------------------
[[81 51 2 18 27]
[83 18 75 8 40]
[29 10 34 95 60]
[69 49 48 21 19]
[15 15 94 23 82]
[14 25 25 38 3]]
--------------------
In [18]: A = ri(1,100,10) #Vector of random integers
print("\nVector of random integers\n",'-'*50,"\n",A)
print("\nHere is the sorted vector\n",'-'*50,"\n",np.sort(A, kind='mergesort'))
Max of a: 95
Max of b: 95
[81 51 2 18 27 83 18 75 8 40 29 10 34 95 60 69 49 48 21 19 15 15 94 23
82 14 25 25 38 3]
Max of a location: 13
max of b location: 13
Max of c location: 13
Array: [ 0 1 2 3 4 5 6 7 8 9 10]
Element at 7th index is: 7
Elements from 3rd to 5th index are: [3 4 5]
Elements up to 4th index are: [0 1 2 3]
Elements from last backwards are: [10 9 8 7 6 5 4 3 2 1 0]
3 Elements from last backwards are: [10 8 6]
print("\nSubsetting sub-matrices\n--------------------------")
print("Matrix with row indices 1 and 2 and column indices 3 and 4\n", mat[1:3,3:5])
print("Matrix with row indices 0 and 1 and column indices 1 and 3\n", mat[0:2,[1,3]])
Subsetting sub-matrices
--------------------------
Matrix with row indices 1 and 2 and column indices 3 and 4
[[60 66]
[28 19]]
Matrix with row indices 0 and 1 and column indices 1 and 3
[[10 94]
[77 60]]
Conditional subsetting
mat_slice = mat[:2,:2]
print ("\nSliced matrix")
print(mat_slice)
print("\nBut the original matrix? WHOA! It got changed too!") # Sliced array points to or
print(mat)
mat_slice = np.array(mat[:2,:2]) # Notice the np.array command to create a new array not ju
print ("\nSliced matrix")
print(mat_slice)
print ("\nChange the sliced matrix")
mat_slice[0,0]= 100
print(mat_slice)
print("\nBut the original matrix? NO CHANGE this time:)") # using nparray created to pointe
print(mat)
Original matrix
[[11 12 13]
[21 22 23]
[31 32 33]]
Sliced matrix
[[11 12]
[21 22]]
Original matrix
[[11 12 13]
[21 22 23]
[31 32 33]]
Sliced matrix
[[11 12]
[21 22]]
print("\nAddition\n------------------\n", mat1+mat2)
print("\nMultiplication\n------------------\n", mat1*mat2)
print("\nDivision\n------------------\n", mat1/mat2)
print("\nLineaer combination: 3*A - 2*B\n-----------------------------\n", 3*mat1-2*mat2)
Addition
------------------
[[15 8 9]
[12 6 8]
[ 4 12 14]]
Multiplication
------------------
[[56 15 14]
[27 9 15]
[ 4 27 48]]
Division
------------------
[[1.14285714 1.66666667 0.28571429]
[0.33333333 1. 1.66666667]
[1. 0.33333333 1.33333333]]
NumPy’s broadcasting rule relaxes this constraint when the arrays’ shapes meet certain constraints. When
operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions,
and works its way forward. Two dimensions are compatible when
they are equal, or one of them is 1 If these conditions are not met, a ValueError: frames are not aligned
exception is thrown, indicating that the arrays have incompatible shapes. The size of the resulting array is the
maximum size along each dimension of the input arrays
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[1 0 2]
[[1. 0. 2.]
[1. 0. 2.]
[1. 0. 2.]
[1. 0. 2.]]
[[0]
[1]
[2]
[3]]
[[0. 0. 0.]
[1. 1. 1.]
[2. 2. 2.]
[3. 3. 3.]]
In [51]: # this will just broadcast in both dimensions
add_scalar = np.array([100])
print(add_scalar)
print(start + add_scalar)
[100]
[[100. 100. 100.]
[100. 100. 100.]
[100. 100. 100.]
[100. 100. 100.]]
mat1 = np.array(ri(1,100,9)).reshape(3,3)
print("\nModified matrix of random numbers from 1 to 99\n","-"*50,"\n",mat1)
print("\nStandard deviation of all numbers in the modified matrix, a larger number\n","-"*
print("\nVariance of all numbers in the modified matrix, a larger number\n","-"*80,"\n",np
print("\nMedian of all numbers in the modified matrix\n","-"*60,"\n",np.median(mat1))
mat2 = np.array(ri(1,100,50)).reshape(10,5)
print("\nModified matrix of 50 random numbers from 1 to 99\n","-"*50,"\n",mat2)
print("\nStandard deviation along the columns in the modified matrix\n","-"*60,"\n",np.std
mat1 = np.array(ri(1,100,20)).reshape(4,5)
print("\nModified matrix of random numbers from 1 to 49\n","-"*50,"\n",mat1)
print("\nFlattened and sorted matrix (as vector)\n","-"*50,"\n",np.sort(mat1.reshape(1,20)
print("\n50th percentile of all numbers in the modified matrix\n","-"*60,"\n",np.percentile
print("\n90th percentile of all numbers in the modified matrix\n","-"*60,"\n",np.percentile
[[ 1. -0.17056766]
[-0.17056766 1. ]]
In [80]: A = ri(1,50,20) # 20 random integeres from a larger range (1-50)
B = 100-2*A+10*np.random.randn(20) # B is 100 minus twice that of A plus some random noise
print("\nB is 100 minus twice that of A plus some random noise")
plt.scatter(A,B) # Scatter plot of B
plt.title("Scatter plot of A vs. B, expect a large negative correlation")
plt.show()
print("")
print(np.corrcoef(A,B)) # Correleation coefficient matrix between A and B
[[ 1. -0.94690455]
[-0.94690455 1. ]]
Dot/Inner/Outer products
In [83]: A = np.arange(1,10).reshape(3,3)
B = ri(1,10,9).reshape(3,3)
print("\n1st Matrix of 1-9 single-digit numbers (A)\n","-"*50,"\n",A)
print("\n2nd Matrix of random single-digit numbers (B)\n","-"*50,"\n",B)
In [89]: A = np.arange(1,6)
B = ri(1,10,5)
print("\n1st Vector of 1-5 numbers (A)\n","-"*50,"\n",A)
print("\n2nd Vector of 5 random single-digit numbers (B)\n","-"*50,"\n",B)
print("\nInner product of vectors A and B \n","-"*50,"\n",np.inner(A,B)) # Sum of product o
print("\nOuter product of vectors A and B \n","-"*50,"\n",np.outer(A,B)) # Normail matrix m
Transpose
In [92]: A = ri(1,10,9).reshape(3,3)
print("\n3x3 Matrix of random single-digit numbers\n","-"*50,"\n",A)
print("\nMatrix transpose\n","-"*50,"\n",np.transpose(A))
B = ri(1,10,6).reshape(3,2)
print("\n3x2 Matrix of random single-digit numbers\n","-"*50,"\n",B)
print("\n2x3 Matrix transpose\n","-"*50,"\n",np.transpose(B))
print("\nMatrix multiplication of B and B-transpose\n","-"*50,"\n",np.dot(B, np.transpose(
Matrix transpose
--------------------------------------------------
[[6 7 5]
[3 1 2]
[2 5 7]]
Trace
In [99]: A = ri(1,10,16).reshape(4,4)
print("\n4x4 Matrix of random single-digit numbers\n","-"*50,"\n",A)
print("\nMatrix trace\n","-"*50,"\n",np.trace(A)) # Sum of main diagonal elements
print("\nMatrix trace with ofset +1 (upper triangle)\n","-"*50,"\n",np.trace(A,offset=1)) #
print("\nMatrix trace with ofset +1 (upper triangle)\n","-"*50,"\n",np.trace(A,offset=2)) #
print("\nMatrix trace with ofset +1 (upper triangle)\n","-"*50,"\n",np.trace(A,offset=3))#
print("\nMatrix trace with ofset +1 (upper triangle)\n","-"*50,"\n",np.trace(A,offset=-1))
Matrix trace
--------------------------------------------------
27
In [ ]: