-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreditCard.py
More file actions
36 lines (28 loc) · 956 Bytes
/
Copy pathcreditCard.py
File metadata and controls
36 lines (28 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score
from xgboost import XGBClassifier
import matplotlib.pyplot as plt
from xgboost import plot_importance
# Load dataset
data = pd.read_csv('data.csv')
# Features and label
X = data.drop(columns=["ID", "default payment next month"])
y = data["default payment next month"]
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)
# Calculate class imbalance ratio
ratio = sum(y_train == 0) / sum(y_train == 1)
# Train XGBoost with imbalance handling
model = XGBClassifier(
scale_pos_weight=ratio, # boosts minority class
eval_metric='logloss',
random_state=42
)
model.fit(X_train, y_train)
# Predictions
y_pred = model.predict(X_test)
# Evaluation
print("Accuracy:", accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))