Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Decorators: I need help with decorating class RidgeRegession with functions sgd and predict to get rid of lines of code in the class Ridge

Python Decorators:

I need help with decorating class RidgeRegession with functions sgd and predict to get rid of lines of code in the class Ridge Regression(Bolded).

class RidgeRegression() : def __init__( self, learning_rate, iterations, l2_penality ) : self.learning_rate = learning_rate self.iterations = iterations self.l2_penality = l2_penality # Function for model training def fit( self, X, Y ) : # no_of_training_examples, no_of_features self.m, self.n = X.shape # weight initialization self.W = np.zeros( self.n ) self.b = 0 self.X = X self.Y = Y  # gradient descent learning for i in range( self.iterations ) : self.update_weights() return self # Helper function to update weights in gradient descent def update_weights( self ) : Y_pred = self.predict( self.X ) # calculate gradients dW = ( - ( 2 * ( self.X.T ).dot( self.Y - Y_pred ) ) + ( 2 * self.l2_penality * self.W ) ) / self.m db = - 2 * np.sum( self.Y - Y_pred ) / self.m # update weights self.W = self.W - self.learning_rate * dW self.b = self.b - self.learning_rate * db return self # Hypothetical function h( x ) def predict( self, X ) : return X.dot( self.W ) + self.b 
def sgd(X, y, learning_rate=0.3, n_epochs=1000, k=30): w = np.random.randn(1,13) b = np.random.randn(1,1) epoch=1 while epoch <= n_epochs: temp = X.sample(k) X_tr = temp.iloc[:,0:13].values y_tr = temp.iloc[:,-1].values Lw = w Lb = b loss = 0 y_predicted = [] least_loss = [] for i in range(k): Lw = (-2/k * X_tr[i]) * (y_tr[i] - np.dot(X_tr[i],w.T) - b) Lb = (-2/k) * (y_tr[i] - np.dot(X_tr[i],w.T) - b) w = w - learning_rate * Lw b = b - learning_rate * Lb y_pred = np.dot(X_tr[i],w.T) y_predicted.append(y_pred) loss = mean_squared_error(y_predicted, y_tr) epoch+=1 learning_rate = learning_rate/1.03 return w,b def predict_using_sgd(x,w,b): y_predicted=[] for i in range(len(x)): tmp = x X_test = tmp.iloc[:,0:13].values y = np.asscalar(np.dot(w,X_test[i])+b) y_predicted.append(y) return np.array(y_predicted) w,b = sgd(X_train,y_train) y_predicted_sgd = predict_using_sgd(X_test,w,b)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4. What sales experience have you had?

Answered: 1 week ago