Question

def stochastic_gradient_descent(feature_matrix, label, learning_rate = 0.05, epoch = 1000):
"""
Implement gradient descent algorithm for regression.
  
Args:
feature_matrix - A numpy matrix describing the given data, with ones added as the first column. Each row
represents a single data point.
  
label - The correct value of response variable, corresponding to feature_matrix.
  
learning_rate - the learning rate with default value 0.5
  
epoch - the number of iterations with default value 1000

Returns: A numpy array for the final value of theta
"""
n = len(label)
theta = np.zeros(feature_matrix.shape[1]) # initialize theta to be zero vector

for i in range(epoch):
# your code below
# generate a random integer between 0 and n
  
# compute gradient at this randomly selected feature vector below
  
# update theta below

# compute average squared error or empirical risk or value of cost function
# It is not necessary to comput cost here. But it is common to use cost
# in the termination condition of the loop
  
# your code above
# test
# print(i, theta, cost)
  
return theta
raise NotImplementedErrordef stochastic_gradient_descent(feature_matrix, label, learning_rate = 0.05, epoch 1000): Implement gradient descent algorith

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import numpy as np

feature_matrix=np.array([310,270,350,251,410,308,263])     

mean=feature_matrix.mean()

std=feature_matrix.std()

feature_matrix=(feature_matrix-mean)/std

feature_matrix=np.c_[np.ones(np.size(feature_matrix)),feature_matrix]                         

label=np.array([50,43,46,33,44,40,41])

learning_rate=0.01                                            

m=len(label)

theta=np.random.rand(2)

def Grad_dec(feature_matrix,label,m,learning_rate,theta):

    for i in range(1000):

        p=np.dot(feature_matrix,theta)

        error=p-label

        cost=sum(error**2)/(2*m)

        

        theta=theta-(learning_rate/m)*np.dot(feature_matrix.T,error)

    return cost,theta

cost,theta=Grad_dec(feature_matrix,label,m,learning_rate,theta)

print(theta)

test_data=int(input('Enter test value:'))

test_data=(test_data-mean)/std

test_data=np.array([1,test_data])

print(round(np.dot(test_data.T,theta),4))

File Edit Selection View Go Run Terminal Help p4.py - HTML1 - Visual Studio Code х EXPLORER e amir5.py p1.py p4.py х sales.tx

Add a comment
Know the answer?
Add Answer to:
def stochastic_gradient_descent(feature_matrix, label, learning_rate = 0.05, epoch = 1000): """ Implement gradient descent algorithm for regression....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT