image_output_1.png
from sklearn.model_selection import KFold

from sklearn.preprocessing import LabelEncoder

import lightgbm as lgb

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import random

import seaborn as sns

df = pd.read_csv('../input/aipowered-literature-review-csvs/kaggle/working/TIE/Proportion of patients who were asymptomatic.csv')

df = df.rename(columns={'Unnamed: 0': 'unnamed', 'Asymptomatic Proportion': 'asymptomatic'})

dfcorr = df.corr()
dfcorr

SEED = 99
random.seed(SEED)
np.random.seed(SEED)

dfmodel = df.copy()
for col in dfmodel.columns[dfmodel.dtypes == 'object']:
    le = LabelEncoder()
    dfmodel[col] = dfmodel[col].astype(str)
    le.fit(dfmodel[col])
    dfmodel[col] = le.transform(dfmodel[col])

dfmodel.columns = [''.join((c if c.isalnum() else '_' for c in str(x))) for x in dfmodel.columns]

X = dfmodel.drop(['unnamed', 'asymptomatic'], axis=1)
y = dfmodel['unnamed']

lgb_params = {'objective': 'binary', 'metric': 'auc', 'n_jobs': -1, 'learning_rate': 0.005, 'num_leaves': 20, 'max_depth': -1, 'subsample': 0.9, 'n_estimators': 2500, 'seed': SEED, 'early_stopping_rounds': 100}

K = 5
folds = KFold(K, shuffle=True, random_state=SEED)
best_scorecv = 0
best_iteration = 0
for fold, (train_index, test_index) in enumerate(folds.split(X, y)):
    X_traincv, X_testcv = (X.iloc[train_index], X.iloc[test_index])
    y_traincv, y_testcv = (y.iloc[train_index], y.iloc[test_index])
    train_data = lgb.Dataset(X_traincv, y_traincv)
    val_data = lgb.Dataset(X_testcv, y_testcv)
    LGBM = lgb.train(lgb_params, train_data, valid_sets=[train_data, val_data], verbose_eval=250)
    best_scorecv += LGBM.best_score['valid_1']['auc']
    best_iteration += LGBM.best_iteration
best_scorecv /= K
best_iteration /= K

lgb_params = {'objective': 'binary', 'metric': 'auc', 'n_jobs': -1, 'learning_rate': 0.05, 'num_leaves': 20, 'max_depth': -1, 'subsample': 0.9, 'n_estimators': round(best_iteration), 'seed': SEED, 'early_stopping_rounds': None}
train_data_final = lgb.Dataset(X, y)
LGBM = lgb.train(lgb_params, train_data)

print(LGBM)