Linear and Multilinear Regression
Linear and Multilinear Regression
Date: 12.01.2024
Name: Harisankar R N R
Reg No: 21BRS1524
Lab1: Linear and Multilinear Regression
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) data = pd.read_csv("C:/Users/91812/21BRS1518/headbrain.csv")
#Download the dataset and give the appropriate path print(data.shape) data.head()
(237, 4)
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]:
X=data['Head Size(cm^3)'].values
Y=data['Brain Weight(grams)'].values
mean_x=np.mean(X)
mean_y=np.mean(Y) n=len(X)
#Total number of values 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(b1,b0) #Print
cooefficients
In [3]:
0.26342933948939945 325.57342104944223
In [4]:
0.6393117199570003
In [ ]:
#IMPORTING THE DEPENDENCIES import
numpy as np import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split from
sklearn.linear_model import LinearRegression from sklearn
import metrics import warnings
warnings.filterwarnings('ignore')
In [1]:
Out[4]:
Out[5]:
In [8]:
age 0
Out[8]:
sex 0
bmi 0
children 0
smoker 0
region 0
charges 0
dtype: int64
In [10]:
In [11]:
In [12]:
In [15]:
1 324
2 240
3 157
4 25
5 18
Name: children, dtype: int64
In [17]:
yes 274
Name: smoker, dtype: int64
In [19]:
northwest 325
northeast 324
Name: region, dtype: int64
In [21]:
In [22]:
print(insurance_dataset)
In [23]:
age sex bmi children smoker region charges
0 19 1 27.900 0
0 1 16884.92400
1 18 0 33.770 1
1 0 1725.55230
2 28 0 33.000 3
1 0 4449.46200
3 33 0 22.705 0
1 3 21984.47061 4 32
0 28.880 0
1 3 3866.85520
print(Y)
Out[16]: 0 574
In [26]:
Out[18]: no 1064
1333
1334 2205.98080
1335 1629.83350
1336 2007.94500
1337 29141.36030
print(X)
In [25]:
3
4
...
1333
1334
1335
1336
1337
input_data=(31,1,25.74,0,1,0)
#changing input_data to a numpy array input_data_array=np.asarray(input_data)
In [33]:
In [36]:
[3760.0805765]
The insurance cost is USD 3760.0805764960514
In [ ]: