Question: Python I have this program to calculate linear regression using gradient decent. Everything works but I'm stuck trying to find prediction for the number 1650.

Python

I have this program to calculate linear regression using gradient decent. Everything works but I'm stuck trying to find prediction for the number 1650. I have to normalize the number before I can matrix multiple it. Something like this but it has to be norm print(np.matmul([1,1650],[theta0,theta1])) How would you normalize one number? I'm probably over thinking this.

New idea: would this work?: newxs = (1650-mx)/sdx

code:

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

x = pd.read_csv('ex3x.csv')

x = x.iloc[:,0].values

mx = np.mean(x)

sdx = np.std(x)

x = (x-mx)/sdx

y = pd.read_csv('ex3y.csv')

y = y.iloc[:,0].values

my = np.mean(y)

sdy = np.std(y)

y = (y-my)/sdy

theta1 = 0

theta0 = 0

L = 0.07 #The learning rate

epochs = 100 #The number of iterations to perform gradient descent

n = float(len(x))

for i in range(epochs):

Y_pred = theta1*x + theta0

D_m = (-2/n)*sum(x*(y-Y_pred))

D_c = (-2/n)*sum(y-Y_pred)

theta1 = theta1-L*D_m

theta0 = theta0-L*D_c

print(theta1,theta0)

Y_pred = theta1*x+theta0

plt.scatter(x,y)

plt.plot([min(x),max(x)], [min(Y_pred),max(Y_pred)], color='red')

plt.show()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!