In [2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

import seaborn as sns

from sklearn.model_selection import train_test_split

In [3]:
tr_data = pd.read_csv('../input/train.csv')
te_data = pd.read_csv('../input/test.csv')
print('train shape is: {} \r\ntest shape is: {} '.format(tr_data.shape,te_data.shape))


train shape is: (61878, 95) 
test shape is: (144368, 94) 

In [4]:
from sklearn.feature_extraction.text import TfidfTransformer

y = tr_data.target
tr_ids = tr_data.id
te_ids = te_data.id
tr_data.drop(['id','target'],axis=1,inplace=True)
te_data.drop(['id'],axis=1,inplace=True)

In [5]:
tr_data['count_zeros'] = (tr_data == 0).astype(int).sum(axis=1)
te_data['count_zeros'] = (te_data == 0).astype(int).sum(axis=1)

tr_data['num_greater_than_3'] = (tr_data > 3).astype(int).sum(axis=1)
te_data['num_greater_than_3'] = (te_data > 3).astype(int).sum(axis=1)

tr_data['num_greater_than_10'] = (tr_data > 10).astype(int).sum(axis=1)
te_data['num_greater_than_10'] = (te_data > 10).astype(int).sum(axis=1)

In [6]:
tfidf = TfidfTransformer()
tr_data_tfidf = pd.DataFrame(tfidf.fit_transform(tr_data).toarray())
te_data_tfidf = pd.DataFrame(tfidf.transform(te_data).toarray())
tr_data_tfidf.columns = [str(x)+'tfidf' for x in tr_data_tfidf.columns]
te_data_tfidf.columns = [str(x)+'tfidf' for x in te_data_tfidf.columns]

tr_data_log1p = pd.DataFrame(tr_data.apply(lambda x: np.log1p(x)))
te_data_log1p = pd.DataFrame(te_data.apply(lambda x: np.log1p(x)))
tr_data_log1p.columns = [str(x)+'log1p' for x in tr_data_log1p.columns]
te_data_log1p.columns = [str(x)+'log1p' for x in te_data_log1p.columns]

tr_data_comb = pd.concat([tr_data,tr_data_tfidf,tr_data_log1p],axis=1)
te_data_comb = pd.concat([te_data,te_data_tfidf,te_data_log1p],axis=1)

In [7]:
# from sklearn.preprocessing import LabelEncoder
# le = LabelEncoder()
# y_encoded = le.fit_transform(y)
y_encoded = [int(x.split('_')[1]) for x in y]
y_encoded = [y-1 for y in y_encoded]

In [8]:
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix,log_loss
from sklearn.model_selection import train_test_split,KFold

X_train, X_val, y_train, y_val = train_test_split(tr_data_comb,y_encoded,test_size = 0.2,random_state =12345)

In [11]:
import xgboost as xgb

params = {'objective':'multi:softprob',
                    'learning_rate':0.1,
                    'subsample':0.8,
                    'colsample_bytree':0.9,
                    'colsample_bylevel':0.7,
                    'max_depth':7,
                    'nthread':4,
                    'eval_metric':'mlogloss',
                    'num_class':9,
                    'gamma':0.1,
                    'seed':1234}

bst_cv = xgb.cv(params=params,dtrain=xgb.DMatrix(tr_data_comb,label=y_encoded),verbose_eval=2,
                nfold=5,early_stopping_rounds=20,num_boost_round=300)


[0]	train-mlogloss:1.9474+0.00072084	test-mlogloss:1.955+0.000620196
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-11-f218d5ee1acc> in <module>()
     14 
     15 bst_cv = xgb.cv(params=params,dtrain=xgb.DMatrix(tr_data_comb,label=y_encoded),verbose_eval=2,
---> 16                 nfold=5,early_stopping_rounds=20,num_boost_round=300)
     17 

~/anaconda3/lib/python3.6/site-packages/xgboost/training.py in cv(params, dtrain, num_boost_round, nfold, stratified, folds, metrics, obj, feval, maximize, early_stopping_rounds, fpreproc, as_pandas, verbose_eval, show_stdv, seed, callbacks)
    398                            evaluation_result_list=None))
    399         for fold in cvfolds:
--> 400             fold.update(i, obj)
    401         res = aggcv([f.eval(i, feval) for f in cvfolds])
    402 

~/anaconda3/lib/python3.6/site-packages/xgboost/training.py in update(self, iteration, fobj)
    217     def update(self, iteration, fobj):
    218         """"Update the boosters for one iteration"""
--> 219         self.bst.update(self.dtrain, iteration, fobj)
    220 
    221     def eval(self, iteration, feval):

~/anaconda3/lib/python3.6/site-packages/xgboost/core.py in update(self, dtrain, iteration, fobj)
    804 
    805         if fobj is None:
