<jupyter_start><jupyter_text>NB: Every line of code that has been commented out does not contribute to the best submission and hence can be ignored<jupyter_code># import all necessary packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.impute import KNNImputer
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix
from sklearn.preprocessing import StandardScaler, OneHotEncoder, Normalizer
from sklearn.pipeline import Pipeline
from catboost import CatBoostClassifier

%matplotlib inline
date_columns = list(pd.read_csv('Train.csv').columns[2:6])
train = pd.read_csv('Train.csv', parse_dates=date_columns, dayfirst=True)
# view general structure of the data
train.head(10)
train.info()
# summary: null values only exist in the 'NumberOfDependency' column
# Feature Generation: Extract year, month and day from each individual date column

train['ApprovalYear'] = pd.DatetimeIndex(train['ApprovalDate']).year
train['ApprovalMonth'] = pd.DatetimeIndex(train['ApprovalDate']).month
train['ApprovalDay'] = pd.DatetimeIndex(train['ApprovalDate']).day

train['MaturityYear'] = pd.DatetimeIndex(train['MaturityDate']).year
train['MaturityMonth'] = pd.DatetimeIndex(train['MaturityDate']).month
train['MaturityDay'] = pd.DatetimeIndex(train['MaturityDate']).day

train['ActualFirstDisbursementYear'] = pd.DatetimeIndex(train['ActualFirstDisbursementDate']).year
train['ActualFirstDisbursementMonth'] = pd.DatetimeIndex(train['ActualFirstDisbursementDate']).month
train['ActualFirstDisbursementDay'] = pd.DatetimeIndex(train['ActualFirstDisbursementDate']).day
# impute missing values in 'NumberOfDependency' column using K-Nearest Neighbours Imputer

imputer = KNNImputer(n_neighbors=2, weights='uniform')

train['NumberOfDependency'] = imputer.fit_transform(np.array(train['NumberOfDependency']).reshape(-1,1))
train['NumberOfDependency'] = train['NumberOfDependency'].apply(np.ceil)
# inspect the balance of the data for the target variable
train['Target'].value_counts().plot(kind='bar')
# extract birth year, month and day from BirthDate column

train['BirthDate'] = train['BirthDate'].apply(lambda x: x.split())

month = []
year = []
 
for i, j in train['BirthDate']:
    month.append(i)
    year.append(j)
    
train['BirthMonth'], train['BirthYear'] = month, year
# drop the original columns from which additional features were extracted from

train = train.drop(columns=['ActualFirstDisbursementDate', 'ApprovalDate', 'FirstInstalmentDate', 'MaturityDate'])
train = train.drop(columns=['BirthDate'])
# drop Loan_ID and BillingFrequency (it does not contribute to the model since all samples have the same value)

train = train.drop(columns=['Loan_ID', 'BillingFrequency'])
# inspect final data before transformation
train
# feature transformers for numerical and categorical variables

numeric_transformer = Pipeline(steps=[('scaler', StandardScaler())])
categorical_transformer = Pipeline(steps=[('ohe',OneHotEncoder(handle_unknown='ignore', sparse=False))])
train.columns
# define the numeric and categorical features
numeric_features = ['LoanAmount']
categorical_features = ['Loan Category Code', 'LoanInterestrate', 'NumberOfDependency', 
                        'OtherLoansBalance', 'MaritalStatus', 'Gender', 'ApprovalYear', 'ApprovalMonth',
                       'ApprovalDay', 'MaturityYear', 'MaturityMonth', 'MaturityDay',
                        'ActualFirstDisbursementYear', 'ActualFirstDisbursementMonth',
                        'ActualFirstDisbursementDay', 'BirthMonth', 'BirthYear']
# define column transformer object
from sklearn.compose import ColumnTransformer
 
preprocessor = ColumnTransformer(transformers=[
    ('num', numeric_transformer, numeric_features),
    ('cat', categorical_transformer, categorical_features)])
# ensuring that values for categorical features are of type str, to aid the encoding process
for category in categorical_features:
    if train[category].dtype != 'object':
         train[category] = train[category].apply(lambda x: str(x))
# check that the datatypes have been successfully updated
train.info()
# prepare data for modelling
X = train.drop(columns=['Target'])
y = train['Target']
# generate train and test splits with a 20% split ratio

from sklearn.model_selection import train_test_split
 
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=0)
# X_train = preprocessor.fit_transform(X_train)
# X_test = preprocessor.transform(X_test)<jupyter_output><empty_output><jupyter_text>XGBoost<jupyter_code># params = {'objective': 'binary:logistic',
#           'max_depth': 20,
#           'alpha': 10,
#           'learning_rate': 0.1,
#           'n_estimators': 100}
 
# clf = XGBClassifier(**params)
# clf.fit(X_train, y_train)
# y_pred = clf.predict(X_test)
# print("Accuracy: {:.2f}%".format(100*accuracy_score(y_test, y_pred)))
# print("ROC AUC Score: {:.2f}%".format(100*roc_auc_score(y_test, clf.predict_proba(X_test)[:,1])))
# sns.heatmap(confusion_matrix(y_pred,y_test), annot=True, fmt='.0f', cmap='Blues')
# # cross validation
# from xgboost import DMatrix, cv
 
