<jupyter_start><jupyter_text>Importing Libraries.<jupyter_code>#import libraries
import pandas as pd
import numpy as np

#importing liabraries for graphical representation
import matplotlib.pyplot as plt
import seaborn as sns
from pandas.plotting import scatter_matrix
import matplotlib.patches as mpatches
from matplotlib import rcParams
from matplotlib.cm import rainbow
%matplotlib inline

#Packages for feature engineering
from sklearn.model_selection import train_test_split, StratifiedKFold,cross_val_score
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from statsmodels.tools.eval_measures import rmse
from scipy import stats
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix
import statsmodels.api as sm
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.feature_selection import RFE
from xgboost import XGBClassifier



import lightgbm as lgb

from sklearn.model_selection import KFold
from sklearn.metrics import f1_score, roc_auc_score

import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Importing the Data set in a dataframe - travel<jupyter_code>train = pd.read_csv('dataset/train.csv')
test = pd.read_csv('dataset/test.csv')

sample_submit = pd.read_csv('submit.csv')
train.head(5)<jupyter_output><empty_output><jupyter_text>Basic Data Hygiene Check.<jupyter_code># Checking the distribution of the claims

(train.groupby('loan_default').size()/train.shape[0]).round(4)
rcParams['figure.figsize'] = 8,6
plt.bar(train['loan_default'].unique(), train['loan_default'].value_counts(), color = ['red', 'green'])
plt.xticks([0, 1])
plt.xlabel('Target Classes')
plt.ylabel('Count')
plt.title('Count of each Target Class')
#checking for the data types and file size.

train.info()
# checking for the null values.

train.isnull().sum()<jupyter_output><empty_output><jupyter_text>去除无用字段<jupyter_code># 'branch_id','supplier_id','manufacturer_id','employee_code_id', 'mobileno_flag', 'idcard_flag', 'year_of_birth', 'disbursed_date'
all_columns = [f for f in train.columns if f not in ['customer_id','loan_default', 'branch_id','supplier_id','manufacturer_id','employee_code_id', 'mobileno_flag', 'idcard_flag', 'year_of_birth', 'disbursed_date']]
x_train = train[all_columns]
x_test = test[all_columns]
y_train = train['loan_default']
x_train.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 150000 entries, 0 to 149999
Data columns (total 43 columns):
 #   Column                         Non-Null Count   Dtype  
---  ------                         --------------   -----  
 0   main_account_loan_no           150000 non-null  int64  
 1   main_account_active_loan_no    150000 non-null  int64  
 2   main_account_overdue_no        150000 non-null  int64  
 3   main_account_outstanding_loan  150000 non-null  int64  
 4   main_account_sanction_loan     150000 non-null  int64  
 5   main_account_disbursed_loan    150000 non-null  int64  
 6   sub_account_loan_no            150000 non-null  int64  
 7   sub_account_active_loan_no     150000 non-null  int64  
 8   sub_account_overdue_no         150000 non-null  int64  
 9   sub_account_outstanding_loan   150000 non-null  int64  
 10  sub_account_sanction_loan      150000 non-null  int64  
 11  sub_account_disbursed_loan     150000 non-null  int64  
 12  disbursed_amount              [...]<jupyter_text>Inference:From the above its clear that more then 60% of the data does not have Gender updated. And we also understand that there are no other features that will help us find out whether the individual is male or female.Thus we will drop this feature<jupyter_code># Check for unique Value in the DataFrame.

x_train.nunique()<jupyter_output><empty_output><jupyter_text>Analysis:From the above we can say that Agency Type and Distribution Channel which has just 2 unique values and has dataype object we can use One Hot Encoding and convert them.We will be left with Destination, Agency and Product Name. We will deep dive into this after some analysis of the rest of the data<jupyter_code># Check for the maximum and minimum values to see if there are any invalid.

x_train.describe()<jupyter_output><empty_output><jupyter_text>Preparing and training a baseline model.<jupyter_code># 作为baseline部分仅使用经典的**LightGBM**作为训练模型，我们还能尝试**XGBoost、CatBoost和NN（神经网络）**
def cv_model(clf, train_x, train_y, test_x, clf_name='lgb'):
    folds = 5
    seed = 2021
    kf = KFold(n_splits=folds, shuffle=True, random_state=seed)

    train = np.zeros(train_x.shape[0])
    test = np.zeros(test_x.shape[0])

    cv_scores = []

    for i, (train_index, valid_index) in enumerate(kf.split(train_x, train_y)):
        print('************************************ {} ************************************'.format(str(i+1)))
#         enc = OneHotEncoder(handle_unknown='ignore')
        trn_x, trn_y, val_x, val_y = train_x.iloc[train_index], train_y[train_index], train_x.iloc[valid_index], train_y[valid_index]
#         enc.fit_transform(trn_x)
#         enc.fit_transform(val_x)

        train_matrix = clf.Dataset(trn_x, label=trn_y)
        valid_matrix = clf.Dataset(val_x, label=val_y)

        params = {
            'boosting_type': 'gbdt',
            'objective': 'binary',
            'metric': 'auc',
            'min_child_weight': 5,
#             'num_leaves': 2 ** 7,
            'num_leaves': 20,
            'max_depth':3,
            'lambda_l2': 10,
            'feature_fraction': 0.9,
            'bagging_fraction': 0.9,
            'bagging_freq': 4,
            'learning_rate': 0.01,
            'seed': 2021,
            'num_threads': 8,
            'n_jobs':-1,
            'silent': False,
            'verbose': -1,
        }