--> 806             _check_call(_LIB.XGBoosterUpdateOneIter(self.handle, iteration, dtrain.handle))
    807         else:
    808             pred = self.predict(dtrain)

KeyboardInterrupt: 

In [38]:
bst = xgb.train(params=params,dtrain=xgb.DMatrix(tr_data_comb,label=y_encoded),num_boost_round=400)


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-38-1414ed62651d> in <module>()
----> 1 bst = xgb.train(params=params,dtrain=xgb.DMatrix(tr_data_comb,label=y_encoded),num_boost_round=300)

~/anaconda3/lib/python3.6/site-packages/xgboost/training.py in train(params, dtrain, num_boost_round, evals, obj, feval, maximize, early_stopping_rounds, evals_result, verbose_eval, learning_rates, xgb_model, callbacks)
    203                            evals=evals,
    204                            obj=obj, feval=feval,
--> 205                            xgb_model=xgb_model, callbacks=callbacks)
    206 
    207 

~/anaconda3/lib/python3.6/site-packages/xgboost/training.py in _train_internal(params, dtrain, num_boost_round, evals, obj, feval, xgb_model, callbacks)
     74         # Skip the first update if it is a recovery step.
     75         if version % 2 == 0:
---> 76             bst.update(dtrain, i, obj)
     77             bst.save_rabit_checkpoint()
     78             version += 1

~/anaconda3/lib/python3.6/site-packages/xgboost/core.py in update(self, dtrain, iteration, fobj)
    804 
    805         if fobj is None:
--> 806             _check_call(_LIB.XGBoosterUpdateOneIter(self.handle, iteration, dtrain.handle))
    807         else:
    808             pred = self.predict(dtrain)

KeyboardInterrupt: 

In [97]:
pred = bst.predict(xgb.DMatrix(te_data_comb))
subm = pd.DataFrame(pred)
subm.columns = ['class_'+ str(x) for x in range(1,10)]
subm['id'] = pd.read_csv('../input/test.csv',usecols=['id'])
#subm.index_label = 'id'
subm.to_csv('../subm/tfidf_log1p_raw_xgb_sub1.csv',index=False)

let's ignore the tfidf transform for and see what you could also come up with based on the things we learned...


In [1]:
tr_data = pd.read_csv('../input/train.csv')
te_data = pd.read_csv('../input/test.csv')

y = tr_data.target
tr_data.drop(['id','target'],axis=1,inplace=True)
te_data.drop(['id'],axis=1,inplace=True)

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y_encoded = le.fit_transform(y)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-091692595715> in <module>()
----> 1 tr_data = pd.read_csv('../input/train.csv')
      2 te_data = pd.read_csv('../input/test.csv')
      3 
      4 y = tr_data.target
      5 tr_data.drop(['id','target'],axis=1,inplace=True)

NameError: name 'pd' is not defined

In [113]:
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix,log_loss
from sklearn.model_selection import train_test_split

X_train, X_val, y_train, y_val = train_test_split(tr_data,y_encoded,test_size = 0.2,random_state =12345)

knn2 = KNeighborsClassifier(n_jobs=4,n_neighbors=2,)
knn2.fit(X_train,y_train)
knn2_pred = knn2.predict_proba(X_val)
print('log loss for knn2: {}'.format(log_loss(y_pred = knn2_pred,y_true = y_val)))

knn4 = KNeighborsClassifier(n_jobs=4,n_neighbors=4,)
knn4.fit(X_train,y_train)
knn4_pred = knn4.predict_proba(X_val)
print('log loss for knn4: {}'.format(log_loss(y_pred = knn4_pred,y_true = y_val)))

knn8 = KNeighborsClassifier(n_jobs=8,n_neighbors=8,)
knn8.fit(X_train,y_train)
knn8_pred = knn8.predict_proba(X_val)
print('log loss for knn8: {}'.format(log_loss(y_pred = knn8_pred,y_true = y_val)))

knn16 = KNeighborsClassifier(n_jobs=4,n_neighbors=16,)
knn16.fit(X_train,y_train)
knn16_pred = knn16.predict_proba(X_val)
print('log loss for knn16: {}'.format(log_loss(y_pred = knn16_pred,y_true = y_val)))

knn32 = KNeighborsClassifier(n_jobs=4,n_neighbors=32,)
knn32.fit(X_train,y_train)
knn32_pred = knn32.predict_proba(X_val)
print('log loss for knn32: {}'.format(log_loss(y_pred = knn32_pred,y_true = y_val)))

knn64 = KNeighborsClassifier(n_jobs=4,n_neighbors=64,)
knn64.fit(X_train,y_train)
knn64_pred = knn64.predict_proba(X_val)
print('log loss for knn64: {}'.format(log_loss(y_pred = knn64_pred,y_true = y_val)))

