<jupyter_start><jupyter_text>Matrices and Vectors<jupyter_code>A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
A
# initialise a vector
v = np.array([1,2,3])
v
# Get the dimension of the matrix A where m = rows and n = columns
[m, n] = A.shape
print(m, n)
# Get the dimension of the vector v 
dim_v = v.shape
dim_v
# Now let's index into the 2nd row 3rd column of matrix A
# Python arrays are zero indexed
A_23 = A[1,2]
A_23<jupyter_output><empty_output><jupyter_text>Addition and Scalar Multiplication<jupyter_code># Initialize matrix A and B 
A = np.array([[1, 2, 4], [ 5, 3, 2]])
B = np.array([[1, 3, 4], [1, 1, 1]])
print(A)
print(B)
# Initialize constant s 
s = 2
# See how element-wise addition works
add_AB = A + B
add_AB
# See how element-wise subtraction works
sub_AB = A - B
sub_AB
# See how scalar multiplication works
mult_As = A * s
mult_As
# Divide A by s
div_As = A / s
div_As
# What happens if we have a Matrix + scalar?
add_As = A + s
add_As<jupyter_output><empty_output><jupyter_text>Matrix Vector Multiplication<jupyter_code># Initialize matrix A 
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
A
# Initialize vector v 
v = np.array([[1], [1], [1]])
v
# Multiply A * v
Av = np.dot(A, v)
Av<jupyter_output><empty_output><jupyter_text>Matrix Matrix Multiplication<jupyter_code># Initialize a 3 by 2 matrix 
A = np.array([[1, 2], [3, 4], [5, 6]])
A
# Initialize a 2 by 1 matrix 
B = np.array([[1], [2]])
B
# We expect a resulting matrix of (3 by 2)*(2 by 1) = (3 by 1) 
mult_AB = np.dot(A, B)
mult_AB<jupyter_output><empty_output><jupyter_text>Matrix Multiplication Properties<jupyter_code># Initialize random matrices A and B 
A = np.array([[1,2],[4,5]])
B = np.array([[1,1],[0,2]])
print(A)
print(B)
# Initialize a 2 by 2 identity matrix
I = np.identity(2)
I
# The above notation is the same as I = [1,0;0,1]
# What happens when we multiply I*A ? 
IA = np.dot(I, A)
IA
# How about A*I ? 
AI = np.dot(A, I)
AI
# Compute A*B 
AB = np.dot(A, B)
AB
# Is it equal to B*A? 
BA = np.dot(B, A)
BA
# Note that IA = AI but AB != BA<jupyter_output><empty_output><jupyter_text>Inverse and Transpose<jupyter_code># Initialize matrix A 
A = np.array([[1,2,0], [0,5,6], [7,0,9]])
A
# Transpose A 
A_trans = A.T
A_trans
# Take the inverse of A 
A_inv = np.linalg.inv(A)
A_inv
# What is A^(-1)*A? 
A = np.array([[1,2,0], [0,5,6], [7,0,9]])
A_inv = np.linalg.inv(A)

A_invA = np.dot(np.linalg.inv(A), A)
A_invA
# Other are close to zero so effectively the identity matrix<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15825049701789265, 'fraction_numerical': 0.04095427435387674, 'mean_word_length': 4.410752688172043, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '423840', 'license': 'MIT', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1078, 'n_words': 339, 'path': 'week_1/01_Matrices_Review_py.ipynb', 'repo_name': 'garybake/coursera_ml', 'size': '2515'}