Linear Regression - Ipynb - Colab
Linear Regression - Ipynb - Colab
ipynb - Colab
# prepare a data
# randomly set 10 pieces of data with linear relationship
import numpy as np
import matplotlib.pyplot as plt
<matplotlib.collections.PathCollection at 0x7ad4ebcef640>
#define functions
#model function - linear regression model mx+b
def model(a,b,x):
return a*x+b
#loss function - MSE
def loss_function(a,b,x,y):
num = len(x)
predict = model(a,b,x)
return(0.5/num)*(np.square(predict-y)).sum()
#optimization function - calculates the partial derivatives of m and b
#by using the gradient descent
def optimize(a,b,x,y):
num = len(x)
predict = model(a,b,x)
da = (1.0/num)*((predict-y)*x).sum()
db = (1.0/num)*((predict-y).sum())
a = a - Lr*da
b = b - Lr*db
return a,b
def iterate(a,b,x,y,frequency):
for i in range(frequency):
a,b = optimize(a,b,x,y)
return a,b
https://colab.research.google.com/drive/1p79l-G_imy4AzFx4ePZ_JsoXRvE9qZm3?authuser=1#scrollTo=y0gJsrVflxh3&printMode=true 1/3
9/24/24, 2:17 PM Linear Regression.ipynb - Colab
keyboard_arrow_down Q1: Must the loss value return to zero when the raw data is modified?
https://colab.research.google.com/drive/1p79l-G_imy4AzFx4ePZ_JsoXRvE9qZm3?authuser=1#scrollTo=y0gJsrVflxh3&printMode=true 2/3
9/24/24, 2:17 PM Linear Regression.ipynb - Colab
A: No, modifying raw data will not guarantee that the loss will return to zero.
https://colab.research.google.com/drive/1p79l-G_imy4AzFx4ePZ_JsoXRvE9qZm3?authuser=1#scrollTo=y0gJsrVflxh3&printMode=true 3/3