# data_dmatrix = DMatrix(data=preprocessor.transform(X),label=y)
 
# params = {'objective': 'binary:logistic',
#           'max_depth': 20,
#           'alpha': 5,
#           'learning_rate': 0.1}
 
# xgb_cv = cv(dtrain=data_dmatrix, params=params, nfold=3, num_boost_round=500, 
#             metrics='auc', as_pandas=True, seed=0,verbose_eval=True)
# params = {'objective': 'binary:logistic',
#           'max_depth': 20,
#           'alpha': 1,
#           'learning_rate': 0.1,
#           'n_estimators': 500,
#           'colsample_bytree':  0.9,
#           'verbosity': 1}
 
# clf = XGBClassifier(**params)
# clf.fit(preprocessor.transform(X), y)
# accuracy_score(y, clf.predict(preprocessor.transform(X)))<jupyter_output><empty_output><jupyter_text>Catboost Model<jupyter_code># define the baseline catboost model

params = {'loss_function': 'Logloss',
          'eval_metric': 'AUC',
          'cat_features': categorical_features,
          'random_seed': 0}

cat_clf = CatBoostClassifier(**params)
 
cat_clf.fit(X_train, y_train, 
        eval_set=(X_test, y_test), 
        verbose=True)
# update the model so it fits with 600 iterations and run it on the whole dataset

params = {'loss_function': 'Logloss',
          'eval_metric': 'AUC',
          'cat_features': categorical_features,
          'random_seed': 0,
          'n_estimators': 600}

cat_clf = CatBoostClassifier(**params)
cat_clf.fit(X, y)
# feature importance

cat_clf.get_feature_importance(prettified=True)<jupyter_output><empty_output><jupyter_text>Submission<jupyter_code># load the test set and transform it

test_columns = pd.read_csv('Test.csv').columns
test = pd.read_csv('Test.csv', parse_dates=list(test_columns[2:6]), dayfirst=True)

IDs = test['Loan_ID']

test['ApprovalYear'] = pd.DatetimeIndex(test['ApprovalDate']).year
test['ApprovalMonth'] = pd.DatetimeIndex(test['ApprovalDate']).month
test['ApprovalDay'] = pd.DatetimeIndex(test['ApprovalDate']).day
 
test['MaturityYear'] = pd.DatetimeIndex(test['MaturityDate']).year
test['MaturityMonth'] = pd.DatetimeIndex(test['MaturityDate']).month
test['MaturityDay'] = pd.DatetimeIndex(test['MaturityDate']).day
 
test['ActualFirstDisbursementYear'] = pd.DatetimeIndex(test['ActualFirstDisbursementDate']).year
test['ActualFirstDisbursementMonth'] = pd.DatetimeIndex(test['ActualFirstDisbursementDate']).month
test['ActualFirstDisbursementDay'] = pd.DatetimeIndex(test['ActualFirstDisbursementDate']).day

test['NumberOfDependency'] = imputer.transform(np.array(test['NumberOfDependency']).reshape(-1,1))
test['NumberOfDependency'] = test['NumberOfDependency'].apply(np.ceil)

test['BirthDate'] = test['BirthDate'].apply(lambda x: x.split())
 
month = []
year = []
 
for i, j in test['BirthDate']:
    month.append(i)
    year.append(j)

test['BirthMonth'], test['BirthYear'] = month, year
 
test = test.drop(columns=['ActualFirstDisbursementDate', 'ApprovalDate', 'FirstInstalmentDate', 'MaturityDate'])
test = test.drop(columns=['BirthDate'])
test
for category in categorical_features:
    if test[category].dtype != 'object':
         test[category] = test[category].apply(lambda x: str(x))
# test = test.drop(columns=['Loan_ID', 'BillingFrequency'])
# test = preprocessor.transform(test)
# preds = clf.predict_proba(test)[:,1]

# data = {'Loan_ID': IDs,
#         'Target': preds}
 
# test_df = pd.DataFrame(data, columns=['Loan_ID','Target'])

# test_df.to_csv('Submission6_xgboost.csv',index=False)
test = test.drop(columns=['Loan_ID', 'BillingFrequency'])
# make predictions on the test set
preds = cat_clf.predict_proba(test)[:,1]

data = {'Loan_ID': IDs,
        'Target': preds}
 
test_df = pd.DataFrame(data, columns=['Loan_ID','Target'])
# generate submission file
test_df.to_csv('Submission_catboost.csv',index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1371981751418716, 'fraction_numerical': 0.007455213085568043, 'mean_word_length': 5.834980988593156, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '430154', 'license': 'MIT', 'n_tokens_mistral': 3007, 'n_tokens_neox': 2904, 'n_words': 594, 'path': 'Umoja Hack/Umoja Hack.ipynb', 'repo_name': 'Nkosana-263/Zindi-Challenges', 'size': '8987'}