knn128 = KNeighborsClassifier(n_jobs=4,n_neighbors=128,)
knn128.fit(X_train,y_train)
knn128_pred = knn128.predict_proba(X_val)
print('log loss for knn128: {}'.format(log_loss(y_pred = knn128_pred,y_true = y_val)))


log loss for knn2: 4.8441469936134425
log loss for knn4: 2.6644552310076475
log loss for knn8: 1.5318682781966835
log loss for knn16: 0.9965564078189585
log loss for knn32: 0.7943305611598044
log loss for knn64: 0.7002169581175647
log loss for knn128: 0.6943451757833374

In [101]:
class_weights = {0:1,1:1,2:1,3:10,4:1,5:1,6:1,7:1,8:1}
from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier(class_weight=class_weights,max_depth=4,max_features=92,min_samples_split=2,random_state=12345)
dtc.fit(X_train,y_train)
tree_pred = dtc.predict_proba(X_val)
print('log loss for dtc: {}'.format(log_loss(y_pred = tree_pred,y_true = y_val)))


log loss for dtc: 1.647334289958738

In [102]:
# class_weights = {0:1,1:1,2:1,3:1,4:1,5:1,6:10,7:1,8:1}
# lets remove the class weights and check our score...
from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier(max_depth=4,max_features=92,min_samples_split=2,random_state=12345)
dtc.fit(X_train,y_train)
tree_pred = dtc.predict_proba(X_val)
print('log loss for dtc: {}'.format(log_loss(y_pred = tree_pred,y_true = y_val)))


log loss for dtc: 1.4348667288258288

In [114]:
from sklearn.svm import SVC
svc = SVC(kernel='linear',C=0.1,max_iter=10000,random_state=12345,probability=True)
svc.fit(X_train,y_train)
svc_pred = svc.predict_proba(X_val)
print('log loss for svc: {}'.format(log_loss(y_pred = svc_pred,y_true = y_val)))


/home/nati/anaconda3/lib/python3.6/site-packages/sklearn/svm/base.py:220: ConvergenceWarning: Solver terminated early (max_iter=10000).  Consider pre-processing your data with StandardScaler or MinMaxScaler.
  % self.max_iter, ConvergenceWarning)
log loss for svc: 0.6653540066424208

In [ ]:
from sklearn.svm import SVC
svc = SVC(kernel='rbf',C=0.1,max_iter=10000,random_state=12345,probability=True,)
svc.fit(X_train,y_train)
svc_pred = svc.predict_proba(X_val)
print('log loss for svc: {}'.format(log_loss(y_pred = svc_pred,y_true = y_val)))

In [109]:
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(n_jobs=4,n_estimators=300)
rfc.fit(X_train,y_train)
rfc_pred = rfc.predict_proba(X_val)
print('log loss for RFC: {}'.format(log_loss(y_pred = rfc_pred,y_true = y_val)))


log loss for RFC: 0.5726116665922214

In [61]:
rfc_test_pred = rfc.predict_proba(te_data)

In [92]:
import xgboost as xgb
dtrain=xgb.DMatrix(X_train,label=y_train)
dval=xgb.DMatrix(X_val,label=y_val)
watchlist = [(dtrain, 'train'),(dval, 'eval')]

params = {'objective':'multi:softprob',
                    'learning_rate':0.05,
                    'subsample':0.7,
                    'colsample_bytree':0.8,
                    'colsample_bylevel':0.7,
                    'max_depth':6,
                    'nthread':3,
                    'eval_metric':'mlogloss',
                    'num_class':9,
                    'silent':0,
                    'seed':1234}

bst = xgb.train(params=params,dtrain=xgb.DMatrix(X_train,label=y_train),num_boost_round=3000,early_stopping_rounds=80,
                evals=watchlist,verbose_eval=50)


[0]	eval-mlogloss:2.0774	train-mlogloss:2.075
Multiple eval metrics have been passed: 'train-mlogloss' will be used for early stopping.

