You are on page 1of 1

from typing import Any, Union

import matplotlib.pyplot as plt


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

mean_x=np.mean(X)
mean_y=np.mean(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=np.max(X)+100
min_x=np.min(X)-100
#Calculating Line values x and y
x=np.linspace(min_x, max_x,1000)
y=b0+b1*x

#ploting
plt.plot(x,y,color='#58b970', label='Regression Line')
#ploting scatter points
plt.scatter(X,Y,c='#ef5423', label='Scatter Plot')
plt.xlabel ('Head Size in cm3')
plt.ylabel ('Brain Weight in grams')
plt.legend()
plt.show()
# 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