
# coding: utf-8

# In[ ]:


import os
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_probs = np.zeros(shape = (len(X),8))
test_probs = np.zeros(shape = (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.05,
            'feature_fraction': 0.85,
            'subsample':0.85,
            'num_threads': 32,
            'metric':'multi_logloss',
            'seed':100,
            'device': 'gpu',
            'device_id': 0,
            'gpu_platform_id': 0,
        }  
    model = lgb.train(params, dtrain, num_boost_round=100000,
                      valid_sets=[dtrain,dvalid],
                      verbose_eval=100, early_stopping_rounds=100)  
    
    pred_val = model.predict(X_valid)
    pred_test = model.predict(X_test)
    
    train_probs[i_valid] = pred_val
    test_probs += pred_test
test_probs /= 5.0


# In[ ]:


pd.to_pickle(train_probs,'./feats/train_feats_lgb_1.pkl')
pd.to_pickle(test_probs,'./feats/test_feats_lgb_1.pkl')


# In[ ]:


skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

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

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': 31,
            'objective': 'multiclass',
            'num_class':8,
            'learning_rate': 0.05,
            'feature_fraction': 0.85,
            'subsample':0.85,
            'num_threads': 32,
            'metric':'multi_logloss',
            'seed':100,
            'device': 'gpu',
            'device_id': 0,
            'gpu_platform_id': 0,
        }  
    model = lgb.train(params, dtrain, num_boost_round=100000,
                      valid_sets=[dtrain,dvalid],
                      verbose_eval=100, early_stopping_rounds=100)  
    
    pred_val = model.predict(X_valid)
    pred_test = model.predict(X_test)
    
    train_probs[i_valid] = pred_val
    test_probs += pred_test
test_probs /= 5.0


# In[ ]:


pd.to_pickle(train_probs,'./feats/train_feats_lgb_2.pkl')
pd.to_pickle(test_probs,'./feats/test_feats_lgb_2.pkl')


# In[ ]:


skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

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

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': 31,
            'objective': 'multiclass',
            'num_class':8,
            'learning_rate': 0.045,
            'feature_fraction': 0.8,
            'subsample':0.8,
            'num_threads': 32,
            'metric':'multi_logloss',
            'seed':100,
            'device': 'gpu',
            'device_id': 0,
            'gpu_platform_id': 0,
        }  
    model = lgb.train(params, dtrain, num_boost_round=100000,
                      valid_sets=[dtrain,dvalid],
                      verbose_eval=100, early_stopping_rounds=100)  
    
    pred_val = model.predict(X_valid)
    pred_test = model.predict(X_test)
    
    train_probs[i_valid] = pred_val
    test_probs += pred_test
test_probs /= 5.0


# In[ ]:


pd.to_pickle(train_probs,'./feats/train_feats_lgb_3.pkl')
pd.to_pickle(test_probs,'./feats/test_feats_lgb_3.pkl')


# In[ ]:


train_probs = np.zeros(shape = (len(X),8))
test_probs = np.zeros(shape = (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': 63,
            'objective': 'multiclass',
            'num_class':8,
            'learning_rate': 0.045,
            'feature_fraction': 0.5,
            'subsample':0.7,
            'num_threads': 54,
            'metric':'multi_logloss',
            'seed':100,
            'device': 'gpu',
            'device_id': 0,
            'gpu_platform_id': 0,
        }  
    model = lgb.train(params, dtrain, num_boost_round=100000,
                      valid_sets=[dtrain,dvalid],
                      verbose_eval=100, early_stopping_rounds=100)  
    
    pred_val = model.predict(X_valid)
    pred_test = model.predict(X_test)
    
    train_probs[i_valid] = pred_val
    test_probs += pred_test
test_probs /= 5.0


# In[ ]:


pd.to_pickle(train_probs,'./feats/train_feats_lgb_4.pkl')
pd.to_pickle(test_probs,'./feats/test_feats_lgb_4.pkl')


# In[ ]:


train_probs = np.zeros(shape = (len(X),8))
test_probs = np.zeros(shape = (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': 25,
            'objective': 'multiclass',
            'num_class':8,
            'learning_rate': 0.05,
            'feature_fraction': 0.7,
            'subsample':0.7,
            'num_threads': 15,
            'metric':'multi_logloss',
            'seed':100,
            'device': 'gpu',
            'device_id': 0,
            'gpu_platform_id': 0,
        }  
    model = lgb.train(params, dtrain, num_boost_round=100000,
                      valid_sets=[dtrain,dvalid],
                      verbose_eval=100, early_stopping_rounds=100)  
    
    pred_val = model.predict(X_valid)
    pred_test = model.predict(X_test)
    
    train_probs[i_valid] = pred_val
    test_probs += pred_test
test_probs /= 5.0


# In[ ]:


pd.to_pickle(train_probs, './feats/train_feats_lgb_5.pkl')
pd.to_pickle(test_probs, './feats/test_feats_lgb_5.pkl')