Will train until train-mlogloss hasn't improved in 80 rounds.
[50]	eval-mlogloss:0.765823	train-mlogloss:0.704729
[100]	eval-mlogloss:0.605446	train-mlogloss:0.512647
[150]	eval-mlogloss:0.553927	train-mlogloss:0.438171
[200]	eval-mlogloss:0.528086	train-mlogloss:0.392941
[250]	eval-mlogloss:0.512905	train-mlogloss:0.359338
[300]	eval-mlogloss:0.50243	train-mlogloss:0.332146
[350]	eval-mlogloss:0.49479	train-mlogloss:0.309143
[400]	eval-mlogloss:0.48858	train-mlogloss:0.288172
[450]	eval-mlogloss:0.484047	train-mlogloss:0.27052
[500]	eval-mlogloss:0.480213	train-mlogloss:0.25378
[550]	eval-mlogloss:0.477109	train-mlogloss:0.238802
[600]	eval-mlogloss:0.474876	train-mlogloss:0.225237
[650]	eval-mlogloss:0.473521	train-mlogloss:0.212742
[700]	eval-mlogloss:0.472207	train-mlogloss:0.200934
[750]	eval-mlogloss:0.471159	train-mlogloss:0.189759
[800]	eval-mlogloss:0.470579	train-mlogloss:0.17962
[850]	eval-mlogloss:0.470158	train-mlogloss:0.170171
[900]	eval-mlogloss:0.470253	train-mlogloss:0.161341
[950]	eval-mlogloss:0.470208	train-mlogloss:0.153517
[1000]	eval-mlogloss:0.470167	train-mlogloss:0.146116
[1050]	eval-mlogloss:0.470499	train-mlogloss:0.139369
[1100]	eval-mlogloss:0.471161	train-mlogloss:0.132695
[1150]	eval-mlogloss:0.472029	train-mlogloss:0.126684
[1200]	eval-mlogloss:0.472778	train-mlogloss:0.120981
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-92-8a8bea751d6f> in <module>()
     17 
     18 bst = xgb.train(params=params,dtrain=xgb.DMatrix(X_train,label=y_train),num_boost_round=3000,early_stopping_rounds=80,
---> 19                 evals=watchlist,verbose_eval=50)

~/anaconda3/lib/python3.6/site-packages/xgboost/training.py in train(params, dtrain, num_boost_round, evals, obj, feval, maximize, early_stopping_rounds, evals_result, verbose_eval, learning_rates, xgb_model, callbacks)
    203                            evals=evals,
    204                            obj=obj, feval=feval,
--> 205                            xgb_model=xgb_model, callbacks=callbacks)
    206 
    207 

~/anaconda3/lib/python3.6/site-packages/xgboost/training.py in _train_internal(params, dtrain, num_boost_round, evals, obj, feval, xgb_model, callbacks)
     74         # Skip the first update if it is a recovery step.
     75         if version % 2 == 0:
---> 76             bst.update(dtrain, i, obj)
     77             bst.save_rabit_checkpoint()
     78             version += 1

~/anaconda3/lib/python3.6/site-packages/xgboost/core.py in update(self, dtrain, iteration, fobj)
    804 
    805         if fobj is None:
--> 806             _check_call(_LIB.XGBoosterUpdateOneIter(self.handle, iteration, dtrain.handle))
    807         else:
    808             pred = self.predict(dtrain)

KeyboardInterrupt: 

In [111]:
xgb_pred = bst.predict(xgb.DMatrix(X_val))
print('log loss for XGB: {}'.format(log_loss(y_pred = xgb_pred,y_true = y_val)))


log loss for XGB: 0.46402587516456023

In [62]:
xgb_test_pred = bst.predict(xgb.DMatrix(te_data))
combined_pred = xgb_test_pred *0.85 + rfc_test_pred *0.15
subm = pd.DataFrame(combined_pred)
subm.columns = ['class_'+ str(x) for x in range(1,10)]
subm['id'] = pd.read_csv('../input/test.csv',usecols=['id'])
#subm.index_label = 'id'
subm.to_csv('../subm/rf_xgb_sub1.csv',index=False)

In [14]:
from keras.models import Sequential,Model
from keras.layers import Dense,Dropout,Merge,Embedding,BatchNormalization
from keras.utils import np_utils

OHE_y_train = np_utils.to_categorical(y_train)
OHE_y_val = np_utils.to_categorical(y_val)


Using TensorFlow backend.

In [15]:
from keras.callbacks import ReduceLROnPlateau
cb = ReduceLROnPlateau(patience=1,factor=0.1,epsilon=0.02)

In [28]:
model1 = Sequential()
model1.add(Dense(500,input_shape=(X_train.shape[1],),activation='relu'))
model1.add(Dropout(0.1))
model1.add(Dense(250,activation='relu'))
model1.add(Dropout(0.05))
model1.add(Dense(50,activation='relu'))
model1.add(Dense(9,activation='softmax'))
model1.compile(optimizer='adam',
              loss='categorical_crossentropy')

model1.fit(X_train.values,OHE_y_train,validation_data=[X_val.values,OHE_y_val],callbacks=[cb],epochs=10)


Train on 49502 samples, validate on 12376 samples
Epoch 1/10
49502/49502 [==============================] - 13s - loss: 0.7291 - val_loss: 0.5988
Epoch 2/10
49502/49502 [==============================] - 12s - loss: 0.5849 - val_loss: 0.5516
Epoch 3/10
49502/49502 [==============================] - 12s - loss: 0.5515 - val_loss: 0.5489
Epoch 4/10
49502/49502 [==============================] - 13s - loss: 0.5268 - val_loss: 0.5249
Epoch 5/10
49502/49502 [==============================] - 13s - loss: 0.5021 - val_loss: 0.4952
Epoch 6/10
49502/49502 [==============================] - 12s - loss: 0.4830 - val_loss: 0.5169
Epoch 7/10
49502/49502 [==============================] - 13s - loss: 0.4676 - val_loss: 0.5033
Epoch 8/10
49502/49502 [==============================] - 13s - loss: 0.4069 - val_loss: 0.4754
Epoch 9/10
49502/49502 [==============================] - 12s - loss: 0.3882 - val_loss: 0.4738
Epoch 10/10
49502/49502 [==============================] - 13s - loss: 0.3869 - val_loss: 0.4733
Out[28]:
<keras.callbacks.History at 0x7fb0f80139b0>

