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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started