text_plain_output_1.png
import math

import pandas as pd

data = pd.read_csv('/kaggle/input/salary-dataset-simple-linear-regression/Salary_dataset.csv')

data.isnull().sum()

data.isnull().sum().to_frame().rename(columns={0: 'Total No. of Missing Values'})

x = data.YearsExperience
y = data.Salary

w = 9909
b = 21641.1797

def linearRegression(x, w, b):
    pre_y = []
    m = len(x)
    for i in range(m):
        f_wb = w * x[i] + b
        pre_y.append(f_wb)
    return pre_y

y_pred = linearRegression(x, w, b)

def SquaredErrorCost(y_pred, y):
    totalCost = 0
    m = len(y_pred)
    for i in range(m):
        cost = (y_pred[i] - y[i]) ** 2
        totalCost += cost
    totalCost /= 2 * m
    return totalCost

def compute_cost(x, y, w, b):
    m = x.shape[0]
    cost = 0
    for i in range(m):
        f_wb = w * x[i] + b
        cost = cost + (f_wb - y[i]) ** 2
    total_cost = 1 / (2 * m) * cost
    return total_cost

def compute_gradient(x, y, w, b):
    m = len(x)
    dj_dw = w
    dj_db = b
    for i in range(m):
        f_wb = w * x[i] + b
        dj_dw_i = (f_wb - y[i]) * x[i]
        dj_db_i = f_wb - y[i]
        dj_dw += dj_dw_i
        dj_db += dj_db_i
    dj_dw /= m
    dj_db /= m
    return (dj_dw, dj_db)

def gradient_descent(x, y, w_in, b_in, alpha, num_iters, cost_function, gradient_function):
    """
    Performs gradient descent to fit w,b. Updates w,b by taking 
    num_iters gradient steps with learning rate alpha
    Args:
      x (ndarray (m,))  : Data, m examples 
      y (ndarray (m,))  : target values
      w_in,b_in (scalar): initial values of model parameters  
      alpha (float):     Learning rate
      num_iters (int):   number of iterations to run gradient descent
      cost_function:     function to call to produce cost
      gradient_function: function to call to produce gradient
    Returns:
      w (scalar): Updated value of parameter after running gradient descent
      b (scalar): Updated value of parameter after running gradient descent
      J_history (List): History of cost values
      p_history (list): History of parameters [w,b] 
      """
    J_history = []
    p_history = []
    b = b_in
    w = w_in
    for i in range(num_iters):
        dj_dw, dj_db = gradient_function(x, y, w, b)
        b = b - alpha * dj_db
        w = w - alpha * dj_dw
        if i < 100000:
            J_history.append(cost_function(x, y, w, b))
            p_history.append([w, b])
    return (w, b, J_history, p_history)

w_init = 0
b_init = 0
iterations = 10000
tmp_alpha = 0.01
w_final, b_final, J_hist, p_hist = gradient_descent(x, y, w_init, b_init, tmp_alpha, iterations, compute_cost, compute_gradient)
print(f'(w,b) found by gradient descent: ({w_final:8.4f},{b_final:8.4f})')