Skip to content

Commit 632a158

Browse files
committed
Ruff formatter applied
1 parent b0a60a3 commit 632a158

2 files changed

Lines changed: 114 additions & 100 deletions

File tree

notebooks/demo_linearboost_usage.ipynb

Lines changed: 113 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"outputs": [],
88
"source": [
99
"import sys\n",
10-
"sys.path.append('../src')\n",
10+
"\n",
11+
"sys.path.append(\"../src\")\n",
1112
"from linearboost.linear_boost import LinearBoostClassifier"
1213
]
1314
},
@@ -55,15 +56,15 @@
5556
"metadata": {},
5657
"outputs": [],
5758
"source": [
58-
"from ucimlrepo import fetch_ucirepo \n",
59+
"from ucimlrepo import fetch_ucirepo\n",
5960
"from sklearn.preprocessing import LabelEncoder\n",
6061
"\n",
6162
"# The Huberman's Survival's id on UCI Machine Learning Repository\n",
6263
"dataset_id = 43\n",
6364
"\n",
64-
"dataset = fetch_ucirepo(id=dataset_id) \n",
65-
" \n",
66-
"# data (as pandas dataframes) \n",
65+
"dataset = fetch_ucirepo(id=dataset_id)\n",
66+
"\n",
67+
"# data (as pandas dataframes)\n",
6768
"X = dataset.data.features.copy()\n",
6869
"y = dataset.data.targets\n",
6970
"\n",
@@ -78,25 +79,24 @@
7879
"metadata": {},
7980
"outputs": [],
8081
"source": [
81-
"\n",
8282
"import pandas as pd\n",
8383
"\n",
8484
"# Identify categorical columns\n",
85-
"categorical_cols = X.select_dtypes(include=['object']).columns.tolist()\n",
85+
"categorical_cols = X.select_dtypes(include=[\"object\"]).columns.tolist()\n",
8686
"\n",
8787
"# Convert categorical columns to 'category' dtype\n",
8888
"for col in categorical_cols:\n",
89-
" X[col] = X[col].astype('category')\n",
89+
" X[col] = X[col].astype(\"category\")\n",
9090
"\n",
9191
"# Handle missing values\n",
9292
"# Fill numeric columns with median\n",
93-
"numeric_cols = X.select_dtypes(include=['int64', 'float64']).columns.tolist()\n",
93+
"numeric_cols = X.select_dtypes(include=[\"int64\", \"float64\"]).columns.tolist()\n",
9494
"for col in numeric_cols:\n",
9595
" X[col] = X[col].fillna(X[col].median())\n",
9696
"\n",
9797
"# Fill categorical columns with mode\n",
9898
"for col in categorical_cols:\n",
99-
" X[col] = X[col].fillna(X[col].mode()[0])\n"
99+
" X[col] = X[col].fillna(X[col].mode()[0])"
100100
]
101101
},
102102
{
@@ -355,13 +355,15 @@
355355
"import numpy as np\n",
356356
"from sklearn.model_selection import StratifiedKFold, cross_val_score\n",
357357
"\n",
358+
"\n",
358359
"def custom_loss(y_true, y_pred, weights):\n",
359360
" return np.mean(weights * (y_true - y_pred) ** 2)\n",
360361
"\n",
362+
"\n",
361363
"df = X\n",
362364
"\n",
363365
"# One-hot encoding\n",
364-
"cat_features = list(df.select_dtypes(include=['object', 'category']).columns)\n",
366+
"cat_features = list(df.select_dtypes(include=[\"object\", \"category\"]).columns)\n",
365367
"for col in cat_features:\n",
366368
" df_onehot = pd.get_dummies(df[col], prefix=col)\n",
367369
" df = df.drop(col, axis=1)\n",
@@ -370,49 +372,52 @@
370372
"\n",
371373
"def objective(trial):\n",
372374
" params = {\n",
373-
" 'n_estimators': trial.suggest_int('n_estimators', 10, 500),\n",
374-
" 'learning_rate': trial.suggest_float('learning_rate', 0.01, 1.0, log=True),\n",
375-
" 'algorithm': trial.suggest_categorical('algorithm', ['SAMME', 'SAMME.R']),\n",
376-
" 'scaler': trial.suggest_categorical('scaler', [\n",
377-
" 'minmax', 'robust', 'quantile-uniform', 'quantile-normal'\n",
378-
" ]),\n",
379-
" 'kernel': trial.suggest_categorical('kernel', ['linear', 'rbf', 'poly', 'sigmoid']),\n",
375+
" \"n_estimators\": trial.suggest_int(\"n_estimators\", 10, 500),\n",
376+
" \"learning_rate\": trial.suggest_float(\"learning_rate\", 0.01, 1.0, log=True),\n",
377+
" \"algorithm\": trial.suggest_categorical(\"algorithm\", [\"SAMME\", \"SAMME.R\"]),\n",
378+
" \"scaler\": trial.suggest_categorical(\n",
379+
" \"scaler\", [\"minmax\", \"robust\", \"quantile-uniform\", \"quantile-normal\"]\n",
380+
" ),\n",
381+
" \"kernel\": trial.suggest_categorical(\n",
382+
" \"kernel\", [\"linear\", \"rbf\", \"poly\", \"sigmoid\"]\n",
383+
" ),\n",
380384
" }\n",
381385
"\n",
382-
" if params['kernel'] != 'linear':\n",
383-
" params['gamma'] = trial.suggest_float('gamma', 1e-3, 10.0, log=True)\n",
384-
" if params['kernel'] == 'poly':\n",
385-
" params['degree'] = trial.suggest_int('degree', 2, 5)\n",
386-
" if params['kernel'] in ['poly', 'sigmoid']:\n",
387-
" params['coef0'] = trial.suggest_float('coef0', 0.0, 1.0)\n",
388-
" \n",
386+
" if params[\"kernel\"] != \"linear\":\n",
387+
" params[\"gamma\"] = trial.suggest_float(\"gamma\", 1e-3, 10.0, log=True)\n",
388+
" if params[\"kernel\"] == \"poly\":\n",
389+
" params[\"degree\"] = trial.suggest_int(\"degree\", 2, 5)\n",
390+
" if params[\"kernel\"] in [\"poly\", \"sigmoid\"]:\n",
391+
" params[\"coef0\"] = trial.suggest_float(\"coef0\", 0.0, 1.0)\n",
392+
"\n",
389393
" # Using a custom loss function here\n",
390-
" #params['loss_function'] = custom_loss\n",
394+
" # params['loss_function'] = custom_loss\n",
391395
"\n",
392396
" model = LinearBoostClassifier(**params)\n",
393397
"\n",
394398
" scores = cross_val_score(\n",
395399
" estimator=model,\n",
396400
" X=df,\n",
397401
" y=y,\n",
398-
" scoring='f1_weighted',\n",
402+
" scoring=\"f1_weighted\",\n",
399403
" cv=StratifiedKFold(n_splits=10, shuffle=True, random_state=42),\n",
400404
" )\n",
401405
"\n",
402406
" return scores.mean()\n",
403407
"\n",
408+
"\n",
404409
"# Create an Optuna study and optimize the objective function\n",
405-
"study = optuna.create_study(direction='maximize')\n",
410+
"study = optuna.create_study(direction=\"maximize\")\n",
406411
"study.optimize(objective, n_trials=200)\n",
407412
"\n",
408413
"# Display the best trial's results\n",
409-
"print('Best trial:')\n",
414+
"print(\"Best trial:\")\n",
410415
"trial = study.best_trial\n",
411416
"\n",
412-
"print(f'F1 Score: {trial.value}')\n",
413-
"print('Parameters: ')\n",
417+
"print(f\"F1 Score: {trial.value}\")\n",
418+
"print(\"Parameters: \")\n",
414419
"for key, value in trial.params.items():\n",
415-
" print(f'{key}: {value}')"
420+
" print(f\"{key}: {value}\")"
416421
]
417422
},
418423
{
@@ -654,25 +659,25 @@
654659
}
655660
],
656661
"source": [
657-
"\n",
658662
"import xgboost as xgb\n",
659663
"\n",
664+
"\n",
660665
"def objective(trial):\n",
661666
" params = {\n",
662-
" 'objective': 'binary:logistic',\n",
663-
" 'use_label_encoder': False,\n",
664-
" 'n_estimators': trial.suggest_int('n_estimators', 20, 1000),\n",
665-
" 'max_depth': trial.suggest_int('max_depth', 1, 20),\n",
666-
" 'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.7),\n",
667-
" 'gamma': trial.suggest_float('gamma', 1e-8, 1.0, log=True),\n",
668-
" 'min_child_weight': trial.suggest_int('min_child_weight', 1, 10),\n",
669-
" 'subsample': trial.suggest_float('subsample', 0.5, 1.0),\n",
670-
" 'colsample_bytree': trial.suggest_float('colsample_bytree', 0.5, 1.0),\n",
671-
" 'reg_alpha': trial.suggest_float('reg_alpha', 1e-8, 1.0, log=True),\n",
672-
" 'reg_lambda': trial.suggest_float('reg_lambda', 1e-8, 1.0, log=True),\n",
673-
" 'enable_categorical': True,\n",
674-
" 'eval_metric': 'logloss',\n",
675-
" 'verbosity': 0\n",
667+
" \"objective\": \"binary:logistic\",\n",
668+
" \"use_label_encoder\": False,\n",
669+
" \"n_estimators\": trial.suggest_int(\"n_estimators\", 20, 1000),\n",
670+
" \"max_depth\": trial.suggest_int(\"max_depth\", 1, 20),\n",
671+
" \"learning_rate\": trial.suggest_float(\"learning_rate\", 0.01, 0.7),\n",
672+
" \"gamma\": trial.suggest_float(\"gamma\", 1e-8, 1.0, log=True),\n",
673+
" \"min_child_weight\": trial.suggest_int(\"min_child_weight\", 1, 10),\n",
674+
" \"subsample\": trial.suggest_float(\"subsample\", 0.5, 1.0),\n",
675+
" \"colsample_bytree\": trial.suggest_float(\"colsample_bytree\", 0.5, 1.0),\n",
676+
" \"reg_alpha\": trial.suggest_float(\"reg_alpha\", 1e-8, 1.0, log=True),\n",
677+
" \"reg_lambda\": trial.suggest_float(\"reg_lambda\", 1e-8, 1.0, log=True),\n",
678+
" \"enable_categorical\": True,\n",
679+
" \"eval_metric\": \"logloss\",\n",
680+
" \"verbosity\": 0,\n",
676681
" }\n",
677682
"\n",
678683
" model = xgb.XGBClassifier(**params)\n",
@@ -681,24 +686,24 @@
681686
" estimator=model,\n",
682687
" X=X,\n",
683688
" y=y,\n",
684-
" scoring='f1_weighted',\n",
689+
" scoring=\"f1_weighted\",\n",
685690
" cv=StratifiedKFold(n_splits=10, shuffle=True, random_state=42),\n",
686-
" n_jobs=-1\n",
691+
" n_jobs=-1,\n",
687692
" )\n",
688693
"\n",
689694
" return scores.mean()\n",
690695
"\n",
691696
"\n",
692-
"study = optuna.create_study(direction='maximize')\n",
697+
"study = optuna.create_study(direction=\"maximize\")\n",
693698
"study.optimize(objective, n_trials=200)\n",
694699
"\n",
695700
"best_trial = study.best_trial\n",
696701
"\n",
697-
"print('Best trial:')\n",
698-
"print(f'F1 Score: {best_trial.value:.6f}')\n",
699-
"print('Parameters:')\n",
702+
"print(\"Best trial:\")\n",
703+
"print(f\"F1 Score: {best_trial.value:.6f}\")\n",
704+
"print(\"Parameters:\")\n",
700705
"for k, v in best_trial.params.items():\n",
701-
" print(f'{k}: {v}')\n"
706+
" print(f\"{k}: {v}\")"
702707
]
703708
},
704709
{
@@ -946,24 +951,27 @@
946951
"source": [
947952
"import lightgbm as lgb\n",
948953
"\n",
954+
"\n",
949955
"def objective(trial):\n",
950956
" params = {\n",
951-
" 'objective': 'binary',\n",
952-
" 'metric': 'binary_logloss',\n",
953-
" 'boosting_type': trial.suggest_categorical('boosting_type', ['gbdt', 'dart', 'goss']),\n",
954-
" 'num_leaves': trial.suggest_int('num_leaves', 2, 256),\n",
955-
" 'learning_rate': trial.suggest_float('learning_rate', 1e-3, 0.1, log=True),\n",
956-
" 'n_estimators': trial.suggest_int('n_estimators', 20, 1000),\n",
957-
" 'max_depth': trial.suggest_int('max_depth', 1, 20),\n",
958-
" 'min_child_samples': trial.suggest_int('min_child_samples', 1, 100),\n",
959-
" 'subsample': trial.suggest_float('subsample', 0.5, 1.0),\n",
960-
" 'colsample_bytree': trial.suggest_float('colsample_bytree', 0.5, 1.0),\n",
961-
" 'reg_alpha': trial.suggest_float('reg_alpha', 1e-8, 10.0, log=True),\n",
962-
" 'reg_lambda': trial.suggest_float('reg_lambda', 1e-8, 10.0, log=True),\n",
963-
" 'min_split_gain': trial.suggest_float('min_split_gain', 1e-8, 1.0, log=True),\n",
964-
" 'cat_smooth': trial.suggest_int('cat_smooth', 1, 100),\n",
965-
" 'cat_l2': trial.suggest_float('cat_l2', 1e-8, 10.0, log=True),\n",
966-
" 'verbosity': -1\n",
957+
" \"objective\": \"binary\",\n",
958+
" \"metric\": \"binary_logloss\",\n",
959+
" \"boosting_type\": trial.suggest_categorical(\n",
960+
" \"boosting_type\", [\"gbdt\", \"dart\", \"goss\"]\n",
961+
" ),\n",
962+
" \"num_leaves\": trial.suggest_int(\"num_leaves\", 2, 256),\n",
963+
" \"learning_rate\": trial.suggest_float(\"learning_rate\", 1e-3, 0.1, log=True),\n",
964+
" \"n_estimators\": trial.suggest_int(\"n_estimators\", 20, 1000),\n",
965+
" \"max_depth\": trial.suggest_int(\"max_depth\", 1, 20),\n",
966+
" \"min_child_samples\": trial.suggest_int(\"min_child_samples\", 1, 100),\n",
967+
" \"subsample\": trial.suggest_float(\"subsample\", 0.5, 1.0),\n",
968+
" \"colsample_bytree\": trial.suggest_float(\"colsample_bytree\", 0.5, 1.0),\n",
969+
" \"reg_alpha\": trial.suggest_float(\"reg_alpha\", 1e-8, 10.0, log=True),\n",
970+
" \"reg_lambda\": trial.suggest_float(\"reg_lambda\", 1e-8, 10.0, log=True),\n",
971+
" \"min_split_gain\": trial.suggest_float(\"min_split_gain\", 1e-8, 1.0, log=True),\n",
972+
" \"cat_smooth\": trial.suggest_int(\"cat_smooth\", 1, 100),\n",
973+
" \"cat_l2\": trial.suggest_float(\"cat_l2\", 1e-8, 10.0, log=True),\n",
974+
" \"verbosity\": -1,\n",
967975
" }\n",
968976
"\n",
969977
" model = lgb.LGBMClassifier(**params)\n",
@@ -972,24 +980,24 @@
972980
" estimator=model,\n",
973981
" X=X,\n",
974982
" y=y,\n",
975-
" scoring='f1_weighted',\n",
983+
" scoring=\"f1_weighted\",\n",
976984
" cv=StratifiedKFold(n_splits=10, shuffle=True, random_state=42),\n",
977-
" n_jobs=-1\n",
985+
" n_jobs=-1,\n",
978986
" )\n",
979987
"\n",
980988
" return scores.mean()\n",
981989
"\n",
982-
"study = optuna.create_study(direction='maximize')\n",
990+
"\n",
991+
"study = optuna.create_study(direction=\"maximize\")\n",
983992
"study.optimize(objective, n_trials=200)\n",
984993
"\n",
985994
"best_trial = study.best_trial\n",
986995
"\n",
987-
"print('Best trial:')\n",
988-
"print(f'F1 Score: {best_trial.value:.6f}')\n",
989-
"print('Parameters:')\n",
996+
"print(\"Best trial:\")\n",
997+
"print(f\"F1 Score: {best_trial.value:.6f}\")\n",
998+
"print(\"Parameters:\")\n",
990999
"for k, v in best_trial.params.items():\n",
991-
" print(f'{k}: {v}')\n",
992-
"\n"
1000+
" print(f\"{k}: {v}\")"
9931001
]
9941002
},
9951003
{
@@ -1234,22 +1242,27 @@
12341242
"source": [
12351243
"from catboost import CatBoostClassifier\n",
12361244
"\n",
1245+
"\n",
12371246
"def objective(trial):\n",
12381247
" params = {\n",
1239-
" 'iterations': trial.suggest_int('iterations', 50, 500),\n",
1240-
" 'depth': trial.suggest_int('depth', 1, 16),\n",
1241-
" 'learning_rate': trial.suggest_float('learning_rate', 1e-3, 0.5, log=True),\n",
1242-
" 'l2_leaf_reg': trial.suggest_float('l2_leaf_reg', 1e-8, 10.0, log=True),\n",
1243-
" 'random_strength': trial.suggest_float('random_strength', 1e-8, 10.0, log=True),\n",
1244-
" 'bagging_temperature': trial.suggest_float('bagging_temperature', 0.1, 10.0, log=True),\n",
1245-
" 'border_count': trial.suggest_int('border_count', 32, 255),\n",
1246-
" 'grow_policy': trial.suggest_categorical('grow_policy', ['SymmetricTree', 'Depthwise', 'Lossguide']),\n",
1247-
" 'min_data_in_leaf': trial.suggest_int('min_data_in_leaf', 1, 100),\n",
1248-
" 'rsm': trial.suggest_float('rsm', 0.1, 1.0),\n",
1249-
" 'loss_function': 'Logloss',\n",
1250-
" 'eval_metric': 'F1',\n",
1251-
" 'cat_features': categorical_cols,\n",
1252-
" 'verbose': 0\n",
1248+
" \"iterations\": trial.suggest_int(\"iterations\", 50, 500),\n",
1249+
" \"depth\": trial.suggest_int(\"depth\", 1, 16),\n",
1250+
" \"learning_rate\": trial.suggest_float(\"learning_rate\", 1e-3, 0.5, log=True),\n",
1251+
" \"l2_leaf_reg\": trial.suggest_float(\"l2_leaf_reg\", 1e-8, 10.0, log=True),\n",
1252+
" \"random_strength\": trial.suggest_float(\"random_strength\", 1e-8, 10.0, log=True),\n",
1253+
" \"bagging_temperature\": trial.suggest_float(\n",
1254+
" \"bagging_temperature\", 0.1, 10.0, log=True\n",
1255+
" ),\n",
1256+
" \"border_count\": trial.suggest_int(\"border_count\", 32, 255),\n",
1257+
" \"grow_policy\": trial.suggest_categorical(\n",
1258+
" \"grow_policy\", [\"SymmetricTree\", \"Depthwise\", \"Lossguide\"]\n",
1259+
" ),\n",
1260+
" \"min_data_in_leaf\": trial.suggest_int(\"min_data_in_leaf\", 1, 100),\n",
1261+
" \"rsm\": trial.suggest_float(\"rsm\", 0.1, 1.0),\n",
1262+
" \"loss_function\": \"Logloss\",\n",
1263+
" \"eval_metric\": \"F1\",\n",
1264+
" \"cat_features\": categorical_cols,\n",
1265+
" \"verbose\": 0,\n",
12531266
" }\n",
12541267
"\n",
12551268
" model = CatBoostClassifier(**params)\n",
@@ -1258,23 +1271,24 @@
12581271
" estimator=model,\n",
12591272
" X=X,\n",
12601273
" y=y,\n",
1261-
" scoring='f1_weighted',\n",
1274+
" scoring=\"f1_weighted\",\n",
12621275
" cv=StratifiedKFold(n_splits=10, shuffle=True, random_state=42),\n",
1263-
" n_jobs=-1\n",
1276+
" n_jobs=-1,\n",
12641277
" )\n",
12651278
"\n",
12661279
" return scores.mean()\n",
12671280
"\n",
1268-
"study = optuna.create_study(direction='maximize')\n",
1281+
"\n",
1282+
"study = optuna.create_study(direction=\"maximize\")\n",
12691283
"study.optimize(objective, n_trials=200)\n",
12701284
"\n",
12711285
"best_trial = study.best_trial\n",
12721286
"\n",
1273-
"print('Best trial:')\n",
1274-
"print(f'F1 Score: {best_trial.value:.6f}')\n",
1275-
"print('Parameters:')\n",
1287+
"print(\"Best trial:\")\n",
1288+
"print(f\"F1 Score: {best_trial.value:.6f}\")\n",
1289+
"print(\"Parameters:\")\n",
12761290
"for k, v in best_trial.params.items():\n",
1277-
" print(f'{k}: {v}')"
1291+
" print(f\"{k}: {v}\")"
12781292
]
12791293
}
12801294
],

src/linearboost/linear_boost.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def fit(self, X, y, sample_weight=None) -> Self:
564564
warnings.filterwarnings(
565565
"ignore",
566566
category=FutureWarning,
567-
message=".*parameter 'algorithm' is deprecated.*"
567+
message=".*parameter 'algorithm' is deprecated.*",
568568
)
569569
return super().fit(X_transformed, y, sample_weight)
570570

0 commit comments

Comments
 (0)