In [29]:
ann_pred = model1.predict_classes(X_val.values)
from sklearn.metrics import classification_report,log_loss
print(confusion_matrix(y_pred=ann_pred,y_true=y_val))
sns.heatmap(confusion_matrix(y_pred=ann_pred+1,y_true=y_val),cmap='Greens',xticklabels=range(1,10),yticklabels=range(1,10))
print('classification report results:\r\n' + classification_report(y_pred=ann_pred,y_true=y_val))
print('log-loss for classifier: {}'.format(log_loss(y_pred=model1.predict(X_val.values),y_true=y_val)))


12128/12376 [============================>.] - ETA: 0s[[ 225   11    1    0    1   26   13   41   92]
 [   2 2630  489   53    5    5   19   11    8]
 [   0  638  832   36    1    1   32    9    1]
 [   1  154   96  268    7   14   14    3    0]
 [   1    8    2    0  523    0    0    1    0]
 [  13   10    3    6    0 2699   34   25   20]
 [   8   46   41    5    0   27  392   17    2]
 [  20   10    3    0    2   37   21 1575   27]
 [  35   16    2    3    0   27    3   31  942]]
classification report results:
             precision    recall  f1-score   support

          0       0.74      0.55      0.63       410
          1       0.75      0.82      0.78      3222
          2       0.57      0.54      0.55      1550
          3       0.72      0.48      0.58       557
          4       0.97      0.98      0.97       535
          5       0.95      0.96      0.96      2810
          6       0.74      0.73      0.74       538
          7       0.92      0.93      0.92      1695
          8       0.86      0.89      0.88      1059

avg / total       0.81      0.81      0.81     12376

log-loss for classifier: 0.473291540907604

In [30]:
model1 = Sequential()
model1.add(Dense(500,input_shape=(X_train.shape[1],),activation='relu'))
model1.add(Dropout(0.2))
model1.add(Dense(250,activation='relu'))
model1.add(Dropout(0.1))
model1.add(Dense(50,activation='relu'))
model1.add(Dense(9,activation='softmax'))
model1.compile(optimizer='adam',
              loss='categorical_crossentropy')

model1.fit(X_train.values,OHE_y_train,validation_data=[X_val.values,OHE_y_val],callbacks=[cb],epochs=10)


Train on 49502 samples, validate on 12376 samples
Epoch 1/10
49502/49502 [==============================] - 13s - loss: 0.7567 - val_loss: 0.5862
Epoch 2/10
49502/49502 [==============================] - 12s - loss: 0.6026 - val_loss: 0.5620
Epoch 3/10
49502/49502 [==============================] - 12s - loss: 0.5685 - val_loss: 0.5448
Epoch 4/10
49502/49502 [==============================] - 12s - loss: 0.5448 - val_loss: 0.5256
Epoch 5/10
49502/49502 [==============================] - 12s - loss: 0.5254 - val_loss: 0.5223
Epoch 6/10
49502/49502 [==============================] - 12s - loss: 0.5086 - val_loss: 0.5086
Epoch 7/10
49502/49502 [==============================] - 12s - loss: 0.4541 - val_loss: 0.4772
Epoch 8/10
49502/49502 [==============================] - 12s - loss: 0.4406 - val_loss: 0.4725
Epoch 9/10
49502/49502 [==============================] - 12s - loss: 0.4309 - val_loss: 0.4717
Epoch 10/10
49502/49502 [==============================] - 12s - loss: 0.4220 - val_loss: 0.4705
Out[30]:
<keras.callbacks.History at 0x7fb0f05ffa20>

In [54]:



Out[54]:
61878

In [94]:
oof_preds = []
test_preds = []
kf = KFold(n_splits=5,shuffle=True,random_state=12345)
for train_index, val_index in kf.split(X=tr_data_comb,y=y_encoded):
    print(train_index,val_index)
    X_train, X_val = tr_data_comb.iloc[train_index,:], tr_data_comb.iloc[val_index,:]
    y_train, y_val = np.array(y_encoded)[train_index], np.array(y_encoded)[val_index]
    OHE_y_train = np_utils.to_categorical(y_train)
    OHE_y_val = np_utils.to_categorical(y_val)
    
    model1 = Sequential()
    model1.add(Dense(1024,input_shape=(X_train.shape[1],),activation='relu'))
    model1.add(BatchNormalization())
    model1.add(Dropout(0.1))
    model1.add(Dense(1024,activation='relu'))
    model1.add(Dropout(0.1))
    model1.add(Dense(1024,activation='relu'))
    model1.add(Dense(9,activation='softmax'))
    model1.compile(optimizer='adam',loss='categorical_crossentropy')

    model1.fit(X_train.values,OHE_y_train,validation_data=[X_val.values,OHE_y_val],callbacks=[cb],epochs=10)
    
    ann_pred = model1.predict(X_val.values)
    ann_test_pred = model1.predict(te_data_comb.values)
    oof_preds.append(ann_pred)
    test_preds.append(ann_test_pred)


