[PYTHON] [SIGNATE] Bank customer targeting @ learning

[SIGNATE] I am learning what machine learning is by targeting customers at banks.


preparation work

#Import the library used this time
import numpy as np
import pandas as pd
import xgboost as xgb
from xgboost import XGBClassifier
from sklearn.metrics import log_loss, accuracy_score
from sklearn.model_selection import KFold

Data reading, preprocessing

#Reading training data and test data
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')

#Divide the training data into features and objective variables
train_x = train.drop(['y'], axis=1)
train_y = train['y']

#The test data is only features, so you can leave it as it is.
test_x = test.copy()

#Exclude the variable id
train_x = train_x.drop(['id'], axis=1)
test_x = test_x.drop(['id'], axis=1)

#Set a dictionary for conversion of each feature
marital_mapping = {'married': 3, 'single': 2, 'divorcedw': 1}
education_mapping = {'secondary': 4, 'tertiary': 3, 'primary': 2, 'unknown': 1}
default_mapping = {'no': 0, 'yes': 1}
housing_mapping = {'no': 0, 'yes': 1}
loan_mapping = {'no': 0, 'yes': 1}
month_mapping = {'jan': 1, 'feb': 2, 'mar': 3, 'apr': 4, 'may': 5, 'jun': 6, 'jul': 7, 'aug': 8, 'sep': 9, 'oct': 10, 'nov': 11, 'dec': 12}

#Convert each feature of training data
train_x['marital'] = train_x['marital'].map(marital_mapping)
train_x['education'] = train_x['education'].map(education_mapping)
train_x['default'] = train_x['default'].map(default_mapping)
train_x['housing'] = train_x['housing'].map(housing_mapping)
train_x['loan'] = train_x['loan'].map(loan_mapping)
train_x['month'] = train_x['month'].map(month_mapping)

#Convert each feature of training data
test_x['marital'] = test_x['marital'].map(marital_mapping)
test_x['education'] = test_x['education'].map(education_mapping)
test_x['default'] = test_x['default'].map(default_mapping)
test_x['housing'] = test_x['housing'].map(housing_mapping)
test_x['loan'] = test_x['loan'].map(loan_mapping)
test_x['month'] = test_x['month'].map(month_mapping)

#Exclude variables
train_x = train_x.drop(['job','marital','education','default','housing','loan','contact','campaign','previous'], axis=1)
test_x = test_x.drop(['job','marital','education','default','housing','loan','contact','campaign','previous'], axis=1)

#success('success')Or other('non-success')Mapping with
for i in range(0,len(train_x)):
    if train_x.loc[i,'poutcome']=='success':
        train_x.loc[i,'poutcome'] = 'non-success'

#success('success')Or other('non-success')Mapping with
for i in range(0,len(test_x)):
    if test_x.loc[i,'poutcome']=='success':
        test_x.loc[i,'poutcome'] = 'non-success'

poutcome_mapping = {'non-success': 0, 'success': 1}
train_x['poutcome'] = train_x['poutcome'].map(poutcome_mapping)

poutcome_mapping = {'non-success': 0, 'success': 1}
test_x['poutcome'] = test_x['poutcome'].map(poutcome_mapping)

Machine learning, validation

#List to store the score for each fold
scores_accuracy = []
scores_logloss = []

#Perform cross-validation
#Dividing the training data into four parts and using one of them as validation data is repeated by changing the validation data.
kf = KFold(n_splits=4, shuffle=True, random_state=71)
for tr_idx, va_idx in kf.split(train_x):
    #Divide the training data into training data and validation data
    tr_x, va_x = train_x.iloc[tr_idx], train_x.iloc[va_idx]
    tr_y, va_y = train_y.iloc[tr_idx], train_y.iloc[va_idx]

    #Train the model
    model = XGBClassifier(n_estimators=20, random_state=71)
    model.fit(tr_x, tr_y)

    #Output the predicted value of validation data with probability
    va_pred = model.predict_proba(va_x)[:, 1]

    #Calculate the score in the validation data
    logloss = log_loss(va_y, va_pred)
    accuracy = accuracy_score(va_y, va_pred > 0.5)

    #Save the score for that fold

Prediction result







Creating a file for submission

pred = model.predict(test_x)
pred_label = np.where(pred > 0.5,1,0)
submission = pd.DataFrame({'id': test['id'], 'y': pred_label})
submission.to_csv('submission_first.csv', index=False, header=None)

Submit to SIGNATE

I've done various things, but it's still not 70% ... What's wrong ... image.png

Recommended Posts

[SIGNATE] Bank customer targeting @ learning
SIGNATE [1st _Beginner Limited Competition] Participated in bank customer targeting
SIGNATE [1st _Beginner Limited Competition] How to Solve Bank Customer Targeting