Update app.py
Browse files
app.py
CHANGED
|
@@ -190,62 +190,6 @@ def train_model(_):
|
|
| 190 |
wandb_run = wandb.init(project="huggingface-data-analysis", name=f"Optuna_Run_{run_counter}", reinit=True)
|
| 191 |
run_counter += 1
|
| 192 |
|
| 193 |
-
import optuna
|
| 194 |
-
from sklearn.model_selection import train_test_split, cross_val_score
|
| 195 |
-
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
|
| 196 |
-
from sklearn.linear_model import LogisticRegression
|
| 197 |
-
import pandas as pd
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
def prepare_data():
|
| 201 |
-
global df_global
|
| 202 |
-
if df_global is None:
|
| 203 |
-
raise ValueError("No dataset uploaded.")
|
| 204 |
-
|
| 205 |
-
target = df_global.columns[-1]
|
| 206 |
-
X = df_global.iloc[:, :-1]
|
| 207 |
-
y = df_global[target]
|
| 208 |
-
|
| 209 |
-
return train_test_split(X, y, test_size=0.2, random_state=42)
|
| 210 |
-
|
| 211 |
-
def make_objective(X_train, y_train):
|
| 212 |
-
def objective(trial):
|
| 213 |
-
model_type = trial.suggest_categorical("model_type", ["RandomForest", "GradientBoosting", "LogisticRegression"])
|
| 214 |
-
|
| 215 |
-
if model_type == "RandomForest":
|
| 216 |
-
model = RandomForestClassifier(
|
| 217 |
-
n_estimators=trial.suggest_int("n_estimators", 50, 300),
|
| 218 |
-
max_depth=trial.suggest_int("max_depth", 2, 32)
|
| 219 |
-
)
|
| 220 |
-
elif model_type == "GradientBoosting":
|
| 221 |
-
model = GradientBoostingClassifier(
|
| 222 |
-
n_estimators=trial.suggest_int("n_estimators", 50, 300),
|
| 223 |
-
learning_rate=trial.suggest_float("learning_rate", 0.01, 0.3),
|
| 224 |
-
max_depth=trial.suggest_int("max_depth", 2, 32)
|
| 225 |
-
)
|
| 226 |
-
else:
|
| 227 |
-
model = LogisticRegression(
|
| 228 |
-
C=trial.suggest_float("C", 1e-3, 1e2),
|
| 229 |
-
solver="liblinear"
|
| 230 |
-
)
|
| 231 |
-
|
| 232 |
-
score = cross_val_score(model, X_train, y_train, cv=3).mean()
|
| 233 |
-
return score
|
| 234 |
-
|
| 235 |
-
return objective
|
| 236 |
-
|
| 237 |
-
# ✅ Call the functions in order
|
| 238 |
-
X_train, X_test, y_train, y_test = prepare_data()
|
| 239 |
-
objective = make_objective(X_train, y_train) # 👈 wrap with your train data
|
| 240 |
-
|
| 241 |
-
# ✅ Now run optimization
|
| 242 |
-
study = optuna.create_study(direction="maximize")
|
| 243 |
-
study.optimize(objective, n_trials=15)
|
| 244 |
-
|
| 245 |
-
# ✅ Print the best params
|
| 246 |
-
print("Best trial:")
|
| 247 |
-
print(study.best_trial)
|
| 248 |
-
|
| 249 |
|
| 250 |
|
| 251 |
def objective(trial):
|
|
|
|
| 190 |
wandb_run = wandb.init(project="huggingface-data-analysis", name=f"Optuna_Run_{run_counter}", reinit=True)
|
| 191 |
run_counter += 1
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
|
| 195 |
def objective(trial):
|