
# coding: utf-8

# In[ ]:


import gc
import pandas as pd
import numpy as np
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold


# In[ ]:


X = np.load('./feats/X_train.npy')
y = np.load('./feats/labels.npy')
X_test = np.load('./feats/X_test.npy')


# In[ ]:


train_cnn_1 = pd.read_pickle('./feats/train_feats_cnn.pkl')
test_cnn_1 = pd.read_pickle('./feats/test_feats_cnn.pkl')


# In[ ]:


train_lgb_1 = pd.read_pickle('./feats/train_feats_lgb_1.pkl')
test_lgb_1 = pd.read_pickle('./feats/test_feats_lgb_1.pkl')

train_lgb_2 = pd.read_pickle('./feats/train_feats_lgb_2.pkl')
test_lgb_2 = pd.read_pickle('./feats/test_feats_lgb_2.pkl')

train_lgb_3 = pd.read_pickle('./feats/train_feats_lgb_3.pkl')
test_lgb_3 = pd.read_pickle('./feats/test_feats_lgb_3.pkl')

train_lgb_4 =  pd.read_pickle('./feats/train_feats_lgb_4.pkl')
test_lgb_4 = pd.read_pickle('./feats/test_feats_lgb_4.pkl')

train_lgb_5 =  pd.read_pickle('./feats/train_feats_lgb_5.pkl')
test_lgb_5 = pd.read_pickle('./feats/test_feats_lgb_5.pkl')


# In[ ]:


train_lr = np.asarray(pd.read_csv('./feats/train_feats_lr_prob.csv').drop('file_id', axis=1))
test_lr = np.asarray(pd.read_csv('./feats/test_feats_lr_prob.csv').drop('file_id', axis=1))


# In[ ]:


X = np.hstack([train_cnn_1, train_lgb_1, train_lgb_2, train_lgb_3, train_lgb_4, train_lgb_5, train_lr])
X_test = np.hstack([test_cnn_1, test_lgb_1, test_lgb_2, test_lgb_3, test_lgb_4, test_lgb_5, test_lr])


# In[ ]:


train_probs = np.zeros((len(X), 8))
test_probs = np.zeros((len(X_test), 8))

skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
for i,(i_train, i_valid) in enumerate(skf.split(X, y)):
    print ('FOLD: ',i)
    
    X_train, y_train = X[i_train], y[i_train]
    X_valid, y_valid = X[i_valid], y[i_valid]
    
    dtrain = lgb.Dataset(X_train, y_train) 
    dvalid   = lgb.Dataset(X_valid, y_valid, reference = dtrain)   
    
    params = {
            'task':'train', 
            'boosting_type':'gbdt',
            'num_leaves': 15,
            'objective': 'multiclass',
            'num_class':8,
            'learning_rate': 0.007,
            'feature_fraction': 0.85,
            'subsample':0.85,
            'num_threads': 54,
            'metric':'multi_logloss',
            'seed':42,
            'device': 'gpu',
            'device_id': 0,
            'gpu_platform_id': 0,
        }  
    model = lgb.train(params, dtrain, num_boost_round=5000,
                      valid_sets=[dtrain,dvalid], verbose_eval=100, 
                      early_stopping_rounds=100)  
    
    pred_test = model.predict(X_test)
    pred_val = model.predict(X_valid)

    train_probs[i_valid] = pred_val
    test_probs += pred_test

test_probs/=5.0


# In[ ]:


-np.mean(np.log(train_probs[np.arange(len(train_probs)), y]))


# In[ ]:


res = pd.DataFrame(test_probs, columns=['prob0','prob1','prob2','prob3','prob4','prob5','prob6','prob7'])
res.index = range(1, len(res)+1)
res.index.name='file_id'
res.to_csv('./submit/submit_lgbx5_cnn_lr.csv', index = True, header=True)

