67 lines
1.6 KiB
Python
67 lines
1.6 KiB
Python
|
import random
|
||
|
import numpy as np
|
||
|
from numpy import arange
|
||
|
import matplotlib.pyplot as plt
|
||
|
|
||
|
random.seed(0)
|
||
|
|
||
|
def line_w_gauss_noise(inter,slope,noise,numPoints):
|
||
|
x = np.zeros(shape=(numPoints, 2))
|
||
|
y = np.zeros(shape=numPoints)
|
||
|
for i in range(0, numPoints):
|
||
|
x[i][0] = i
|
||
|
x[i][1] = 1
|
||
|
y[i] = (i + inter) + random.uniform(0, noise) * slope
|
||
|
return x, y
|
||
|
|
||
|
|
||
|
def MSE(x,y,weights):
|
||
|
N,D = np.shape(x)
|
||
|
x_T = x.transpose()
|
||
|
y_hat = np.dot(weights,x_T)
|
||
|
return np.sum((y - y_hat)**2) / N
|
||
|
|
||
|
|
||
|
def dMSE(x,y,weights):
|
||
|
N = len(x)
|
||
|
inter = np.sum(np.dot(np.dot(x[:,1],weights[1]) - y,x[:,1])) * (2/N)
|
||
|
slope = np.sum(np.dot(x[:,0],weights[0]) - y,) * (2/N)
|
||
|
new_weight = np.array([slope,inter])
|
||
|
return new_weight
|
||
|
|
||
|
def gradient_desscent(x,y,alpha,weights,iter):
|
||
|
losses = list()
|
||
|
costs = list()
|
||
|
for n in range(iter):
|
||
|
cost = MSE(x,y,weights)
|
||
|
loss = np.sum(y - np.dot(x,weights))
|
||
|
losses.append(loss)
|
||
|
costs.append(cost)
|
||
|
print(cost)
|
||
|
print(loss)
|
||
|
print(n)
|
||
|
if np.abs(losses[n]) > np.abs(losses[n-1]):
|
||
|
break
|
||
|
|
||
|
weights = weights - (alpha * dMSE(x,y,weights))
|
||
|
return weights,costs,losses
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
x,y = line_w_gauss_noise(1,2,5,100)
|
||
|
num_var = len(x.transpose())
|
||
|
w = np.ones(num_var)
|
||
|
|
||
|
result,cost,loss = gradient_desscent(x,y,0.001,w,80)
|
||
|
print(result)
|
||
|
y_hat = []
|
||
|
for i in x[:,0]:
|
||
|
y_hat.append(result[1]+(result[0]*i))
|
||
|
|
||
|
plt.scatter(x[:,0],y)
|
||
|
plt.plot(x[:,0],y_hat)
|
||
|
plt.show()
|
||
|
|
||
|
#plt.plot(cost)
|
||
|
#plt.plot(loss)
|
||
|
#plt.show()
|