[    0     1     2 ..., 61874 61875 61877] [    8    14    18 ..., 61868 61869 61876]
Train on 49502 samples, validate on 12376 samples
Epoch 1/10
49502/49502 [==============================] - 17s - loss: 0.7025 - val_loss: 0.6005
Epoch 2/10
49502/49502 [==============================] - 15s - loss: 0.6093 - val_loss: 0.5537
Epoch 3/10
49502/49502 [==============================] - 16s - loss: 0.5730 - val_loss: 0.5551
Epoch 4/10
 3936/49502 [=>............................] - ETA: 14s - loss: 0.5406
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-94-0ae6ed68ade6> in <module>()
     19     model1.compile(optimizer='adam',loss='categorical_crossentropy')
     20 
---> 21     model1.fit(X_train.values,OHE_y_train,validation_data=[X_val.values,OHE_y_val],callbacks=[cb],epochs=10)
     22 
     23     ann_pred = model1.predict(X_val.values)

~/anaconda3/lib/python3.6/site-packages/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
    869                               class_weight=class_weight,
    870                               sample_weight=sample_weight,
--> 871                               initial_epoch=initial_epoch)
    872 
    873     def evaluate(self, x, y, batch_size=32, verbose=1,

~/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1598                               initial_epoch=initial_epoch,
   1599                               steps_per_epoch=steps_per_epoch,
-> 1600                               validation_steps=validation_steps)
   1601 
   1602     def evaluate(self, x=None, y=None,

~/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
   1181                     batch_logs['size'] = len(batch_ids)
   1182                     callbacks.on_batch_begin(batch_index, batch_logs)
-> 1183                     outs = f(ins_batch)
   1184                     if not isinstance(outs, list):
   1185                         outs = [outs]

~/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
   2255         updated = session.run(self.outputs + [self.updates_op],
   2256                               feed_dict=feed_dict,
-> 2257                               **self.session_kwargs)
   2258         return updated[:len(self.outputs)]
   2259 

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    893     try:
    894       result = self._run(None, fetches, feed_dict, options_ptr,
--> 895                          run_metadata_ptr)
    896       if run_metadata:
    897         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1122     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1123       results = self._do_run(handle, final_targets, final_fetches,
-> 1124                              feed_dict_tensor, options, run_metadata)
   1125     else:
   1126       results = []

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1319     if handle is None:
   1320       return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1321                            options, run_metadata)
   1322     else:
   1323       return self._do_call(_prun_fn, self._session, handle, feeds, fetches)

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1325   def _do_call(self, fn, *args):
   1326     try:
-> 1327       return fn(*args)
   1328     except errors.OpError as e:
   1329       message = compat.as_text(e.message)

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1304           return tf_session.TF_Run(session, options,
   1305                                    feed_dict, fetch_list, target_list,
-> 1306                                    status, run_metadata)
   1307 
   1308     def _prun_fn(session, handle, feed_dict, fetch_list):

KeyboardInterrupt: 

In [80]:
test_pred_arr = np.array(test_preds)
preds = pd.DataFrame(np.clip(np.mean(test_pred_arr,axis=0),a_max=0.999,a_min=0.001))
subm = pd.DataFrame(preds)
subm.columns = ['Class_'+ str(x) for x in range(1,10)]
subm['id'] = te_ids
subm.to_csv('../subm/ANN_5fold.csv',index=False)

In [78]:
subm


