def find_e2(L, tol=1e-4): ''' find the eigen vector that corresponds to the smallest non-zeros eigen values of the Laplacian matrix. Input: L: the Laplacian matrix, a numpy float matrix of shape n by n. tol: the tolerance threshold for eigen values, a float scalar. A very small positive value. If an eigen value is smaller than tol, then we consider the eigen value as being 0. Output: e2: the eigen vector corresponding to the smallest non-zero eigen value, a numpy float vector of length n. For example, if the eigen values are [0.5, 0.000000000001, 2.], the first eigen vector is the answer. Here we assume 0.00000001 is close enough to 0 because is smaller than the tolerance level (tol). For example, if the eigen values are [2., 0.0000000000001, 0.3], the last eigen vector is the answer. For example, if the eigen values are [0.00003, 0.000001, 0.3], the last eigen vector is the answer. ''' ######################################### ## INSERT YOUR CODE HERE Ep = p1.sort_eigen_pairs(p1.compute_eigen_pairs(L)) for row in Ep: if row[0] >= tol: e2 = row[1] break ######################################### return e2 ''' TEST: Now you can test the correctness of your code above by typing `nosetests -v test5.py:test_find_e2' in the terminal. '''
def compute_P(C,k): ''' Compute the projection matrix P by combining the k eigen vectors, that correspond to the top k largest eigen values of matrix C. Here the projection matrix P includes all the k principle components. Input: C: the covariance matrix, a numpy float matrix of shape p by p. k: the number of dimensions to reduce to (k should be smaller than p), an integer scalar Output: P: the projection matrix, a numpy float matrix of shape p by k. For example, if we sort the eigen pairs of matrix C in descending order, and the result [(v1,e1),(v2,e2),...., (vp, ep)], here v1 >= v2 >= v3 ... The projection matrix should be [e1^T, e2^T, ..., ek^T], here e^T represents the transpose of a row vector e (which is a column vector) Hint: you can re-use the functions in problem 1 to solve this problem. ''' ######################################### ## INSERT YOUR CODE HERE eigen = p1.compute_eigen_pairs(C) eigen_sorted = p1.sort_eigen_pairs(eigen, '') arr = np.zeros((len(eigen_sorted[0][1]), k)) for i, x in enumerate(eigen_sorted): if i >= k: break transpose = x[1][np.newaxis].transpose() for j in range(len(x[1])): arr[j][i] = transpose[j] P = np.array(arr) ######################################### return P ''' TEST: Now you can test the correctness of your code above by typing `nosetests -v test2.py:test_compute_P' in the terminal. '''
def compute_P(C,k): ''' Compute the projection matrix P by combining the k eigen vectors, that correspond to the top k largest eigen values of matrix C. Here the projection matrix P includes all the k principle components. Input: C: the covariance matrix, a numpy float matrix of shape p by p. k: the number of dimensions to reduce to (k should be smaller than p), an integer scalar Output: P: the projection matrix, a numpy float matrix of shape p by k. For example, if we sort the eigen pairs of matrix C in descending order, and the result [(v1,e1),(v2,e2),...., (vp, ep)], here v1 >= v2 >= v3 ... The projection matrix should be [e1^T, e2^T, ..., ek^T], here e^T represents the transpose of a row vector e (which is a column vector) Hint: you can re-use the functions in problem 1 to solve this problem. ''' ######################################### ## INSERT YOUR CODE HERE Ep = p1.compute_eigen_pairs(C) eigenp = p1.sort_eigen_pairs(Ep, order='descending') klargest =[] pairs = [] for i in range(k): klargest.append(eigenp[i]) pairs.append(klargest[i][1]) P = np.reshape(pairs,(k,C.shape[0])) P = P.T ######################################### return P ''' TEST: Now you can test the correctness of your code above by typing `nosetests -v test2.py:test_compute_P' in the terminal. '''
def find_e2(L, tol=1e-4): ''' find the eigen vector that corresponds to the smallest non-zeros eigen values of the Laplacian matrix. Input: L: the Laplacian matrix, a numpy float matrix of shape n by n. tol: the tolerance threshold for eigen values, a float scalar. A very small positive value. If an eigen value is smaller than tol, then we consider the eigen value as being 0. Output: e2: the eigen vector corresponding to the smallest non-zero eigen value, a numpy float vector of length n. For example, if the eigen values are [0.5, 0.000000000001, 2.], the first eigen vector is the answer. Here we assume 0.00000001 is close enough to 0 because is smaller than the tolerance level (tol). For example, if the eigen values are [2., 0.0000000000001, 0.3], the last eigen vector is the answer. For example, if the eigen values are [0.00003, 0.000001, 0.3], the last eigen vector is the answer. Hint: you could solve this problem using 6 lines of code. ''' ######################################### ## INSERT YOUR CODE HERE pairs = p1.compute_eigen_pairs(L) vals = np.argsort([i[0] for i in pairs]) e2 = [] for i in vals: if pairs[i][0] < tol: continue else: e2 = pairs[i][1] break ######################################### return e2 ''' TEST: Now you can test the correctness of your code above by typing `nosetests -v test5.py:test_find_e2' in the terminal. '''
def compute_P(C, k): ''' Compute the projection matrix P by combining the k eigen vectors, that correspond to the top k largest eigen values of matrix C. Here the projection matrix P includes all the k principle components. Input: C: the covariance matrix, a numpy float matrix of shape p by p. k: the number of dimensions to reduce to (k should be smaller than p), an integer scalar Output: P: the projection matrix, a numpy float matrix of shape p by k. For example, if we sort the eigen pairs of matrix C in descending order, and the result [(v1,e1),(v2,e2),...., (vp, ep)], here v1 >= v2 >= v3 ... The projection matrix should be [e1^T, e2^T, ..., ek^T], here e^T represents the transpose of a row vector e (which is a column vector) Hint: you can re-use the functions in problem 1 to solve this problem. ''' ######################################### ## INSERT YOUR CODE HERE eigenPairs = p1.compute_eigen_pairs(C) sortedE = p1.sort_eigen_pairs(eigenPairs, False) P = (np.vstack([x[1] for x in sortedE]).T)[:, :k] ######################################### return P ''' TEST: Now you can test the correctness of your code above by typing `nosetests s-v test2.py:test_compute_P' in the terminal. '''
def find_e2(L, tol=1e-4): ''' find the eigen vector that corresponds to the smallest non-zeros eigen values of the Laplacian matrix. Input: L: the Laplacian matrix, a numpy float matrix of shape n by n. tol: the tolerance threshold for eigen values, a float scalar. A very small positive value. If an eigen value is smaller than tol, then we consider the eigen value as being 0. Output: e2: the eigen vector corresponding to the smallest non-zero eigen value, a numpy float vector of length n. For example, if the eigen values are [0.5, 0.000000000001, 2.], the first eigen vector is the answer. Here we assume 0.00000001 is close enough to 0 because is smaller than the tolerance level (tol). For example, if the eigen values are [2., 0.0000000000001, 0.3], the last eigen vector is the answer. For example, if the eigen values are [0.00003, 0.000001, 0.3], the last eigen vector is the answer. Hint: you could solve this problem using 6 lines of code. ''' ######################################### ## INSERT YOUR CODE HERE ep = p1.sort_eigen_pairs(p1.compute_eigen_pairs(L)) E = np.asarray([x[1] for x in ep]) v = np.asarray([x[0] for x in ep]) val = 0 while tol < 1: tol *= 10 val += 1 v = np.around(v, val) minimum = float('inf') index = float('nan') for i in range(len(v)): if (0 < v[i] < minimum): minimum = v[i] index = i e2 = E[:, index] ######################################### return e2 ''' TEST: Now you can test the correctness of your code above by typing `nosetests -v test5.py:test_find_e2' in the terminal. '''