0% found this document useful (0 votes)
76 views3 pages

Linear Regration

This document contains code to analyze the relationship between head size and brain weight using linear regression. It reads in data, calculates the linear regression coefficients B1 and B0, and plots the regression line along with scatter points of the original data. B1 is calculated as 0.2634 and B0 as 325.57. A line is fitted to the data from the minimum to maximum X value and overlaid with the scatter points on a plot with head size on the x-axis and brain weight on the y-axis.

Uploaded by

Govinda Kankal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views3 pages

Linear Regration

This document contains code to analyze the relationship between head size and brain weight using linear regression. It reads in data, calculates the linear regression coefficients B1 and B0, and plots the regression line along with scatter points of the original data. B1 is calculated as 0.2634 and B0 as 325.57. A line is fitted to the data from the minimum to maximum X value and overlaid with the scatter points on a plot with head size on the x-axis and brain weight on the y-axis.

Uploaded by

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

7/24/2019 Untitled7

In [1]: %matplotlib inline


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize']=(20.0,10.0)
#Reading data
data=pd.read_csv('headbrain.csv')
print (data.shape)
data.head()

(237, 4)

Out[1]:
Gender Age Range Head Size(cm^3) Brain Weight(grams)

0 1 1 4512 1530

1 1 1 3738 1297

2 1 1 4261 1335

3 1 1 3777 1282

4 1 1 4177 1590

In [2]: %matplotlib inline


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize']=(20.0,10.0)
#Reading data
data=pd.read_csv('headbrain.csv')
print (data.shape)
data.head()
#collecting X and Y
X=data['Head Size(cm^3)'].values
Y=data['Brain Weight(grams)'].values
#mean X and Y
mean_x = np.mean(X)
mean_y = np.mean(Y)
# total number of values
n = len(X)
# using the formula calculate B0 and B1
numer = 0
denom = 0
for i in range(n):
numer += (X[i] - mean_x) * (Y[i] - mean_y)
denom += (X[i] - mean_x) ** 2
B1 = numer/denom
B0 = mean_y - B1 * mean_x
#Print coefficient
print(B1,B0)

(237, 4)
0.26342933948939945 325.57342104944223

localhost:8888/notebooks/Untitled7.ipynb?kernel_name=python3 1/3
7/24/2019 Untitled7

In [3]: %matplotlib inline


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize']=(20.0,10.0)
#Reading data
data=pd.read_csv('headbrain.csv')
print (data.shape)
data.head()
#collecting X and Y
X=data['Head Size(cm^3)'].values
Y=data['Brain Weight(grams)'].values
#mean X and Y
mean_x = np.mean(X)
mean_y = np.mean(Y)
# total number of values
n = len(X)
# using the formula calculate B0 and B1
numer = 0
denom = 0
for i in range(n):
numer += (X[i] - mean_x) * (Y[i] - mean_y)
denom += (X[i] - mean_x) ** 2
B1 = numer/denom
B0 = mean_y - B1 * mean_x
#Print coefficient
print(B1,B0)

#Ploting values and regression Line


max_x = np.max(X) + 100
min_x = np.min(X) - 100
#calculating line values X and Y
X1 = np.linspace(max_x, min_x, 1000)
Y1 = B0 + B1 * X1
#plot X and Y
plt.plot(X1,Y1,color='blue',label='Regression Line')

#plotting scatter point


plt.scatter(X, Y, c='Red', label='Scatter point')
plt.xlabel('Head Size(cm^3)')
plt.ylabel('Brain Weight(grams)')
plt.legend(loc='upper left')
plt.show()

(237, 4)
0.26342933948939945 325.57342104944223

localhost:8888/notebooks/Untitled7.ipynb?kernel_name=python3 2/3
7/24/2019 Untitled7

In [ ]:

localhost:8888/notebooks/Untitled7.ipynb?kernel_name=python3 3/3

You might also like