"""
**In this notebook I have done EDA and model prediction with LightGBM. Minimal feature engineering has been done. This notebook will be further enhanced to add feature engineering and other models for prediction**
"""
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

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

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
#import necessary libraries
from pandas_profiling import ProfileReport
import matplotlib.pyplot as plt

from fancyimpute import KNN
from sklearn.preprocessing import OrdinalEncoder
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
import lightgbm as lgb
train=pd.read_csv("/kaggle/input/widsdatathon2021/TrainingWiDS2021.csv")
test=pd.read_csv("/kaggle/input/widsdatathon2021/UnlabeledWiDS2021.csv")
train.drop(["Unnamed: 0"],axis=1,inplace=True)
test.drop(["Unnamed: 0"],axis=1,inplace=True)
#missingno is library to visulaize missing data
import missingno as msno
msno.bar(train)
#PandasProfiling is a very handy tool for EDA
trainprofile = ProfileReport(train,'EDA')
trainprofile
import plotly.express as px
fig = px.histogram(train[['age','gender','diabetes_mellitus','bmi']].dropna(), x="age", y="diabetes_mellitus", color="gender",
                   marginal="box", hover_data=train[['age','gender','diabetes_mellitus','bmi']].columns)
fig.show()
#Seperate categorical and numerical variables
cattrain=train.select_dtypes('object')
cattest=test.select_dtypes('object')
numtrain=train.select_dtypes('number')
numtest=test.select_dtypes('number')
#encoding categorical train variables

encoder = OrdinalEncoder()
imputer = KNN()
# create a list of categorical columns to iterate over
cat_cols = cattrain.columns

def encode(data):
    '''function to encode non-null data and replace it in the original data'''
    #retains only non-null values
    nonulls = np.array(data.dropna())
    #reshapes the data for encoding
    impute_reshape = nonulls.reshape(-1,1)
    #encode data
    impute_ordinal = encoder.fit_transform(impute_reshape)
    #Assign back encoded values to non-null values
    data.loc[data.notnull()] = np.squeeze(impute_ordinal)
    return data

#create a for loop to iterate through each column in the data
for columns in cat_cols:
    encode(cattrain[columns])
#encoding categorical test variables

encoder = OrdinalEncoder()
imputer = KNN()
# create a list of categorical columns to iterate over
cat_cols = cattest.columns

def encode(data):
    '''function to encode non-null data and replace it in the original data'''
    #retains only non-null values
    nonulls = np.array(data.dropna())
    #reshapes the data for encoding
    impute_reshape = nonulls.reshape(-1,1)
    #encode data
    impute_ordinal = encoder.fit_transform(impute_reshape)
    #Assign back encoded values to non-null values
    data.loc[data.notnull()] = np.squeeze(impute_ordinal)
    return data

#create a for loop to iterate through each column in the data
for columns in cat_cols:
    encode(cattest[columns])
train1=pd.concat([cattrain,numtrain], axis=1)
test1=pd.concat([cattest,numtest], axis=1)
#imputing missing values using SimpleImputer

imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
imputer = imputer.fit(train1)
trainfinal = pd.DataFrame(imputer.transform(train1))
trainfinal.columns=train.columns
imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
imputer = imputer.fit(test1)
testfinal = pd.DataFrame(imputer.transform(test1))
testfinal.columns=test.columns
y=trainfinal['diabetes_mellitus']
trainfinal.drop(['diabetes_mellitus'], axis=1, inplace=True)
# Split into training and validation set
x_train, x_val, y_train, y_val = train_test_split(trainfinal, y, test_size = 0.25, random_state = 1)
#Model building
d_train = lgb.Dataset(x_train, label=y_train)
params = {}
params['learning_rate'] = 0.003
params['boosting_type'] = 'gbdt'
params['objective'] = 'binary'
params['metric'] = 'binary_logloss'
params['sub_feature'] = 0.5
params['num_leaves'] = 100
params['min_data'] = 50
params['max_depth'] = 10
clf = lgb.train(params, d_train, 100)
#Prediction
y_pred=clf.predict(x_val)
y_pred1=np.round(y_pred)
#Prediction on Test variables
pred_on_test=clf.predict(testfinal)
solution = pd.read_csv("/kaggle/input/widsdatathon2021/SolutionTemplateWiDS2021.csv")
solution.diabetes_mellitus = pred_on_test
solution.to_csv("submissionlgbm.csv", index=0)