import numpy as np a = np.array([1, 2, 3]) # this creates a 1d array b = np.array([[4, 5, 6], [7, 8, 9]]) # this creates a 2d array type(a), type(b) print(a[0]) # accessing an element in a 1d array print(a[1:]) # accessing elements in a 1d array print(b[1,1]) # accessing an element in a 2d array print(b[:,::2]) # accessing elements in a 2d array b[1,1] = -8 print(b) a_1 = np.array([1, 2, 3]) # this creates a 1d array a_2 = np.array([[1, 2, 3]]) # this creates a 2d array, which is different from the above a_3 = np.array([[1], [2], [3]]) # this creates a 2d array, which is different from the above print(a_1) print(a_2) print(a_3) print(a_1.shape) print(a_2.shape) print(a_3.shape) a = np.ones(5) # this creates a 1d array with all ones print(a) print(a.shape) b = np.ones((5,3)) # this creates a 2d array with all ones print(b) print(b.shape) np.zeros((3,2)) # this creates a 2d array with all zeros np.eye(4) # this creates a 4x4 identity matrix np.eye(4, dtype=int) # this creates an integer identity matrix x = np.random.randn(4,2) print (x) print (x.T) np.random.seed(0) np.random.rand(1,2) np.random.rand(5) np.random.randn(5,4) A = np.random.randn(1000000) # generates a million random samples from standard normal distribution print(np.mean(A)) # computes mean of A print(np.std(A)) # computes standard deviation of A np.random.randint(12, size=(10,2)) # generate 10 random nonnegative integers less than 12 np.random.randint(12, size=(4,6)) a = np.random.randn(100) b = np.random.randn(100) c = np.dot(a,b) print(f'a: {a}') print(f'b: {b}') print(f'c: {c}') A = np.random.randn(100,50) B = np.random.randn(50,20) C = np.dot(A,B) print(C.shape) C2 = A@B print(np.max(abs(C-C2))) # check if C and C2 are equal A = np.random.randn(100,50) b = np.random.randn(50) C = np.dot(A,b) print(C.shape) C2 = A@b print(np.max(abs(C-C2))) # check if C and C2 are equal x = np.arange(-6,5,2) print(x) np.linalg.norm(x) np.linalg.norm(x,2) np.linalg.norm(x,1) sum(abs(x)) np.linalg.norm(x,np.inf) np.max(abs(x)) A = np.array([[1,2],[3,4]]) norm_A_fro = np.linalg.norm(A,'fro') print(norm_A_fro**2) A = np.random.randn(10,5) np.linalg.norm(A,2) U, S, VT = np.linalg.svd(A, full_matrices=False) # singular value decomposition of A print(S) # print singular values of A print(np.linalg.norm(U@np.diag(S)@VT-A)) # check if A=U*S*VT A = np.random.randn(3,3) D, Q = np.linalg.eig(A) # eigenvalues and eigenvectors of A print(D) # eigenvalues of A print(Q) # eigenvectors of A A = np.random.randn(10,10) A = A + A.T # make A symmteric D, Q = np.linalg.eig(A) # eigenvalues and eigenvectors of A print(D) # print eigenvalues of A print(np.linalg.norm(Q@np.diag(D)@Q.T-A)) # check if A=Q*D*Q.T n = 1000; x = np.random.randn(n); A = np.random.randn(n,n) y = np.dot(A,x) x_hat = np.linalg.solve(A,y) print(np.linalg.norm(x)) print(np.linalg.norm(x_hat-x)) x[0:10] x_hat[0:10] x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]]) v = np.random.randn(3) # random noise y = np.zeros(x.shape) # creates a zero matrix with the same shape as x print(f'v: {v}') for i in range(x.shape[0]): y[i, :] = x[i, :] + v print(y) vv = np.tile(v, (x.shape[0], 1)) # Stack 4 copies of v on top of each other y = x + vv print(y) y = x + v print(y) # outer product v = np.array([1,2,3]) # v has shape (3,) w = np.array([4,5]) # w has shape (2,) x = v.reshape(3,1)*w print(x) # adding a vector to each row of a matrix x = np.array([[1,2,3], [4,5,6]]) print(x+v) # adding a vector to each column of a matrix print(x+w.reshape(2, 1))