Out[78]:
class_1 class_2 class_3 class_4 class_5 class_6 class_7 class_8 class_9 id
0 0.001000 0.208968 0.240721 0.547515 0.001000 0.001000 0.002681 0.001000 0.001000 1
1 0.001000 0.001000 0.001000 0.001000 0.001000 0.674757 0.001000 0.321769 0.003071 2
2 0.001000 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 0.001000 0.001000 3
3 0.001000 0.609515 0.388631 0.001851 0.001000 0.001000 0.001000 0.001000 0.001000 4
4 0.069937 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 0.001525 0.928398 5
5 0.001000 0.312959 0.684896 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 6
6 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 7
7 0.001000 0.963565 0.028423 0.007456 0.001000 0.001000 0.001000 0.001000 0.001000 8
8 0.003016 0.427492 0.424492 0.124076 0.001000 0.001000 0.018744 0.001000 0.001000 9
9 0.005035 0.428688 0.262573 0.002361 0.010959 0.051200 0.017695 0.215042 0.006446 10
10 0.006408 0.355690 0.389999 0.174736 0.001000 0.001872 0.068765 0.001000 0.001118 11
11 0.057557 0.001000 0.001000 0.001000 0.001000 0.002416 0.001000 0.001000 0.939708 12
12 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 0.001000 0.001000 0.001000 13
13 0.001000 0.966769 0.024264 0.007309 0.001000 0.001000 0.001000 0.001000 0.001000 14
14 0.003499 0.855406 0.117514 0.009208 0.001000 0.001000 0.003543 0.001000 0.008453 15
15 0.062670 0.001000 0.001000 0.001000 0.001000 0.016578 0.782979 0.137492 0.001000 16
16 0.001000 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 0.001000 0.001000 17
17 0.001000 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 0.001000 0.001000 18
18 0.001000 0.001000 0.001000 0.022623 0.976627 0.001000 0.001000 0.001000 0.001000 19
19 0.001000 0.779807 0.181688 0.035555 0.001000 0.001000 0.002030 0.001000 0.001000 20
20 0.001000 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 0.001000 0.001000 21
21 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 22
22 0.006439 0.476985 0.228144 0.242141 0.001000 0.002819 0.007388 0.001474 0.034144 23
23 0.007656 0.001000 0.001000 0.001000 0.001000 0.018217 0.001000 0.958579 0.015429 24
24 0.112242 0.001000 0.001000 0.001000 0.001000 0.067750 0.001000 0.010292 0.808263 25
25 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 26
26 0.001000 0.145573 0.827702 0.026218 0.001000 0.001000 0.001000 0.001000 0.001000 27
27 0.001000 0.925317 0.066953 0.007486 0.001000 0.001000 0.001000 0.001000 0.001000 28
28 0.001000 0.450735 0.538647 0.010565 0.001000 0.001000 0.001000 0.001000 0.001000 29
29 0.001000 0.180890 0.819059 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 30
... ... ... ... ... ... ... ... ... ... ...
144338 0.005022 0.260690 0.406058 0.128609 0.001025 0.006966 0.184826 0.002917 0.003886 144339
144339 0.001000 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 0.001000 0.001000 144340
144340 0.032840 0.347416 0.267714 0.085864 0.009410 0.040524 0.147880 0.047727 0.020626 144341
144341 0.001000 0.809849 0.142712 0.047162 0.001000 0.001000 0.001000 0.001000 0.001000 144342
144342 0.001000 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 0.001000 0.001000 144343
144343 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 144344
144344 0.001000 0.776325 0.194695 0.028288 0.001000 0.001000 0.001000 0.001000 0.001000 144345
144345 0.004291 0.001000 0.001000 0.002878 0.001000 0.988525 0.001000 0.001442 0.002731 144346
144346 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 0.001000 0.001000 0.001000 144347
144347 0.001000 0.336146 0.525665 0.132012 0.001000 0.001000 0.005282 0.001000 0.001000 144348
144348 0.001000 0.827664 0.100055 0.070153 0.001000 0.001000 0.001000 0.001000 0.001226 144349
144349 0.001000 0.572332 0.397430 0.028070 0.001000 0.001000 0.002038 0.001000 0.001000 144350
144350 0.001357 0.001000 0.001000 0.001000 0.001000 0.997659 0.001000 0.001000 0.001000 144351
144351 0.001000 0.664792 0.293183 0.042009 0.001000 0.001000 0.001000 0.001000 0.001000 144352
144352 0.001000 0.903193 0.092301 0.001000 0.001000 0.001000 0.001000 0.002898 0.001000 144353
144353 0.001000 0.001000 0.001000 0.001000 0.001000 0.044817 0.001000 0.001000 0.954635 144354
144354 0.072238 0.001000 0.001000 0.001677 0.001000 0.737002 0.162776 0.001942 0.023210 144355
144355 0.001000 0.001000 0.001000 0.001000 0.001000 0.094825 0.001000 0.905166 0.001000 144356
144356 0.442786 0.001000 0.001000 0.001000 0.001000 0.001000 0.001000 0.556996 0.001000 144357
144357 0.001000 0.001000 0.001000 0.001000 0.999000 0.001000 0.001000 0.001000 0.001000 144358
144358 0.007232 0.001000 0.001000 0.002481 0.001000 0.966677 0.001000 0.004247 0.018775 144359
144359 0.001000 0.872030 0.096009 0.026228 0.001000 0.001000 0.002657 0.001000 0.001522 144360
144360 0.001000 0.001000 0.001000 0.001000 0.001000 0.038284 0.961553 0.001000 0.001000 144361
144361 0.001000 0.531110 0.413039 0.054133 0.001000 0.001000 0.001378 0.001000 0.001000 144362
144362 0.001000 0.027134 0.357569 0.509527 0.001000 0.001000 0.105559 0.001000 0.001000 144363
144363 0.384997 0.001606 0.001000 0.005161 0.001000 0.557151 0.021771 0.010221 0.018949 144364
144364 0.001402 0.325014 0.496746 0.136171 0.001000 0.001000 0.040175 0.001000 0.001000 144365
144365 0.001000 0.671773 0.312076 0.015415 0.001000 0.001000 0.001000 0.001000 0.001000 144366
144366 0.001000 0.466384 0.059653 0.473535 0.001000 0.001000 0.001000 0.001000 0.001000 144367
144367 0.001000 0.382231 0.519363 0.058214 0.001000 0.001000 0.039444 0.001000 0.001000 144368