#         print("调参1：提高准确率")
        # {'num_leaves': 20, 'max_depth': 3}
#         min_merror = float('Inf')
#         best_params = {}

#         for num_leaves in range(10,50,2):
#             for max_depth in range(1,8,1):
#                 params['num_leaves'] = num_leaves
#                 params['max_depth'] = max_depth

#                 cv_results = clf.cv(
#                                     params,
#                                     train_matrix,
#                                     seed=2018,
#                                     nfold=3,
#                                     metrics=['binary_error'],
#                                     early_stopping_rounds=100,
#                                     verbose_eval=True
#                                     )

#                 mean_merror = pd.Series(cv_results['binary_error-mean']).min()
#                 boost_rounds = pd.Series(cv_results['binary_error-mean']).argmin()

#                 if mean_merror < min_merror:
#                     min_merror = mean_merror
#                     best_params['num_leaves'] = num_leaves
#                     best_params['max_depth'] = max_depth

#         params['num_leaves'] = best_params['num_leaves']
#         params['max_depth'] = best_params['max_depth']
#         print(best_params)
#         break



        model = clf.train(params, train_matrix, 50000, valid_sets=[train_matrix, valid_matrix], verbose_eval=800,early_stopping_rounds=1000)
        val_pred = model.predict(val_x, num_iteration=model.best_iteration)
        test_pred = model.predict(test_x, num_iteration=model.best_iteration)

        # print(list(sorted(zip(features, model.feature_importance("gain")), key=lambda x: x[1], reverse=True))[:20])

        train[valid_index] = val_pred
        test += test_pred / kf.n_splits
        cv_scores.append(roc_auc_score(val_y, val_pred))
        
        print(cv_scores)
       
    print("%s_scotrainre_list:" % clf_name, cv_scores)
    print("%s_score_mean:" % clf_name, np.mean(cv_scores))
    print("%s_score_std:" % clf_name, np.std(cv_scores))
    return train, test



lgb_train, lgb_test = cv_model(lgb, x_train, y_train, x_test)


# 预测结果
sample_submit['loan_default'] = lgb_test
sample_submit['loan_default'] = sample_submit['loan_default'].apply(lambda x:1 if x>0.25 else 0).values
sample_submit.to_csv('ans_lgb.csv', index=False)<jupyter_output>************************************ 1 ************************************
Training until validation scores don't improve for 1000 rounds
[800]	training's auc: 0.653208	valid_1's auc: 0.645507
[1600]	training's auc: 0.662541	valid_1's auc: 0.650525
[2400]	training's auc: 0.668651	valid_1's auc: 0.65224
[3200]	training's auc: 0.673577	valid_1's auc: 0.653253
[4000]	training's auc: 0.678151	valid_1's auc: 0.653418
[4800]	training's auc: 0.682145	valid_1's auc: 0.653543
[5600]	training's auc: 0.685916	valid_1's auc: 0.653626
[6400]	training's auc: 0.689395	valid_1's auc: 0.653452
Early stopping, best iteration is:
[5913]	training's auc: 0.687226	valid_1's auc: 0.653688
[0.6536882449051326]
************************************ 2 ************************************
[LightGBM] [Warning] Unknown parameter: silent
[LightGBM] [Warning] num_threads is set=8, n_jobs=-1 will be ignored. Current value: num_threads=8
Training until validation scores don't improve for 1000 rounds
[800]	training's a[...]<jupyter_text>Train-Test Split<jupyter_code>X_train, X_test, Y_train, Y_test = train_test_split(X,Y, train_size = 0.7, random_state = 7)<jupyter_output><empty_output><jupyter_text>Random Forest Classifier.<jupyter_code>rf = RandomForestClassifier()
rf.fit(X_train, Y_train)
y_pred_rf = rf.predict(X_test)
print(accuracy_score(Y_test, y_pred_rf))
print(classification_report(Y_test, y_pred_rf))<jupyter_output>0.9943811205650988
              precision    recall  f1-score   support

           0       1.00      0.99      1.00     14972
           1       0.97      1.00      0.99      3715

    accuracy                           0.99     18687
   macro avg       0.99      1.00      0.99     18687
weighted avg       0.99      0.99      0.99     18687<jupyter_text>Logistic Regression<jupyter_code>logr = LogisticRegression()
logr.fit(X_train,Y_train)
y_pred = logr.predict(X_test)
print(accuracy_score(Y_test, y_pred))
print(classification_report(Y_test, y_pred))<jupyter_output>0.8174131749344464
              precision    recall  f1-score   support

           0       0.83      0.97      0.90     14972
           1       0.63      0.19      0.29      3715

    accuracy                           0.82     18687
   macro avg       0.73      0.58      0.59     18687
weighted avg       0.79      0.82      0.78     18687
{'fraction_non_alphanumeric': 0.1180373831775701, 'fraction_numerical': 0.054299065420560746, 'mean_word_length': 3.1284722222222223, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '218293', 'license': 'Apache-2.0', 'n_tokens_mistral': 3991, 'n_tokens_neox': 3533, 'n_words': 789, 'path': 'car-loan/rfc.ipynb', 'repo_name': 'ironartisan/competition', 'size': '10700'}