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

Linear Regression with Head Size Data

This document reads in a CSV dataset containing head size and brain weight data. It calculates the linear regression line and coefficients to model the relationship between head size and brain weight. Several key steps are shown, including calculating the mean of X and Y, computing the b1 and b0 coefficients, plotting the regression line and data points on a graph, and calculating the r-squared value to evaluate the model fit.

Uploaded by

ochin
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)
65 views1 page

Linear Regression with Head Size Data

This document reads in a CSV dataset containing head size and brain weight data. It calculates the linear regression line and coefficients to model the relationship between head size and brain weight. Several key steps are shown, including calculating the mean of X and Y, computing the b1 and b0 coefficients, plotting the regression line and data points on a graph, and calculating the r-squared value to evaluate the model fit.

Uploaded by

ochin
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

from typing import Any, Union

import [Link] as plt


import numpy as np
import pandas as pd
from pandas import DataFrame
from [Link] import TextFileReader
data = pd.read_csv("C:\\Users\\ochin\\OneDrive\\Desktop\\Computer Backup\\ML
8CCA\ML Prog\\[Link]") # type: Union[Union[TextFileReader, DataFrame], Any]
print([Link])
print([Link]())
X=data['Head Size(cm^3)'].values
Y=data['Brain Weight(grams)'].values
### y=mx+c or y= b1x+b0

mean_x=[Link](X)
mean_y=[Link](Y)
#total no of items
m=len(X)
#use the formula to calculate b0 and b1

num=0
denom=0

for i in range (m):


num +=(X[i]-mean_x) * (Y [i] - mean_y)
denom+= (X[i]-mean_x) **2
b1=num/denom
b0= mean_y-(b1*mean_x)

#print coefficient
print("coefficient b1 = ",b1,"Coefficient b0 = ", b0)
# we get b1=0.263 and b0=325.573

# plot graphically
max_x=[Link](X)+100
min_x=[Link](X)-100
#Calculating Line values x and y
x=[Link](min_x, max_x,1000)
y=b0+b1*x

#ploting
[Link](x,y,color='#58b970', label='Regression Line')
#ploting scatter points
[Link](X,Y,c='#ef5423', label='Scatter Plot')
[Link] ('Head Size in cm3')
[Link] ('Brain Weight in grams')
[Link]()
[Link]()
# Calculate R square value

ss_t=0 #total sum of square


ss_r=0 #total sum of residual
for i in range(m):
y_pred=b0+b1* X[i]
ss_t+=(Y[i]-mean_y) **2
ss_r+=(Y[i]-y_pred) **2
r2=1-(ss_r/ss_t)
print("r square value = ",r2)

You might also like