0% found this document useful (0 votes)
2 views1 page

Linear Reg From Scratch Codes

The document contains a Python script that performs linear regression analysis on salary data based on years of experience. It defines functions for calculating the regression coefficients, correlation coefficient, and making predictions. The script reads data from a CSV file, computes the regression line, and prints the results including the goodness of fit.

Uploaded by

Dr Sumathy V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Linear Reg From Scratch Codes

The document contains a Python script that performs linear regression analysis on salary data based on years of experience. It defines functions for calculating the regression coefficients, correlation coefficient, and making predictions. The script reads data from a CSV file, computes the regression line, and prints the results including the goodness of fit.

Uploaded by

Dr Sumathy V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

import numpy as np

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('/content/salary data scatrch.csv')


x = data['YearsExperience']
y = data['Salary']

def linear_regression(x, y):


N = len(x)
x_mean = x.mean()
y_mean = y.mean()
B1_num = ((x - x_mean) * (y - y_mean)).sum()
B1_den = ((x - x_mean)**2).sum()
B1 = B1_num / B1_den
B0 = y_mean - (B1*x_mean)
reg_line = 'y = {} + {}x'.format(B0, round(B1, 3))
return (B0, B1, reg_line)
print(reg_line)

def corr_coef(x, y):


N = len(x)

num = (N * (x*y).sum()) - (x.sum() * y.sum())


den = np.sqrt((N * (x**2).sum() - x.sum()**2) * (N * (y**2).sum() -
y.sum()**2))
R = num / den
return R

B0, B1, reg_line = linear_regression(x, y)


print('Regression Line: ', reg_line)
R = corr_coef(x, y)
print('Correlation Coef.: ', R)
print('"Goodness of Fit": ', R**2)

def predict(B0, B1, new_x):


y = B0 + B1 * new_x
return y

You might also like