Can you add StandardScaler to it and somehow connect it to the model, so models works on…
- User
Can you add StandardScaler to it and somehow connect it to the model, so models works on raw data and scales it? Something like pipeline, but adjusted to it.
import xgboost as xgb import optuna from sklearn.model_selection import cross_validate from sklearn.metrics import roc_auc_score
def objective(trial): max_depth = trial.suggest_int('max_depth', 1, 20) learning_rate = trial.suggest_float('learning_rate', 0, 0.5) subsample = trial.suggest_float('subsample', 0, 1) gamma = trial.suggest_float("gamma", 1e-4, 1e2) reg_alpha = trial.suggest_float('reg_alpha', 0, 1) reg_lambda = trial.suggest_float('reg_lambda', 0, 1) min_split_loss = trial.suggest_float('min_split_loss', 0, 8) nround=trial.suggest_int('nround',10,300) params = { 'max_depth': max_depth, 'learning_rate': learning_rate, 'subsample': subsample, 'gamma': gamma, 'reg_alpha': reg_alpha, 'reg_lambda': reg_lambda, 'eval_metric': 'auc', 'min_split_loss':min_split_loss, 'objective': 'binary:logitraw', }
dtrain = xgb.DMatrix(X, Y, enable_categorical=True, missing=True) dtest = xgb.DMatrix(X_test, Y_test, enable_categorical=True, missing=True) model = xgb.train(params, dtrain, num_boost_round=nround, evals=[(dtest, 'eval'), (dtrain, 'train')], verbose_eval=False) score, is_overfitted = validate(model, X, Y, X_test, Y_test) if is_overfitted: return 0 else: return scoredef validate(model, X, Y, X_test, Y_test): dtrain = xgb.DMatrix(X, Y, enable_categorical=True, missing=True) dtest = xgb.DMatrix(X_test, Y_test, enable_categorical=True, missing=True)
train_preds = model.predict(dtrain) test_preds = model.predict(dtest) train_score = roc_auc_score(Y, train_preds) test_score = roc_auc_score(Y_test, test_preds) is_overfitted = train_score - test_score > 0.05 return test_score, is_overfittedstudy = optuna.create_study(direction='maximize') study.optimize(objective, n_trials=1500)
?
- Assistant
To add StandardScaler to the code and connect it to the model, you can create a custom class that combines preprocessing and the XGBoost model, which will enable the model to work on raw data and scale it. Here's an example modification to accommodate this change:
import xgboost as xgb import optuna import numpy as np from sklearn.model_selection import cross_validate from sklearn.metrics import roc_auc_score from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler class XGBoostWithPreprocessing(xgb.XGBClassifier): def __init__( self, learning_rate=0.1, max_depth=3, subsample=0.5, gamma=0, reg_alpha=0, reg_lambda=1, min_split_loss=0, nround=10, **kwargs ): self.scaler = None super().__init__( max_depth=max_depth, learning_rate=learning_rate, subsample=subsample, gamma=gamma, reg_alpha=reg_alpha, reg_lambda=reg_lambda, min_split_loss=min_split_loss, nround=nround, **kwargs ) def fit(self, X, y, **kwargs): self.scaler = StandardScaler() X_scaled = self.scaler.fit_transform(X) return super().fit(X_scaled, y, **kwargs) def predict(self, X, **kwargs): if self.scaler is None: raise RuntimeError("fit needs to be called before predict") X_scaled = self.scaler.transform(X) return super().predict(X_scaled, **kwargs) def objective(trial): # ... params.update({"random_state": 0, "n_jobs": -1, "silent": True}) model = XGBoostWithPreprocessing(**params) model.fit(X, Y) score, is_overfitted = validate(model, X, Y, X_test, Y_test) # ...
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.