144368 rows × 10 columns


In [35]:
model1 = Sequential()
model1.add(Dense(1500,input_shape=(X_train.shape[1],),activation='relu'))
model1.add(Dropout(0.3))
model1.add(Dense(750,activation='relu'))
model1.add(Dense(9,activation='softmax'))
model1.compile(optimizer='adam',
              loss='categorical_crossentropy')

model1.fit(X_train.values,OHE_y_train,validation_data=[X_val.values,OHE_y_val],callbacks=[cb],epochs=10)
ann_pred = model1.predict_classes(X_val.values)
from sklearn.metrics import classification_report,log_loss
print(confusion_matrix(y_pred=ann_pred,y_true=y_val))
sns.heatmap(confusion_matrix(y_pred=ann_pred+1,y_true=y_val),cmap='Greens',xticklabels=range(1,10),yticklabels=range(1,10))
print('classification report results:\r\n' + classification_report(y_pred=ann_pred,y_true=y_val))
print('log-loss for classifier: {}'.format(log_loss(y_pred=model1.predict(X_val.values),y_true=y_val)))


Train on 49502 samples, validate on 12376 samples
Epoch 1/10
49502/49502 [==============================] - 12s - loss: 0.9291 - val_loss: 0.6100
Epoch 2/10
49502/49502 [==============================] - 11s - loss: 0.5873 - val_loss: 0.5631
Epoch 3/10
49502/49502 [==============================] - 11s - loss: 0.5590 - val_loss: 0.5341
Epoch 4/10
49502/49502 [==============================] - 11s - loss: 0.5394 - val_loss: 0.5259
Epoch 5/10
49502/49502 [==============================] - 11s - loss: 0.5226 - val_loss: 0.5203
Epoch 6/10
49502/49502 [==============================] - 11s - loss: 0.4520 - val_loss: 0.4678
Epoch 7/10
49502/49502 [==============================] - 11s - loss: 0.4273 - val_loss: 0.4671
Epoch 8/10
49502/49502 [==============================] - 11s - loss: 0.4157 - val_loss: 0.4602
Epoch 9/10
49502/49502 [==============================] - 11s - loss: 0.4012 - val_loss: 0.4592
Epoch 10/10
49502/49502 [==============================] - 11s - loss: 0.3990 - val_loss: 0.4592
11808/12376 [===========================>..] - ETA: 0s[[ 231   10    2    0    1   28   14   41   83]
 [   1 2698  431   42    5    3   25   10    7]
 [   1  675  805   30    1    1   29    7    1]
 [   1  160   81  285    5   12   11    2    0]
 [   0    8    1    0  524    0    0    2    0]
 [  14   13    1    6    0 2695   32   25   24]
 [   8   57   35    7    0   28  382   18    3]
 [  25    8    3    0    2   39   23 1568   27]
 [  37   15    1    2    0   19    7   28  950]]
classification report results:
             precision    recall  f1-score   support

          0       0.73      0.56      0.63       410
          1       0.74      0.84      0.79      3222
          2       0.59      0.52      0.55      1550
          3       0.77      0.51      0.61       557
          4       0.97      0.98      0.98       535
          5       0.95      0.96      0.96      2810
          6       0.73      0.71      0.72       538
          7       0.92      0.93      0.92      1695
          8       0.87      0.90      0.88      1059

avg / total       0.82      0.82      0.82     12376

log-loss for classifier: 0.4592365163666902

In [42]:
print('log-loss for classifier: {}'.format(log_loss(y_pred=np.clip(model1.predict(X_val.values),a_min=0.0001,a_max=0.9999),y_true=y_val)))


log-loss for classifier: 0.4583844790366684

In [43]:
test_pred = np.clip(,a_min=0.001,a_max=0.999)
subm = pd.DataFrame(test_pred)
subm.columns = ['class_'+ str(x) for x in range(1,10)]
subm['id'] = te_ids
subm.to_csv('../subm/ANN_0.4583.csv',index=False)

In [ ]: