Code
Code
import pandas as pd
import numpy as np
import optuna
X = data.drop('cardio', axis=1)
y = data['cardio']
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
#mice_imputer = IterativeImputer(random_state=0)
#smote = BorderlineSMOTE(random_state=0)
#X_train_resampled, y_train_resampled =
smote.fit_resample(X_train_imputed, y_train) # SMOTE on SCALED and
IMPUTED data
def objective(trial):
params = {
'objective': 'binary',
model = lgb.train(
params,
dtrain,
valid_sets=[dtrain],
callbacks=[lgb.early_stopping(stopping_rounds=50, verbose=False)],
# Pass verbose=False to early_stopping
)
y_pred = model.predict(X_test_imputed) #Predict against the SCALED
test data.
study = optuna.create_study(direction='maximize')
best_params = study.best_params
final_model = lgb.LGBMClassifier(**best_params)
final_model.fit(X_train_resampled, y_train_resampled)
# 10. Predictions and Evaluation (Use AUC and a more granular probability
threshold.)