In [1]:
!pip install imbalanced-learn
Requirement already satisfied: imbalanced-learn in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (0.14.0) Requirement already satisfied: numpy<3,>=1.25.2 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from imbalanced-learn) (2.3.2) Requirement already satisfied: scipy<2,>=1.11.4 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from imbalanced-learn) (1.16.1) Requirement already satisfied: scikit-learn<2,>=1.4.2 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from imbalanced-learn) (1.7.1) Requirement already satisfied: joblib<2,>=1.2.0 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from imbalanced-learn) (1.5.1) Requirement already satisfied: threadpoolctl<4,>=2.0.0 in /opt/anaconda3/envs/NNM24AM045/lib/python3.13/site-packages (from imbalanced-learn) (3.6.0)
In [2]:
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
from imblearn.over_sampling import SMOTE
In [3]:
dataset = pd.read_csv("dataset/creditcard.xls")
dataset.head()
Out[3]:
| 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 [4]:
x = dataset.drop(['target'], axis=1)
y = dataset['target']
In [5]:
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)
In [6]:
classifier = DecisionTreeClassifier()
classifier.fit(x_train, y_train)
Out[6]:
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 [7]:
y_pred = classifier.predict(x_test)
accuracy_score(y_test, y_pred)
Out[7]:
0.9992275552122467
In [8]:
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 56879
1 0.73 0.73 0.73 83
accuracy 1.00 56962
macro avg 0.87 0.87 0.87 56962
weighted avg 1.00 1.00 1.00 56962
In [9]:
from sklearn.metrics import confusion_matrix, precision_score, recall_score
cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cm)
tn, fp, fn, tp = cm.ravel()
print(f"True Negatives: {tn}")
print(f"False Negatives: {fp}")
print(f"False Positives: {fn}")
print(f"True Positives: {tp}")
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
Confusion Matrix: [[56857 22] [ 22 61]] True Negatives: 56857 False Negatives: 22 False Positives: 22 True Positives: 61 Precision: 0.7349 Recall: 0.7349