4. Apply oversampling to balance an imbalanced dataset by increasing by number of minority class samples. Classify it using Decision Tree Classifier. (Dataset: creditcard.xls)¶
In [1]:
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
In [2]:
data = pd.read_csv("dataset/creditcard.xls")
data.head()
Out[2]:
| Time | V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | ... | V21 | V22 | V23 | V24 | V25 | V26 | V27 | V28 | Amount | target | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0.0 | -1.359807 | -0.072781 | 2.536347 | 1.378155 | -0.338321 | 0.462388 | 0.239599 | 0.098698 | 0.363787 | ... | -0.018307 | 0.277838 | -0.110474 | 0.066928 | 0.128539 | -0.189115 | 0.133558 | -0.021053 | 149.62 | 0 |
| 1 | 0.0 | 1.191857 | 0.266151 | 0.166480 | 0.448154 | 0.060018 | -0.082361 | -0.078803 | 0.085102 | -0.255425 | ... | -0.225775 | -0.638672 | 0.101288 | -0.339846 | 0.167170 | 0.125895 | -0.008983 | 0.014724 | 2.69 | 0 |
| 2 | 1.0 | -1.358354 | -1.340163 | 1.773209 | 0.379780 | -0.503198 | 1.800499 | 0.791461 | 0.247676 | -1.514654 | ... | 0.247998 | 0.771679 | 0.909412 | -0.689281 | -0.327642 | -0.139097 | -0.055353 | -0.059752 | 378.66 | 0 |
| 3 | 1.0 | -0.966272 | -0.185226 | 1.792993 | -0.863291 | -0.010309 | 1.247203 | 0.237609 | 0.377436 | -1.387024 | ... | -0.108300 | 0.005274 | -0.190321 | -1.175575 | 0.647376 | -0.221929 | 0.062723 | 0.061458 | 123.50 | 0 |
| 4 | 2.0 | -1.158233 | 0.877737 | 1.548718 | 0.403034 | -0.407193 | 0.095921 | 0.592941 | -0.270533 | 0.817739 | ... | -0.009431 | 0.798278 | -0.137458 | 0.141267 | -0.206010 | 0.502292 | 0.219422 | 0.215153 | 69.99 | 0 |
5 rows × 31 columns
In [3]:
x = data.drop(['target'], axis = 1)
y = data['target']
In [4]:
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)
In [5]:
classifier = DecisionTreeClassifier()
classifier.fit(x_train, y_train)
Out[5]:
DecisionTreeClassifier()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
| criterion | 'gini' | |
| splitter | 'best' | |
| max_depth | None | |
| min_samples_split | 2 | |
| min_samples_leaf | 1 | |
| min_weight_fraction_leaf | 0.0 | |
| max_features | None | |
| random_state | None | |
| max_leaf_nodes | None | |
| min_impurity_decrease | 0.0 | |
| class_weight | None | |
| ccp_alpha | 0.0 | |
| monotonic_cst | None |
In [6]:
pred = classifier.predict(x_test)
In [7]:
from sklearn.metrics import accuracy_score, classification_report
accuracy_score(y_test, pred)
Out[7]:
0.9991397773954567
In [8]:
targets = ['0', '1']
print(classification_report(y_test, pred, target_names = targets))
precision recall f1-score support
0 1.00 1.00 1.00 56865
1 0.77 0.71 0.74 97
accuracy 1.00 56962
macro avg 0.88 0.86 0.87 56962
weighted avg 1.00 1.00 1.00 56962
In [9]:
legit = data[data['target'] == 0]
fraud = data[data['target'] == 1]
In [10]:
legit.shape
Out[10]:
(284315, 31)
In [11]:
fraud.shape
Out[11]:
(492, 31)
In [12]:
fraud_sample = fraud.sample(n = 284315, replace = True)
fraud_sample.shape
Out[12]:
(284315, 31)
In [13]:
new_dataset = pd.concat([legit, fraud_sample], axis = 0)
new_dataset['target'].value_counts()
Out[13]:
target 0 284315 1 284315 Name: count, dtype: int64
In [14]:
x = new_dataset.drop(['target'], axis = 1)
y = new_dataset['target']
In [15]:
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)
classifier = DecisionTreeClassifier()
classifier.fit(x_train, y_train)
Out[15]:
DecisionTreeClassifier()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Parameters
| criterion | 'gini' | |
| splitter | 'best' | |
| max_depth | None | |
| min_samples_split | 2 | |
| min_samples_leaf | 1 | |
| min_weight_fraction_leaf | 0.0 | |
| max_features | None | |
| random_state | None | |
| max_leaf_nodes | None | |
| min_impurity_decrease | 0.0 | |
| class_weight | None | |
| ccp_alpha | 0.0 | |
| monotonic_cst | None |
In [16]:
y_pred = classifier.predict(x_test)
In [17]:
accuracy_score(y_test, y_pred)
Out[17]:
0.9997362080790673
In [18]:
targets = ['0', '1']
print(classification_report(y_test, y_pred, target_names = targets))
precision recall f1-score support
0 1.00 1.00 1.00 56715
1 1.00 1.00 1.00 57011
accuracy 1.00 113726
macro avg 1.00 1.00 1.00 113726
weighted avg 1.00 1.00 1.00 113726
In [ ]: