/
Kaggle_DataScienceLondon.py
661 lines (305 loc) · 10.2 KB
/
Kaggle_DataScienceLondon.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
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
# coding: utf-8
# In[34]:
import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
from pandas import DataFrame,Series
import math
# In[35]:
get_ipython().system(u'ls')
# In[65]:
test=pd.read_csv('test.csv',header=None)
# In[37]:
test.head(2)
# In[38]:
test.shape
# In[39]:
train=pd.read_csv('train.csv',header=None)
train.head(3)
# In[40]:
train.shape
# In[41]:
train_y=pd.read_csv('labels.csv',header=None)
train_y.head(2)
# In[42]:
train[40]=0
train[40]=train_y
train.head(2)
##### So now we have the prepared train dataset where the 41st column carries the value of the dependent variable
# In[43]:
t=train
train=pd.read_csv('train.csv',header=None)
### Bivariate analysis
#### Pearson correlation coefficient
##### Pearson correlation coefficient is the standard correlation coefficient between 2 variables that is defined as the ratio of covariance between the 2 variables and the square root of the product of variances of the 2 variables
# In[44]:
from scipy.stats.stats import pearsonr
# In[45]:
pearsonr(train[0],train[1])[0]
##### the first value is the correlation coefficient while the second one is the p value of the null hypothesis so clearly lesser the p value of the hypothesis better the correlation coefficient accuracy
# In[46]:
train.head()
##### We will calculate the pearson correlation coefficient for each of the bivariable combination of the 40 variables
# In[51]:
temp=np.zeros((40,40))
for i in np.arange(0,40,1):
for j in np.arange(0,40,1):
temp[i][j]=pearsonr(train[i],train[j])[0]
# In[52]:
C=DataFrame(temp)
C1=C
# In[53]:
C1.head(1)
##### We want to see the correlation amongst the different variables , so we eliminate those variables with very low correlation coefficient
# In[54]:
C[(C>-0.35) & (C<0.35)]=0
##### Now count the number of non zero values for each of the variable
##### store it in a numpy array for further analysis
# In[55]:
S=np.array([])
for i in np.arange(0,40,1):
S=np.append(S,np.count_nonzero(C[i]))
# In[56]:
S
##### But this is not true in reality , 1 value is due to the fact that the variable correlation with itself is always 1 so make those 1's also 0
# In[57]:
for i in np.arange(0,40,1):
C[i][i]=0
##### Now run the procedure for S again
# In[58]:
S=np.array([])
for i in np.arange(0,40,1):
S=np.append(S,np.count_nonzero(C[i]))
# In[59]:
S
##### So we have now got the required correlation coefficients which could be significant in our final model building
#### KMeans Clustering Analysis
# In[60]:
get_ipython().magic(u'pylab inline')
# In[61]:
from sklearn.cluster import KMeans
# In[62]:
Estimator=KMeans(init='k-means++',n_clusters=2,n_init=10)
Estimator
# In[63]:
model=Estimator.fit(train)
model
# In[67]:
test.head(2)
# In[68]:
result=np.array([])
for i in np.arange(0,9000,1):
result=np.append(result,model.predict(test.ix[i]))
# In[69]:
len(result)
# In[70]:
submission=DataFrame({'Id':np.arange(1,9001,1),'Solution':result})
# In[71]:
submission.head()
# In[75]:
submission.to_csv('submit1_KMeans.csv',index=False)
##### We have to also convert the solution column to int type from float else a 0 score is obtained
# In[82]:
submission.info()
# In[91]:
submission['Solution']=submission['Solution'].astype(int)
# In[92]:
submission.info()
# In[93]:
submission['Solution'].value_counts()
# In[94]:
submission.to_csv('submit1_KMeans.csv',index=False)
## Score : .49814
## Random forests
##### Random forests are an ensemble learning method for classification (and regression) that operate by constructing a multitude of decision trees at training time and outputting the class that is the mode of the classes output by individual trees. The term came from random decision forests that was first proposed by Tin Kam Ho of Bell Labs in 1995. The method combines Breiman's "bagging" idea and the random selection of features, introduced in order to construct a collection of decision trees with controlled variance.
# In[99]:
from sklearn import ensemble
# In[100]:
clf = ensemble.RandomForestClassifier(n_estimators = 500, n_jobs = -1)
# In[103]:
train.shape
# In[106]:
train_y=pd.read_csv('labels.csv',header=None)
# In[108]:
clf.fit(train,train_y)
# In[109]:
test_y=clf.predict(test)
# In[110]:
type(test_y)
# In[111]:
test_y
# In[115]:
submission['Solution']=Series(test_y)
# In[116]:
submission.head(2)
# In[117]:
submission.shape
# In[118]:
submission.to_csv('random_forests.csv',index=False)
## Score :0.87109
##### As the data is small , we can try random forest with a greater number of estimators
# In[127]:
clf = ensemble.RandomForestClassifier(n_estimators = 1000, n_jobs = -1)
clf.fit(train,train_y)
test_y=clf.predict(test)
submission.to_csv('random_forests1000.csv',index=False)
submission.head(2)
## Score :0.87444
# In[126]:
clf = ensemble.RandomForestClassifier(n_estimators = 10000, n_jobs = -1)
clf.fit(train,train_y)
test_y=clf.predict(test)
submission.to_csv('random_forests10000.csv',index=False)
submission.head(2)
### Removing some of the non correlated variables
# In[128]:
from scipy.stats.stats import pearsonr
# In[167]:
corrr=np.array([])
p_value=np.array([])
for i in np.arange(0,40,1):
corrr=np.append(corrr,pearsonr(train[i],train_y[0])[0])
p_value=np.append(p_value,pearsonr(train[i],train_y[0])[1])
# In[168]:
print len(corrr),len(p_value)
# In[169]:
corr=DataFrame({'corelation':corrr,'p_value':p_value})
# In[170]:
corr.head(2)
# In[171]:
corr['p_value'].describe()
## Logistic Regression
#### Missing Value Imputation
# In[95]:
train.info()
##### As there is no missing value and all are float type variables , we dont need any missing value imputation or dummy variable creation as there is no characteristic variable
# In[207]:
from sklearn import linear_model
# In[177]:
t[40].value_counts()
# In[178]:
y=t[40]
type(y)
# In[180]:
lr = linear_model.LogisticRegression()
lr.fit(train, y)
# In[181]:
test.head(2)
# In[182]:
predicted_probs = lr.predict_proba(test)
# In[183]:
predicted_probs
# In[184]:
predicted_probs.shape
# In[185]:
p=DataFrame(predicted_probs)
# In[186]:
p.shape
# In[187]:
p.head(2)
# In[188]:
p.describe()
# In[189]:
get_ipython().magic(u'pinfo lr.predict_proba')
# In[190]:
p1=p[0]
p2=p[1]
# In[192]:
p1[p1<0.5]=0
p1[p1==0.5]=0
p1[p1>0.5]=1
p2[p2<0.5]=0
p2[p2==0.5]=0
p2[p2>0.5]=1
# In[198]:
p1=np.array(p1)
p2=np.array(p2)
# In[195]:
p1
# In[199]:
print sum(p1),sum(p2)
# In[202]:
submission['Solution']=p2
# In[203]:
submission.head(2)
# In[204]:
submission.to_csv('logistic_regression.csv',index=False)
# In[205]:
submission['Solution']=submission['Solution'].astype(int)
# In[206]:
submission.to_csv('logistic_regression.csv',index=False)
## Score :0.80179
## Univariate analysis
##### Here we see how the various independent variables are distributed
# In[253]:
train.head(1)
# In[254]:
train[0].hist()
##### we will combine the test and the train data as we are computing the distributions of only the independent variables
# In[255]:
pieces=[train,test]
# In[256]:
comb=pd.concat(pieces)
# In[258]:
comb.index
# In[259]:
comb.index=np.arange(0,10000,1)
# In[262]:
fig1=comb[0].hist()
# In[263]:
comb[0].describe()
# In[264]:
comb.boxplot(column=[0])
## Q-Q Plot
##### In statistics, a Q–Q plot ("Q" stands for quantile) is a probability plot, which is a graphical method for comparing two probability distributions by plotting their quantiles against each other. If the two distributions being compared are similar, the points in the Q–Q plot will approximately lie on the line y = x. If the distributions are linearly related, the points in the Q–Q plot will approximately lie on a line, but not necessarily on the line y = x.
# In[266]:
import statsmodels.api as sm
# In[269]:
sm.qqplot(comb[1],line='45')
# In[275]:
os.getcwd()
# In[287]:
for i in np.arange(0,40,1):
pieces1='histograms/histogram',format(i),'.jpg'
hist=comb[i].hist()
fig = hist.get_figure()
fig.savefig(''.join(pieces1))
fig.clear()
##### The above code saves all the histograms for the 40 variables
# In[285]:
for i in np.arange(0,40,1):
sm.qqplot(comb[i],line='45')
# In[291]:
sm.qqplot(comb[4],line='45')
## Principal component analysis
#### Principal component analysis (PCA) is a statistical procedure that uses an orthogonal transformation to convert a set of observations of possibly correlated variables into a set of values of linearly uncorrelated variables called principal components.The number of principal components is less than or equal to the number of original variables. This transformation is defined in such a way that the first principal component has the largest possible variance (that is, accounts for as much of the variability in the data as possible), and each succeeding component in turn has the highest variance possible under the constraint that it is orthogonal to (i.e., uncorrelated with) the preceding components. The principal components are orthogonal because they are the eigenvectors of the covariance matrix, which is symmetric. PCA is sensitive to the relative scaling of the original variables.
##### The main purposes of a principal component analysis are the analysis of data to identify patterns and finding patterns to reduce the dimensions of the dataset with minimal loss of information.
# In[292]:
from matplotlib.mlab import PCA as mlabPCA
# In[295]:
mlab_pca = mlabPCA(train)
mlab_pca
# In[296]:
mlab_pca.Y
# In[298]:
mlab_pca.Y.shape
# In[299]:
PCAY=DataFrame(mlab_pca.Y)
# In[300]:
PCAY.head(2)
# In[301]:
train.head(2)
# In[309]:
from sklearn import ensemble
clf = ensemble.RandomForestClassifier(n_estimators = 1000, n_jobs = -1)
clf.fit(PCAY,train_y)
test_y=clf.predict(test)
submission['Solution']=test_y
submission.to_csv('PCA.csv',index=False)
submission.head(2)
# In[310]:
submission.info()
### score: 0.5182
##### The PCA analysis will be done separately in a more detailed manner to get a good score for the contest . I would still recommend that all the analysis should be done independently to make urself familiar with all the algorithms
# In[ ]: