!pip install numpy
!pip install pandas
!pip install sklearn
Import necessary packages¶
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
Download data¶
The first step is to download out train test data. We will be training a classifier on the train data and make predictions on test data. We submit our predictions
!wget https://s3.eu-central-1.wasabisys.com/aicrowd-public-datasets/aicrowd_educational_dcrcl/data/public/test.csv
!wget https://s3.eu-central-1.wasabisys.com/aicrowd-public-datasets/aicrowd_educational_dcrcl/data/public/train.csv
train_data = pd.read_csv('train.csv')
Analyse Data¶
train_data.head()
LIMIT_BAL | SEX | EDUCATION | MARRIAGE | AGE | PAY_0 | PAY_2 | PAY_3 | PAY_4 | PAY_5 | ... | BILL_AMT4 | BILL_AMT5 | BILL_AMT6 | PAY_AMT1 | PAY_AMT2 | PAY_AMT3 | PAY_AMT4 | PAY_AMT5 | PAY_AMT6 | default payment next month | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 30000 | 2 | 2 | 1 | 38 | 0 | 0 | 0 | 0 | 0 | ... | 22810 | 25772 | 26360 | 1650 | 1700 | 1400 | 3355 | 1146 | 0 | 0 |
1 | 170000 | 1 | 4 | 1 | 28 | 0 | 0 | 0 | -1 | -1 | ... | 11760 | 0 | 4902 | 14000 | 5695 | 11760 | 0 | 4902 | 6000 | 0 |
2 | 340000 | 1 | 1 | 2 | 38 | 0 | 0 | 0 | -1 | -1 | ... | 1680 | 1920 | 9151 | 5000 | 7785 | 1699 | 1920 | 9151 | 187000 | 0 |
3 | 140000 | 2 | 2 | 2 | 29 | 0 | 0 | 0 | 2 | 0 | ... | 65861 | 64848 | 64936 | 3000 | 8600 | 6 | 2500 | 2500 | 2500 | 0 |
4 | 130000 | 2 | 2 | 1 | 42 | 2 | 2 | 2 | 0 | 0 | ... | 126792 | 103497 | 96991 | 6400 | 0 | 4535 | 3900 | 4300 | 3700 | 1 |
5 rows × 24 columns
Here we use the describe
function to get an understanding of the data. It shows us the distribution for all the columns. You can use more functions like info()
to get useful info.
train_data.describe()
#train_data.info()
LIMIT_BAL | SEX | EDUCATION | MARRIAGE | AGE | PAY_0 | PAY_2 | PAY_3 | PAY_4 | PAY_5 | ... | BILL_AMT4 | BILL_AMT5 | BILL_AMT6 | PAY_AMT1 | PAY_AMT2 | PAY_AMT3 | PAY_AMT4 | PAY_AMT5 | PAY_AMT6 | default payment next month | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count | 25500.000000 | 25500.000000 | 25500.000000 | 25500.000000 | 25500.000000 | 25500.000000 | 25500.000000 | 25500.000000 | 25500.000000 | 25500.000000 | ... | 25500.000000 | 25500.000000 | 25500.000000 | 25500.000000 | 2.550000e+04 | 25500.000000 | 25500.000000 | 25500.000000 | 25500.000000 | 25500.000000 |
mean | 167436.458039 | 1.604667 | 1.852824 | 1.551961 | 35.503333 | -0.016275 | -0.131882 | -0.166706 | -0.218667 | -0.264157 | ... | 43139.224941 | 40252.920588 | 38846.415529 | 5690.801373 | 5.986709e+03 | 5246.605294 | 4829.790078 | 4810.296706 | 5187.016549 | 0.220902 |
std | 129837.118639 | 0.488932 | 0.791803 | 0.522754 | 9.235048 | 1.126813 | 1.196710 | 1.192883 | 1.168375 | 1.132166 | ... | 64214.508636 | 60789.101393 | 59397.443604 | 17070.733348 | 2.402498e+04 | 18117.236738 | 16021.336645 | 15505.873498 | 17568.450557 | 0.414863 |
min | 10000.000000 | 1.000000 | 0.000000 | 0.000000 | 21.000000 | -2.000000 | -2.000000 | -2.000000 | -2.000000 | -2.000000 | ... | -170000.000000 | -81334.000000 | -209051.000000 | 0.000000 | 0.000000e+00 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
25% | 50000.000000 | 1.000000 | 1.000000 | 1.000000 | 28.000000 | -1.000000 | -1.000000 | -1.000000 | -1.000000 | -1.000000 | ... | 2360.000000 | 1779.250000 | 1280.000000 | 1000.000000 | 8.635000e+02 | 390.000000 | 292.750000 | 256.750000 | 113.750000 | 0.000000 |
50% | 140000.000000 | 2.000000 | 2.000000 | 2.000000 | 34.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | ... | 19033.000000 | 18085.000000 | 17129.000000 | 2100.000000 | 2.010000e+03 | 1800.000000 | 1500.000000 | 1500.000000 | 1500.000000 | 0.000000 |
75% | 240000.000000 | 2.000000 | 2.000000 | 2.000000 | 42.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | ... | 54084.750000 | 50080.750000 | 49110.500000 | 5006.000000 | 5.000000e+03 | 4507.000000 | 4001.250000 | 4024.000000 | 4000.000000 | 0.000000 |
max | 1000000.000000 | 2.000000 | 6.000000 | 3.000000 | 79.000000 | 8.000000 | 8.000000 | 8.000000 | 8.000000 | 8.000000 | ... | 891586.000000 | 927171.000000 | 961664.000000 | 873552.000000 | 1.684259e+06 | 896040.000000 | 621000.000000 | 426529.000000 | 527143.000000 | 1.000000 |
8 rows × 24 columns
Split Data into Train and Validation¶
Now we want to see how well our classifier is performing, but we dont have the test data labels with us to check. What do we do ? So we split our dataset into train and validation. The idea is that we test our classifier on validation set in order to get an idea of how well our classifier works. This way we can also ensure that we dont overfit on the train dataset. There are many ways to do validation like k-fold,leave one out, etc
X = train_data.drop('default payment next month',1)
y = train_data['default payment next month']
# Validation testing
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
Here we have selected the size of the validation data to be 20% of the total data. You can change it and see what effect it has on the accuracies. To learn more about the train_test_split function click here.
Define the Classifier and Train¶
Now we come to the juicy part. We have fixed our data and now we train a classifier. The classifier will learn the function by looking at the inputs and corresponding outputs. There are a ton of classifiers to choose from some being Logistic Regression, SVM, Random Forests, Decision Trees, etc.
Tip: A good model doesnt depend solely on the classifier but on the features(columns) you choose. So make sure to play with your data and keep only whats important.
classifier = LogisticRegression()
classifier.fit(X_train,y_train)
# from sklearn.svm import SVC
# clf = SVC(gamma='auto')
# clf.fit(X_train, y_train)
# from sklearn import tree
# clf = tree.DecisionTreeClassifier()
# clf = clf.fit(X_train, y_train)
/home/gera/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning. FutureWarning)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, l1_ratio=None, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False)
We have used Logistic Regression as a classifier here and set few of the parameteres. But one can set more parameters and increase the performance. To see the list of parameters visit here.
Also given are SVM and Decision Tree examples. Check out SVM's parameters here and Decision Tree's here
got a warning! Dont worry, its just beacuse the number of iteration is very less(defined in the classifier in the above cell).Increase the number of iterations and see if the warning vanishes.Do remember increasing iterations also increases the running time.( Hint: max_iter=500)
Predict on Validation¶
Now we predict our trained classifier on the validation set and evaluate our model
y_pred = classifier.predict(X_val)
df = pd.DataFrame({'Actual': y_val, 'Predicted': y_pred})
df1 = df.head(25)
df1
Actual | Predicted | |
---|---|---|
6913 | 0 | 0 |
11124 | 0 | 0 |
25100 | 1 | 0 |
2764 | 0 | 0 |
23216 | 0 | 0 |
17269 | 0 | 0 |
3073 | 0 | 0 |
8184 | 0 | 0 |
2595 | 0 | 0 |
5483 | 0 | 0 |
6508 | 0 | 0 |
11776 | 0 | 0 |
5306 | 0 | 0 |
18846 | 0 | 0 |
19854 | 0 | 0 |
2463 | 0 | 0 |
5304 | 0 | 0 |
23739 | 0 | 0 |
20427 | 0 | 0 |
20263 | 0 | 0 |
9578 | 0 | 0 |
14164 | 0 | 0 |
5107 | 0 | 0 |
5160 | 0 | 0 |
8450 | 0 | 0 |
print('F1 score Score:', metrics.f1_score(y_val, y_pred))
print('ROC AUC Score:', metrics.roc_auc_score(y_val, y_pred))
F1 score Error: 0.0 ROC AUC Error: 0.49975062344139654
Load Test Set¶
Load the test data now
test_data = pd.read_csv('test.csv')
Predict Test Set¶
Time for the moment of truth! Predict on test set and time to make the submission.
y_test = classifier.predict(test_data)
df = pd.DataFrame(y_test,columns=['default payment next month'])
df.to_csv('submission.csv',index=False)
To download the generated csv in collab run the below command¶
from google.colab import files
files.download('submission.csv')
To participate in the challenge click here