-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsurance Price Prediction .py
307 lines (141 loc) · 5.38 KB
/
Insurance Price Prediction .py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
sns.set()
# In[2]:
insurance = pd.read_csv('G:\Data Science\Dataset\insurance.csv')
insurance.head()
# In[3]:
# check null value
insurance.isnull().sum()
# In[4]:
# check datatype for each column
insurance.info()
# In[5]:
insurance.describe()
# In[6]:
insurance.shape
# ### Create dummy variables for object datatype
# In[7]:
insurance_dum = pd.get_dummies(insurance, columns = ['sex', 'smoker', 'region'], drop_first = True)
# In[8]:
# re-order the columns
columns = ['age', 'bmi', 'children', 'sex_male', 'smoker_yes', 'region_northwest', 'region_southeast', 'region_southwest', 'charges']
insurance_dum = insurance_dum.reindex(columns = columns)
# In[9]:
insurance_dum.head()
# ### Exploratory Data Analysis
# In[10]:
sns.pairplot(insurance_dum)
# In[11]:
sns.distplot(insurance_dum['charges'])
print('Mean of charges is', insurance_dum['charges'].mean())
# In[12]:
plt.figure(figsize=(18,7))
sns.heatmap(insurance_dum.corr(), annot = True, cmap='coolwarm')
# ### Linear Regression
# In[13]:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# In[14]:
insurance_x = insurance_dum.drop(['charges'], 1)
insurance_y = insurance_dum['charges']
# In[15]:
print(insurance_x.shape)
insurance_x.head()
# In[16]:
insurance_y.head()
# In[17]:
X_train, X_test, y_train, y_test = train_test_split(insurance_x, insurance_y, test_size = 0.2, random_state = 123)
print('X_train shape is', X_train.shape)
print('X_test shape is', X_test.shape)
print('y_train shape is', y_train.shape)
print('y_test shape is', y_test.shape)
# In[18]:
lr = LinearRegression()
lr.fit(X_train, y_train)
# In[19]:
print('The intercept is', lr.intercept_)
# In[20]:
print(lr.coef_)
# In[21]:
linear_coef_df = pd.DataFrame(lr.coef_, index = X_train.columns, columns = ['linear_coef'])
linear_coef_df
# In[22]:
# charges prediction
ypred_linear = pd.DataFrame(lr.predict(X_test), columns = ['y_pred_lin'])
# In[23]:
plt.scatter(y_test, ypred_linear)
# #### Evaluation Metrics
# In[24]:
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
# In[25]:
print(f'Mean Absolute Error: {mean_absolute_error(ypred_linear, y_test)}')
print(f'Mean Squared Error: {mean_squared_error(ypred_linear, y_test)}')
print(f'Root Mean Absolute Error: {np.sqrt(mean_absolute_error(ypred_linear, y_test))}')
print(f'R_squared: {r2_score(ypred_linear, y_test)}')
# ### Let's compare to other regression models
# In[26]:
from sklearn import linear_model
# In[27]:
# Lasso Regression
lm_lasso = linear_model.Lasso()
# Ridge Regression
lm_ridge = linear_model.Ridge()
# Elastic Net Regression
lm_elastic = linear_model.ElasticNet()
# In[28]:
lm_lasso.fit(X_train, y_train)
lm_ridge.fit(X_train, y_train)
lm_elastic.fit(X_train, y_train)
# In[29]:
# Return the intercept for each model
print(f'Lasso model intercept: {lm_lasso.intercept_}')
print(f'Ridge model intercept: {lm_ridge.intercept_}')
print(f'Elastic Net model intercept: {lm_elastic.intercept_}')
# In[30]:
# Return coefficient for each model
print(f'Lasso model coefficient: {lm_lasso.coef_}')
print(f'Ridge model coefficient: {lm_ridge.coef_}')
print(f'Elastic Net model coefficient: {lm_elastic.coef_}')
# In[31]:
lasso_coef_df = pd.DataFrame(lm_lasso.coef_, index = X_train.columns, columns = ['lasso_coef'])
lasso_coef_df
# In[32]:
ridge_coef_df = pd.DataFrame(lm_ridge.coef_, index = X_train.columns, columns = ['ridge_coef'])
ridge_coef_df
# In[33]:
elastic_coef_df = pd.DataFrame(lm_elastic.coef_, index = X_train.columns, columns = ['elastic_coef'])
elastic_coef_df
# In[34]:
coefmod_df = pd.concat([linear_coef_df, lasso_coef_df, ridge_coef_df, elastic_coef_df], axis=1)
coefmod_df
# In[35]:
# charges prediction
ypred_lasso = pd.DataFrame(lm_lasso.predict(X_test), columns = ['y_lasso_pred'])
ypred_ridge = pd.DataFrame(lm_ridge.predict(X_test), columns = ['y_ridge_pred'])
ypred_elastic = pd.DataFrame(lm_elastic.predict(X_test), columns = ['y_elastic_pred'])
ypred_df = pd.concat([ypred_linear, ypred_lasso, ypred_ridge, ypred_elastic], axis=1)
ypred_df.head()
# In[36]:
print(f'Linear Mean Absolute Error: {mean_absolute_error(ypred_linear, y_test)}')
print(f'Lasso Mean Absolute Error: {mean_absolute_error(ypred_lasso, y_test)}')
print(f'Ridge Mean Absolute Error: {mean_absolute_error(ypred_ridge, y_test)}')
print(f'Elastic Net Absolute Error: {mean_absolute_error(ypred_elastic, y_test)}')
# In[37]:
print(f'Linear Mean Squared Error: {mean_squared_error(ypred_linear, y_test)}')
print(f'Lasso Mean Squared Error: {mean_squared_error(ypred_lasso, y_test)}')
print(f'Ridge Mean Squared Error: {mean_squared_error(ypred_ridge, y_test)}')
print(f'Elastic Net Mean Squared Error: {mean_squared_error(ypred_elastic, y_test)}')
# In[38]:
print(f'Linear Root Mean Absolute Error: {np.sqrt(mean_absolute_error(ypred_linear, y_test))}')
print(f'Lasso Root Mean Absolute Error: {np.sqrt(mean_absolute_error(ypred_lasso, y_test))}')
print(f'Ridge Root Mean Absolute Error: {np.sqrt(mean_absolute_error(ypred_ridge, y_test))}')
print(f'Elastic Net Root Mean Absolute Error: {np.sqrt(mean_absolute_error(ypred_elastic, y_test))}')
# In[39]:
# INSERT COMMENT HERE