コード例 #1
0
    def cca_projection(self, X, Y, k=2):
        '''
		Return U_K^T, \Simgma_{XX}^{-1/2}
		'''

        ###SCALE AN IDENTITY MATRIX BY THIS TERM AND ADD TO COMPUTED COVARIANCE MATRIX TO PREVENT IT BEING SINGULAR ###
        reg = 1e-5
        #number of classes for this part is 3
        y_one_hot = create_one_hot_label(Y, 3)
        #perform mean subtraction
        X_new, Y_new = subtract_mean_from_data(X, y_one_hot)
        m = X_new.shape[1]
        n = Y_new.shape[1]

        XX = compute_covariance_matrix(X_new, X_new)
        YY = compute_covariance_matrix(Y_new, Y_new)
        #compute the trace of each matrix
        XX += np.trace(XX)**reg**np.eye(m)
        YY += np.trace(YY)**reg**np.eye(n)
        #compute the inverses
        XX_inverse = inv(sqrtm(XX)).T
        YY_inverse = inv(sqrtm(YY)).T
        #correlation X and Y
        correlation_XY = np.dot(
            np.dot(X_new, XX_inverse).T, np.dot(Y_new, YY_inverse))
        #finally svd decomposition
        U, sigma, V = svd(correlation_XY)

        #first two columns only
        U = U[:, :3]
        return U.T, XX_inverse
コード例 #2
0
	def cca_projection(self,X,Y,k=2):

		'''
		Return U_K^T, \Simgma_{XX}^{-1/2}
		'''

		Y = create_one_hot_label(Y,self.NUM_CLASSES)
		X,Y = subtract_mean_from_data(X,Y)


		C_XY = compute_covariance_matrix(X,Y)
		C_XX = compute_covariance_matrix(X,X)
		C_YY = compute_covariance_matrix(Y,Y)

		dim_x = C_XX.shape[0]
		dim_y = C_YY.shape[0]

		A = inv(sqrtm(C_XX+1e-5*np.eye(dim_x)))
		B = inv(sqrtm(C_YY+1e-5*np.eye(dim_y)))


		C = np.matmul(A,np.matmul(C_XY,B))



		u,s,d = svd(C)

		return u[:,0:k].T, A
コード例 #3
0
ファイル: projection.py プロジェクト: dhruvmalik95/189hw
    def cca_projection(self, X, Y, k=2):
        '''
		Return U_K^T, \Simgma_{XX}^{-1/2}
		'''

        ###SCALE AN IDENTITY MATRIX BY THIS TERM AND ADD TO COMPUTED COVARIANCE MATRIX TO PREVENT IT BEING SINGULAR ###
        reg = 1e-5

        Y = create_one_hot_label(Y, self.NUM_CLASSES)
        X, Y = subtract_mean_from_data(X, Y)

        cov_XX = compute_covariance_matrix(X, X)
        cov_XX = cov_XX + reg * np.identity(len(cov_XX))
        cov_XY = compute_covariance_matrix(X, Y)
        cov_YY = compute_covariance_matrix(Y, Y)
        cov_YY = cov_YY + reg * np.identity(len(cov_YY))

        left = sqrtm(inv(cov_XX))
        middle = cov_XY
        right = sqrtm(inv(cov_YY))

        m = left.dot(middle.dot(right))

        U, D, V = svd(m)
        return (U.T)[0:k], left
コード例 #4
0
ファイル: ridge_model.py プロジェクト: YaraMubarak/cs189HWS
	def train_model(self,X,Y): 

	
		Y_one_hot = create_one_hot_label(Y,self.NUM_CLASSES)

		self.ridge = Ridge(alpha=self.lmbda)

		self.ridge.fit(X,Y_one_hot)
コード例 #5
0
    def train_model(self, X, Y):
        ''''
		FILL IN CODE TO TRAIN MODEL
		MAKE SURE TO ADD HYPERPARAMTER TO MODEL 

		'''
        Y = create_one_hot_label(Y, self.NUM_CLASSES)
        self.solver = Ridge(self.lmda)
        self.solver.fit(X, Y)
コード例 #6
0
    def pca_projection(self, X, Y):
        '''
        Return U_2^T
        '''
        Y = create_one_hot_label(Y, self.NUM_CLASSES)
        X, Y = subtract_mean_from_data(X, Y)

        C_XX = compute_covariance_matrix(X, X)

        u, s, d = svd(C_XX)

        return u[:, 0:2].T
コード例 #7
0
    def pca_projection(self, X, Y):
        '''
		Return U_2^T
		'''
        Y_one_hot = create_one_hot_label(Y, 729)
        #perform mean subtraction
        X_new, Y_new = subtract_mean_from_data(X, Y_one_hot)
        #compute covariance matrix
        cov_matrix = compute_covariance_matrix(X_new, Y_new)
        #svd decomposition
        U, sigma, V_transpose = LA.svd(cov_matrix, full_matrices=False)

        return np.dot(U, np.dot(np.diag(sigma), V_transpose.T))
コード例 #8
0
ファイル: projection.py プロジェクト: nathanaelraj/CS189
 def cca_projection(self, X, Y, k=2):
     reg = 1e-5
     Y = np.array(Y)
     one_hot_y = create_one_hot_label(Y, self.NUM_CLASSES)
     X_bar = subtract_mean_from_data(X, Y)
     Y_bar = subtract_mean_from_data(one_hot_y, X)
     cov_XX = compute_covariance_matrix(X_bar[0], X_bar[0])
     cov_XX += np.identity(729) * (reg)
     cov_YY = compute_covariance_matrix(Y_bar[0], Y_bar[0])
     cov_XY = compute_covariance_matrix(X_bar[0], Y_bar[0])
     cov_XX_inv = inv(sqrtm(cov_XX))
     cov_YY_inv = inv(sqrtm(cov_YY))
     ccm = cov_XX_inv.dot(cov_XY).dot(cov_YY_inv)
     U, s, V = svd(ccm)
     return U.T[:k], cov_XX_inv
コード例 #9
0
 def train_model(self, X, Y):
     X = np.array(X)
     Y = create_one_hot_label(Y, 3)
     self.clf = Ridge(alpha=self.lmda)
     self.clf.fit(X, Y)