-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProject1.py
More file actions
734 lines (312 loc) · 13 KB
/
Project1.py
File metadata and controls
734 lines (312 loc) · 13 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
# In[2]:
insurance = pd.read_csv("data.csv")
insurance
# In[3]:
insurance.info()
# In[3]:
insurance.head()
# In[8]:
insurance.describe()
# We can infer somethings by observing the above data:-
# 1. The min age is 18 and max age in 64 so maybe the company doesn't consider the people for insurance having age less the 18 or more than 64.
# 2. bmi tells us that there are wide range of people from severely malnutrioned to severe obese people.
# 3. Min and Max charges vary alot. Even the price difference between 75 percentile nad the max price is very high.
# In[5]:
import plotly.express as px
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
# In[9]:
insurance.hist(bins = 50,figsize = (20,20))
# In[10]:
graph = px.histogram(insurance,x='charges',marginal = 'box',nbins = 50)
graph.update_layout(bargap = 0.1)
graph.show()
# In[11]:
insurance.charges.describe()
# In[12]:
insurance.age.describe()
# In[6]:
graph = px.histogram(insurance,x='age',marginal = 'box',nbins = 50)
graph.update_layout(bargap = 0.1)
graph.show()
# We can see that the distribution of age is fairly uniform except for the ages 18 and 19. The reason has not been mentioned but may be the company is offering the people between ages 18 and 19 something extra than the older people.
# In[13]:
insurance.bmi.describe()
# In[14]:
graph = px.histogram(insurance,x='bmi',marginal = 'box',nbins = 50)
graph.update_layout(bargap = 0.1)
graph.show()
# This looks like a gausian or normal distribution
# In[16]:
graph = px.histogram(insurance,x='charges',marginal = 'box',color = 'smoker',nbins = 50)
graph.update_layout(bargap = 0.1)
graph.show()
# In[17]:
graph = px.histogram(insurance,x='charges',color = 'region',marginal = 'box',nbins = 50)
graph.update_layout(bargap = 0.1)
graph.show()
# From the above two graphs:
# 1. Non smokers pay lesser bill than the smokers
# 2. Region doesn't have and clear affect on the medical bills.
# In[19]:
px.violin(insurance,x='children',y='charges')
# There is a week increasing trend in the charges as the number of children increases.
# Age,smoker,bmi seems to be the factors most affecting the medical bills
# In[20]:
px.scatter(insurance,x='age',y='charges',color = 'smoker')
# In[21]:
px.scatter(insurance,x='bmi',y='charges',color = 'smoker')
# In the scatter plot of charges with age; we can clearly see 3 distinct linear trends; bottom most being the non smoker region and the above two are smoker region.
#
# In scatter plot of charges with bmi; there is no specific trend but again we see three distinct regions.
#
# Thus we may infer that in the scatter plot of charges with age; the bottomost linear trend is of the non smoker; the middle one is of the smoker with lesser bmi(less than 30 approx) and the top one is the smoker with more bmi(more than 30 approx)
# In[24]:
insurance.corr()
# In[26]:
px.imshow(insurance.corr(),text_auto = True)
# By the above heatmap it is clear that the correlation of charges with age is the highest.
# Now to train my model I will divide my datasets into 3 categories:
# 1. Non smokers
# 2. Smokers with bmi over 30
# 3. Smoker with bmi lesser than 30
# I will we using the strategy of Ordinary Least Squares(LinearRegression)
#
# I will compute the loss using root mean squared value.
# Loss lesser than 5000-6000 will be optimum.
# In[27]:
def estimate_charges(age,w,b):
return w*age+b
# In[28]:
non_smoker_df = insurance[insurance.smoker == 'no']
# In[29]:
import numpy as np
# In[30]:
def rmse(targets,predictions):
return np.sqrt(np.mean(np.square(targets-predictions)))
# In[31]:
def try_parameters(w,b):
ages = non_smoker_df.age
target = non_smoker_df.charges
predictions = estimate_charges(ages,w,b)
plt.plot(ages,predictions,'r');
plt.scatter(ages,target);
plt.xlabel('Age');
plt.ylabel('Charges')
plt.legend(['Prediction','Actual']);
loss = rmse(target,predictions)
print("RMSE Loss: ",loss)
# In[32]:
try_parameters(300,-5000)
# # Linear Regression using Scikit-learn
# Scikit-learn provides us with ready made libraries to implement strategies like Ordinary Least Squares or Stochastic gradient descent.
#
# In[33]:
from sklearn.linear_model import LinearRegression
# In[34]:
model = LinearRegression()
# In[40]:
from sklearn.model_selection import train_test_split
# In[41]:
inputs_nonsmoker = non_smoker_df[['age']]
targets_nonsmoker = non_smoker_df.charges
inputs,inputs_test,targets,targets_test = train_test_split(inputs_nonsmoker,targets_nonsmoker,test_size = 0.1)
# In[42]:
model.fit(inputs,targets)
# In[59]:
predictions = model.predict(inputs_test)
# In[60]:
predictions
# In[61]:
rmse(targets_test,predictions)
# This rmse loss is the lowest till now. Plus there are outliers which effects the loss considerably.
# In[46]:
model.coef_ #weight
# In[47]:
model.intercept_ #bias
# In[48]:
try_parameters(model.coef_,model.intercept_)
# Till now we worked with the data having just the non smokers. Let's work with the data containing the smokers.
# In[50]:
smokers_df = insurance[insurance.smoker == 'yes']
# In[51]:
smokers_df
# In[32]:
ages = smokers_df.age
target = smokers_df.charges
# In[52]:
px.scatter(smokers_df,x='age',y='charges')
# Unlike non smokers data here we can clearly see distribution of smokers into two distinct lines. Lets include the third factor which is actually affecting the dependecies of smokers and their medical bills.
# In[53]:
px.scatter(insurance,x = 'bmi',y = 'charges',color = 'smoker')
# In[54]:
high_smoker = smokers_df[smokers_df.bmi >= 30]
low_smoker = smokers_df[smokers_df.bmi < 30]
# In[55]:
px.scatter(high_smoker,x='age',y='charges')
# In[56]:
px.scatter(low_smoker,x='age',y='charges')
# Clearly we seprated the curves of two linear relations and now we can work on them separately.
# In[63]:
inputs_smokerhigh = high_smoker[['age']]
targets_smokerhigh = high_smoker['charges']
inputs1,inputs_test_high,targets1,targets_test_high = train_test_split(inputs_smokerhigh,targets_smokerhigh,test_size = 0.1)
# In[64]:
model = LinearRegression()
# In[66]:
model.fit(inputs1,targets1)
# In[67]:
targets.describe()
# In[68]:
predictions1 = model.predict(inputs_test_high)
# In[69]:
predictions1
# In[70]:
rmse(predictions1,targets_test_high)
# In[71]:
model.coef_
# In[72]:
model.intercept_
# In[73]:
inputs_lowsmoker = low_smoker[['age']]
targets_lowsmoker = low_smoker['charges']
inputs2,inputs_test_low,targets2,targets_test_low = train_test_split(inputs_lowsmoker,targets_lowsmoker,test_size = 0.1)
# In[74]:
model.fit(inputs2,targets2)
# In[75]:
predictions2 = model.predict(inputs_test_low)
# In[76]:
predictions2
# In[77]:
rmse(predictions2,targets_test_low)
# In[78]:
model.coef_
# In[79]:
model.intercept_
# We can combine the above models to find how our model is performing for the smokers
# In[83]:
smokers_predictions = list(predictions1)+list(predictions2) #this is complete list of predictions for smokers but in increasing trend of bmi
# In[84]:
smokers_predictions = np.array(smokers_predictions)
# In[85]:
smokers_predictions
# In[88]:
targets = pd.Series(list(targets_test_high)+list(targets_test_low))
# In[89]:
rmse(targets,smokers_predictions)
# We created decent models for smokers and non smokers to calculate their annual medical bills.
# Now considering the entire dataset instead of making the distinction between the smokers and non smokers.
# In[90]:
inputs,targets = insurance[['age','bmi','children']],insurance['charges']
model = LinearRegression().fit(inputs,targets)
predictions = model.predict(inputs)
rmse(predictions,targets)
# The model is pretty bad; we analysed the reason pretty well earlier.
# # Using Categorical column in our machine learning model
# For using categorical columns in our machine learning model we would want to make it to a numeric data out of it first.
# Three ways of doing this is:-
# 1. if a categorical column has just two categories then we can replace their vallues with 0 and 1.
# 2. if a categorical colunn has more than two categories, we can preform one-hot encoding i.e. create a new column for each category with 1s and 0s.
# 3. if the categoreis have a natural order(eg. cold,neutral,warm,hot) then we can be converted to numbers directly based on their weights. These are called ordinals.
# In[91]:
sns.barplot(data = insurance,x = 'smoker',y = 'charges');
# The above graph shows avg charge for smoker and non smoker respectively.
# In[92]:
type(insurance)
# In[93]:
smokers_codes = {'no':0,'yes':1}
# In[94]:
insurance['smokers_codes'] = insurance.smoker.map(smokers_codes)
# In[95]:
insurance
# In[96]:
insurance.corr()
# smokers_codes correlation with charges is quite high.
# In[97]:
inputs,targets = insurance[['age','bmi','children','smokers_codes']],insurance['charges']
model = LinearRegression().fit(inputs,targets)
predictions = model.predict(inputs)
rmse(predictions,targets)
# 50% reduction of loss!!!
# In[98]:
sns.barplot(data = insurance,x='sex',y='charges');
# In[99]:
sex_codes = {'female':0,'male':1}
# In[100]:
insurance['sex_codes'] = insurance.sex.map(sex_codes)
# In[101]:
insurance.corr()
# In[102]:
inputs,targets = insurance[['age','bmi','children','smokers_codes','sex_codes']],insurance['charges']
model = LinearRegression().fit(inputs,targets)
predictions = model.predict(inputs)
rmse(predictions,targets)
# We will be using one hot encoding for the region columns as of course i can't think of any specific order of weightage to give to certain regions.
# In[103]:
sns.barplot(data = insurance,x='region',y='charges');
# In[104]:
from sklearn import preprocessing
enc = preprocessing.OneHotEncoder()
enc.fit(insurance[['region']])
enc.categories_
# In[105]:
one_hot = enc.transform(insurance[['region']]).toarray()
one_hot
# In[106]:
insurance[['northeast','northwest','southeast','southwest']] = one_hot
# In[107]:
insurance
# In[108]:
insurance.corr()
# In[109]:
inputs,targets = insurance[['age','bmi','children','sex_codes','smokers_codes','northeast','northwest','southeast','southwest']],insurance['charges']
model = LinearRegression().fit(inputs,targets)
predictions = model.predict(inputs)
rmse(predictions,targets)
# Now let's consider the models for smoker and non smoker separately and compute the rmse losses.
# In[114]:
non_smoker_df = insurance[insurance.smoker == 'no']
# In[112]:
non_smoker_df.corr()
# In[115]:
smoker_df = insurance[insurance.smoker == 'yes']
high_smoker = smoker_df[smoker_df.bmi >= 30]
low_smoker = smoker_df[smoker_df.bmi < 30]
# In[116]:
high_smoker.corr()
# In[117]:
low_smoker.corr()
# In[120]:
inputs_nonsmoker = non_smoker_df[['age','sex_codes','bmi','children','northeast',
'northwest','southeast','southwest']]
inputs1 = high_smoker[['age','sex_codes','bmi','children','northeast',
'northwest','southeast','southwest']]
inputs2 = low_smoker[['age','sex_codes','bmi','children','northeast',
'northwest','southeast','southwest']]
# As I used Linear Regression to train my model thus its not necessary to perform feature scaling.
# In[121]:
from sklearn.model_selection import train_test_split
# In[124]:
inputs_train,inputs_test,targets_train,targets_test = train_test_split(inputs_nonsmoker,targets_nonsmoker,test_size = 0.1)
inputs_train1,inputs_test1,targets_train1,targets_test1 = train_test_split(inputs1,targets_smokerhigh,test_size = 0.1)
inputs_train2,inputs_test2,targets_train2,targets_test2 = train_test_split(inputs2,targets_lowsmoker,test_size = 0.1)
# In[132]:
model_nonsmokers = LinearRegression().fit(inputs_train,targets_train)
predictions = model.predict(inputs_test)
model_smokers_high = LinearRegression().fit(inputs_train1,targets_train1)
predictions1 = model.predict(inputs_test1)
model_smokers_low = LinearRegression().fit(inputs_train2,targets_train2)
predictions2 = model.predict(inputs_test2)
# In[130]:
from joblib import dump,load
# In[134]:
dump(model_nonsmokers,'medical_insurance_nonsmokers.joblib')
dump(model_smokers_high,'medical_insurance_smokers_high_bmi')
dump(model_smokers_low,'medical_insurance_smokers_low_bmi')
# In[ ]: