10601-hws/HW 4/handout/python/LogReg.py
def calculateGradient(self, weight, X, Y, regLambda):
        '''
        Computes the gradient of the objective function
        Arguments:
        
            X is a n-by-(d+1) numpy matrix
            Y is an n-by-1 dimensional numpy matrix
            weight is (d+1)-by-1 dimensional numpy matrix
            regLambda is the scalar regularization constant
        Returns:
            the gradient, an (d+1)-by-1 dimensional numpy matrix
        '''
        #Takes in the weight matrix
        def lrHypothesis_Theta(Theta, xRow):
            # To use np.dot, make sure that you are multiplying matrices - and for that, make sure that xRow is a matrix, and not an array
            #may have to try np.dot(Theta, xRow)
            return self.sigmoid(np.dot(np.transpose(Theta), xRow))

        # Make Array filled with LogReg Hypothesis function, so we don't compute it every time
        weight = np.array(weight)
        hyp = np.zeros((X.shape[0],1))
        for i in range(0, hyp.shape[0]):
            hyp[i] = lrHypothesis_Theta(weight, X[i])

        Gradient = np.zeros((weight.shape[0],1))
        for j in range(0, Gradient.shape[0]):
            reg = regLambda*weight[j]
            subt = np.subtract(hyp, Y)
            mult = np.transpose(subt)[0]*X[:,j]
            Gradient[j] = np.sum(mult, axis=0) + reg
        Gradient[0] = Gradient[0] - (regLambda*weight[0])

        return Gradient

        Computes the gradient of the objective function
        Arguments:
        
            X is a n-by-(d+1) numpy matrix
            Y is an n-by-1 dimensional numpy matrix
            weight is (d+1)-by-1 dimensional numpy matrix
            regLambda is the scalar regularization constant
        Returns:
            the gradient, an (d+1)-by-1 dimensional numpy matrix
        
Computes the gradient of the objective function
Arguments.


Computes the gradient of the objective function Arguments .
def calculateGradient(self, weight, X, Y, regLambda):
        def lrHypothesis_Theta(Theta, xRow):
            return self.sigmoid(np.dot(np.transpose(Theta), xRow))
        weight = np.array(weight)
        hyp = np.zeros((X.shape[0],1))
        for i in range(0, hyp.shape[0]):
            hyp[i] = lrHypothesis_Theta(weight, X[i])
        Gradient = np.zeros((weight.shape[0],1))
        for j in range(0, Gradient.shape[0]):
            reg = regLambda*weight[j]
            subt = np.subtract(hyp, Y)
            mult = np.transpose(subt)[0]*X[:,j]
            Gradient[j] = np.sum(mult, axis=0) + reg
        Gradient[0] = Gradient[0] - (regLambda*weight[0])
        return Gradient
def calculateGradient ( self , weight , X , Y , regLambda ) : def lrHypothesis_Theta ( Theta , xRow ) : return self . sigmoid ( np . dot ( np . transpose ( Theta ) , xRow ) ) weight = np . array ( weight ) hyp = np . zeros ( ( X . shape [ 0 ] , 1 ) ) for i in range ( 0 , hyp . shape [ 0 ] ) : hyp [ i ] = lrHypothesis_Theta ( weight , X [ i ] ) Gradient = np . zeros ( ( weight . shape [ 0 ] , 1 ) ) for j in range ( 0 , Gradient . shape [ 0 ] ) : reg = regLambda * weight [ j ] subt = np . subtract ( hyp , Y ) mult = np . transpose ( subt ) [ 0 ] * X [ : , j ] Gradient [ j ] = np . sum ( mult , axis = 0 ) + reg Gradient [ 0 ] = Gradient [ 0 ] - ( regLambda * weight [ 0 ] ) return Gradient
Computes the gradient of the objective function
Arguments:
Computes the gradient of the objective function Arguments :
'''
        Computes the gradient of the objective function
        Arguments:
        
            X is a n-by-(d+1) numpy matrix
            Y is an n-by-1 dimensional numpy matrix
            weight is (d+1)-by-1 dimensional numpy matrix
            regLambda is the scalar regularization constant
        Returns:
            the gradient, an (d+1)-by-1 dimensional numpy matrix
        ''' #Takes in the weight matrix # To use np.dot, make sure that you are multiplying matrices - and for that, make sure that xRow is a matrix, and not an array #may have to try np.dot(Theta, xRow) # Make Array filled with LogReg Hypothesis function, so we don't compute it every time
self
weight
X
Y
regLambda