image_output_1.png
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

cost = SquaredErrorCost(y_pred, y)
print(' Squared Error Cost :', cost)
print(f' Squared Error Cost : {cost:10}')
print('Squared Error Cost: {:.5e}'.format(cost))