I implement multiple linear regression from scratch but I did not find slope and intercept, gradient decent give me nan value.
Here is my code and I also give ipython notebook file.
https://drive.google.com/file/d/1NMUNL28czJsmoxfgeCMu3KLQUiBGiX1F/view?usp=sharing
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = np.array([[ 1, 2104, 3],
[ 1, 1600, 3],
[ 1, 2400, 3],
[ 1, 1416, 2],
[ 1, 3000, 4],
[ 1, 1985, 4]])
y = np.array([399900, 329900, 369000, 232000, 539900, 299900])
def gradient_runner(x, y, altha, b, theta1, theta2):
initial_m1 = 0
initial_m2 = 0
initial_b = 0
N = len(x)
for i in range(0, len(y)):
x0 = x[i, 0]
x1 = x[i, 1]
x2 = x[i, 2]
yi = y[i]
h_theta = (theta1 * x1 + theta2 * x2 + b)
initial_b += -(1/N) * x0 * (yi - h_theta)
initial_m1 += -(1/N) * x1 * (yi - h_theta)
initial_m2 += -(1/N) * x2 * (yi - h_theta)
new_b = b - (altha * initial_b)
new_m1 = theta1 - (altha * initial_m1)
new_m2 = theta2 - (altha * initial_m2)
return new_b, new_m1, new_m2
def fit(x, y, alpha, iteration, b, m1, m2):
for i in range(0, iteration):
b, m1, m2 = gradient_runner(x, y, alpha, b, m1, m2)
return b, m1, m2
fit(x,y, 0.001, 1500, 0,0,0)