Importation et préparation des données¶
In [1]:
# changement de dossier
import os
os.chdir("C:/Users/ricco/Desktop/demo")
# chargement des données
import pandas
df = pandas.read_excel("segmentation.xlsx")
df.info()
<class 'pandas.DataFrame'> RangeIndex: 2310 entries, 0 to 2309 Data columns (total 20 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 region_centroid_col 2310 non-null int64 1 region_centroid_row 2310 non-null int64 2 region_pixel_count 2310 non-null int64 3 short_line_density_5 2310 non-null float64 4 short_line_density_2 2310 non-null float64 5 vedge_mean 2310 non-null float64 6 vegde_sd 2310 non-null float64 7 hedge_mean 2310 non-null float64 8 hedge_sd 2310 non-null float64 9 intensity_mean 2310 non-null float64 10 rawred_mean 2310 non-null float64 11 rawblue_mean 2310 non-null float64 12 rawgreen_mean 2310 non-null float64 13 exred_mean 2310 non-null float64 14 exblue_mean 2310 non-null float64 15 exgreen_mean 2310 non-null float64 16 value_mean 2310 non-null float64 17 saturation_mean 2310 non-null float64 18 hue_mean 2310 non-null float64 19 classe 2310 non-null str dtypes: float64(16), int64(3), str(1) memory usage: 361.1 KB
In [2]:
# partition train-test
from sklearn.model_selection import train_test_split
dfTrain, dfTest = train_test_split(df,train_size=310,stratify=df.classe,random_state=0)
# distributions TRAIN
dfTrain.classe.value_counts(normalize=True).sort_index()
Out[2]:
classe brickface 0.141935 cement 0.141935 foliage 0.145161 grass 0.141935 path 0.141935 sky 0.141935 window 0.145161 Name: proportion, dtype: float64
In [3]:
# test
dfTest.classe.value_counts(normalize=True).sort_index()
Out[3]:
classe brickface 0.1430 cement 0.1430 foliage 0.1425 grass 0.1430 path 0.1430 sky 0.1430 window 0.1425 Name: proportion, dtype: float64
In [4]:
# préparation des structures -- train
XTrain = dfTrain.drop(columns=['classe'])
yTrain = dfTrain.classe
print(XTrain.shape, yTrain.shape)
# test
XTest = dfTest[XTrain.columns]
yTest = dfTest.classe
print(XTest.shape, yTest.shape)
(310, 19) (310,) (2000, 19) (2000,)
Modèle Elasticnet (pipeline)¶
In [5]:
# outils
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
# Pipeline : standardisation + régression logistique ElasticNet
pipeline = Pipeline([
("scaler", StandardScaler()),
("logreg", LogisticRegression(
solver="saga", # obligatoire pour elasticnet
max_iter=5000,
C=1.0, # inverse de la régularisation
l1_ratio=0.5 # mix entre L1 (lasso) et L2 (ridge)
))
])
In [6]:
# entraînement
pipeline.fit(XTrain,yTrain)
# accuracy en test
pipeline.score(XTest,yTest)
Out[6]:
0.9035
Optimisation des hyper-paramètres avec optuna¶
In [7]:
# outils
from sklearn.model_selection import cross_val_score
# définir une fonction objectif
def objective(trial):
# paramètres à optimiser
C_param = trial.suggest_float("logreg__C", 0.01, 10)
l1_ratio_param = trial.suggest_float("logreg__l1_ratio", 0, 1)
# Pipeline
pipeline = Pipeline([
("scaler", StandardScaler()),
("logreg", LogisticRegression(
solver="saga",
max_iter=5000,
C=C_param,
l1_ratio=l1_ratio_param
))
])
score = cross_val_score(
pipeline, XTrain, yTrain,
cv=5,
scoring="accuracy"
).mean()
return score
In [8]:
# outil
import optuna
# définir l'étude incluant les sorties dans une base "sqlite"
study = optuna.create_study(
direction="maximize",
storage="sqlite:///study.db", #base de stockage des résultats
study_name="logreg_elasticnet",
load_if_exists=True #ajoute les résultats le cas échéant
)
c:\Users\ricco\anaconda3\envs\env_optuna\Lib\site-packages\tqdm\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
[I 2026-04-04 11:24:50,870] A new study created in RDB with name: logreg_elasticnet
In [9]:
# lancer l'analyse -- multiprocessing 4 coeurs
study.optimize(objective, n_jobs = 4, n_trials=100)
print("Best params:", study.best_params)
[I 2026-04-04 11:27:02,904] Trial 1 finished with value: 0.9419354838709676 and parameters: {'logreg__C': 3.294743974596236, 'logreg__l1_ratio': 0.8317563266778742}. Best is trial 1 with value: 0.9419354838709676. [I 2026-04-04 11:27:03,076] Trial 0 finished with value: 0.9419354838709676 and parameters: {'logreg__C': 6.176517457331378, 'logreg__l1_ratio': 0.1560340338849524}. Best is trial 0 with value: 0.9419354838709676. [I 2026-04-04 11:27:03,555] Trial 3 finished with value: 0.9451612903225806 and parameters: {'logreg__C': 3.75773342167917, 'logreg__l1_ratio': 0.9211351332854507}. Best is trial 3 with value: 0.9451612903225806. [I 2026-04-04 11:27:03,778] Trial 2 finished with value: 0.9419354838709678 and parameters: {'logreg__C': 9.442746125201971, 'logreg__l1_ratio': 0.355612082486924}. Best is trial 3 with value: 0.9451612903225806. [I 2026-04-04 11:27:04,172] Trial 5 finished with value: 0.9258064516129032 and parameters: {'logreg__C': 0.6755539113970934, 'logreg__l1_ratio': 0.3264888563575179}. Best is trial 3 with value: 0.9451612903225806. [I 2026-04-04 11:27:04,341] Trial 4 finished with value: 0.932258064516129 and parameters: {'logreg__C': 2.657130052973282, 'logreg__l1_ratio': 0.29943124032357604}. Best is trial 3 with value: 0.9451612903225806. [I 2026-04-04 11:27:05,727] Trial 8 finished with value: 0.9419354838709676 and parameters: {'logreg__C': 1.832155550090175, 'logreg__l1_ratio': 0.6449664710942654}. Best is trial 3 with value: 0.9451612903225806. [I 2026-04-04 11:27:05,820] Trial 7 finished with value: 0.935483870967742 and parameters: {'logreg__C': 4.583296716877382, 'logreg__l1_ratio': 0.2713261298658729}. Best is trial 3 with value: 0.9451612903225806. [I 2026-04-04 11:27:06,164] Trial 6 finished with value: 0.9419354838709676 and parameters: {'logreg__C': 5.667813092818203, 'logreg__l1_ratio': 0.6273103734834174}. Best is trial 3 with value: 0.9451612903225806. [I 2026-04-04 11:27:07,792] Trial 12 finished with value: 0.935483870967742 and parameters: {'logreg__C': 3.120869510287998, 'logreg__l1_ratio': 0.4394279424573261}. Best is trial 3 with value: 0.9451612903225806. [I 2026-04-04 11:27:07,902] Trial 11 finished with value: 0.935483870967742 and parameters: {'logreg__C': 5.669168486824013, 'logreg__l1_ratio': 0.11555873060329958}. Best is trial 3 with value: 0.9451612903225806. [I 2026-04-04 11:27:08,053] Trial 9 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 9.638178120881966, 'logreg__l1_ratio': 0.7973931504535479}. Best is trial 9 with value: 0.9451612903225808. [I 2026-04-04 11:27:08,153] Trial 10 finished with value: 0.9387096774193548 and parameters: {'logreg__C': 6.583153115355411, 'logreg__l1_ratio': 0.2120796285688512}. Best is trial 9 with value: 0.9451612903225808. [I 2026-04-04 11:27:11,610] Trial 13 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.023551809067264, 'logreg__l1_ratio': 0.9644938113569576}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:11,759] Trial 14 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.430910648043712, 'logreg__l1_ratio': 0.9011877863886888}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:12,149] Trial 15 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.862914703679227, 'logreg__l1_ratio': 0.9797613548575895}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:12,306] Trial 16 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.834343355452003, 'logreg__l1_ratio': 0.9708034393677638}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:15,209] Trial 19 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 7.973935880355139, 'logreg__l1_ratio': 0.691281599590776}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:15,322] Trial 20 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 7.674167311997016, 'logreg__l1_ratio': 0.6851693812914561}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:15,579] Trial 18 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 7.923728154081552, 'logreg__l1_ratio': 0.9936902785117581}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:15,650] Trial 17 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.998314608103493, 'logreg__l1_ratio': 0.9792899993353309}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:18,565] Trial 21 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 7.870955489808572, 'logreg__l1_ratio': 0.8214877173609484}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:18,819] Trial 22 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 8.341299366457097, 'logreg__l1_ratio': 0.8391105726973593}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:19,138] Trial 23 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 8.621729093639505, 'logreg__l1_ratio': 0.8226020243266887}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:19,219] Trial 24 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 8.703882184908744, 'logreg__l1_ratio': 0.8299524873387393}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:22,348] Trial 28 finished with value: 0.9419354838709678 and parameters: {'logreg__C': 7.171426666065676, 'logreg__l1_ratio': 0.5439166814049295}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:22,643] Trial 25 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.76591928361927, 'logreg__l1_ratio': 0.8632172536258833}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:23,124] Trial 26 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.818062312154598, 'logreg__l1_ratio': 0.8893834841437013}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:23,724] Trial 27 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.918868619473614, 'logreg__l1_ratio': 0.9082543876154592}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:27,030] Trial 31 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 7.120736435118658, 'logreg__l1_ratio': 0.7303852328948607}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:27,269] Trial 30 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 9.204408351555228, 'logreg__l1_ratio': 0.7435997334352418}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:27,412] Trial 29 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.062150412182348, 'logreg__l1_ratio': 0.9198726348940147}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:27,736] Trial 32 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 6.925356084349861, 'logreg__l1_ratio': 0.7544169689296454}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:31,369] Trial 33 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 9.30129844295437, 'logreg__l1_ratio': 0.7479511417629194}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:32,184] Trial 34 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.968101463132555, 'logreg__l1_ratio': 0.95867221100355}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:32,492] Trial 35 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.945044097648653, 'logreg__l1_ratio': 0.9848821827420253}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:32,899] Trial 36 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.821902635993785, 'logreg__l1_ratio': 0.9974939833531038}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:35,734] Trial 39 finished with value: 0.9387096774193548 and parameters: {'logreg__C': 9.34226545175114, 'logreg__l1_ratio': 0.040084503963998985}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:36,265] Trial 37 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.596094370149327, 'logreg__l1_ratio': 0.9717251878165858}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:37,448] Trial 38 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.789190212973185, 'logreg__l1_ratio': 0.9985161121931931}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:37,554] Trial 40 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.268327065239212, 'logreg__l1_ratio': 0.9253522911554548}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:39,027] Trial 42 finished with value: 0.9419354838709676 and parameters: {'logreg__C': 4.892660901735859, 'logreg__l1_ratio': 0.5645726594472567}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:39,105] Trial 41 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 4.36454861077472, 'logreg__l1_ratio': 0.901754909180743}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:40,576] Trial 46 finished with value: 0.9161290322580644 and parameters: {'logreg__C': 0.17002735342633457, 'logreg__l1_ratio': 0.9399750129040976}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:42,009] Trial 43 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.333852029197592, 'logreg__l1_ratio': 0.9220849898812349}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:42,109] Trial 44 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.215317330433876, 'logreg__l1_ratio': 0.8925033215925989}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:43,589] Trial 45 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.293733214121566, 'logreg__l1_ratio': 0.9371572154509141}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:44,864] Trial 47 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.355456022737032, 'logreg__l1_ratio': 0.8802803184840443}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:45,634] Trial 48 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 5.898925229532427, 'logreg__l1_ratio': 0.8704752254349112}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:46,117] Trial 49 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 7.378577396150307, 'logreg__l1_ratio': 0.7909800630663663}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:47,402] Trial 50 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 7.480827644579986, 'logreg__l1_ratio': 0.7853589994207735}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:47,720] Trial 53 finished with value: 0.9387096774193548 and parameters: {'logreg__C': 2.3062799577915407, 'logreg__l1_ratio': 0.3941014908599857}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:48,615] Trial 51 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 6.083268755187893, 'logreg__l1_ratio': 0.8617060483186418}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:48,927] Trial 52 finished with value: 0.9387096774193548 and parameters: {'logreg__C': 7.538289010947691, 'logreg__l1_ratio': 0.4218703451190152}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:51,737] Trial 54 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 6.289198045733217, 'logreg__l1_ratio': 0.9547123626845195}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:52,719] Trial 55 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.539246425056241, 'logreg__l1_ratio': 0.967277314026503}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:53,509] Trial 57 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 6.7319253308903075, 'logreg__l1_ratio': 0.9674189599992691}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:53,803] Trial 56 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.555075951812583, 'logreg__l1_ratio': 0.9599654148918146}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:58,721] Trial 58 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.353323371468049, 'logreg__l1_ratio': 0.9594964175451121}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:27:59,979] Trial 59 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 7.9771235808293195, 'logreg__l1_ratio': 0.9979463973498045}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:00,479] Trial 61 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 8.922254407333707, 'logreg__l1_ratio': 0.8443935121412768}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:00,677] Trial 60 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 7.908838692761017, 'logreg__l1_ratio': 0.9995336091655008}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:02,414] Trial 62 finished with value: 0.9387096774193548 and parameters: {'logreg__C': 7.890009531006662, 'logreg__l1_ratio': 0.2636933693905895}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:04,390] Trial 63 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.937038730451308, 'logreg__l1_ratio': 0.8524250916173607}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:04,795] Trial 64 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 8.645053774455429, 'logreg__l1_ratio': 0.8632672989457694}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:05,021] Trial 65 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.59721216859495, 'logreg__l1_ratio': 0.8786255244376098}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:06,675] Trial 66 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 8.710435731376528, 'logreg__l1_ratio': 0.8514749223676181}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:09,042] Trial 67 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.995968016443738, 'logreg__l1_ratio': 0.9006699504089507}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:09,442] Trial 68 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.946681086678671, 'logreg__l1_ratio': 0.9027239605135298}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:09,678] Trial 69 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 9.970167827356443, 'logreg__l1_ratio': 0.9110425462765414}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:11,320] Trial 70 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.723954721341196, 'logreg__l1_ratio': 0.9174209300617608}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:13,502] Trial 72 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 9.028977240291798, 'logreg__l1_ratio': 0.7011675057827955}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:13,694] Trial 71 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.1965745184687, 'logreg__l1_ratio': 0.9293805598536138}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:13,984] Trial 73 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 9.189496324035051, 'logreg__l1_ratio': 0.810560422905263}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:15,602] Trial 74 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 9.186436681514975, 'logreg__l1_ratio': 0.8090702726382345}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:17,776] Trial 75 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 9.164291572033353, 'logreg__l1_ratio': 0.8134672757991209}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:17,870] Trial 76 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 8.864378863294641, 'logreg__l1_ratio': 0.8136645253114743}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:18,561] Trial 77 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.513827865409127, 'logreg__l1_ratio': 0.9440880547814284}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:19,874] Trial 78 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 9.570992096991525, 'logreg__l1_ratio': 0.7740067660102017}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:22,538] Trial 79 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.488344700361578, 'logreg__l1_ratio': 0.9421582022034851}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:22,740] Trial 80 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.593252669594222, 'logreg__l1_ratio': 0.9450386980291926}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:23,253] Trial 81 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.594590494468056, 'logreg__l1_ratio': 0.9727276335202539}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:24,981] Trial 82 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.60027844908095, 'logreg__l1_ratio': 0.9781651796679385}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:26,482] Trial 84 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 8.450243232239284, 'logreg__l1_ratio': 0.6146524415362227}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:27,458] Trial 83 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.504163688591957, 'logreg__l1_ratio': 0.9817493902201155}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:27,888] Trial 85 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.118276940225917, 'logreg__l1_ratio': 0.9777732428756161}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:28,758] Trial 87 finished with value: 0.9387096774193548 and parameters: {'logreg__C': 3.620994490489225, 'logreg__l1_ratio': 0.4920538273514875}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:29,561] Trial 86 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.14147754609301, 'logreg__l1_ratio': 0.8937594531485576}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:30,762] Trial 88 finished with value: 0.9419354838709676 and parameters: {'logreg__C': 3.618511071061117, 'logreg__l1_ratio': 0.9143785262065377}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:31,462] Trial 91 finished with value: 0.9258064516129032 and parameters: {'logreg__C': 1.2189857062839615, 'logreg__l1_ratio': 0.8852749014377717}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:31,603] Trial 89 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 5.39915914945496, 'logreg__l1_ratio': 0.8882645173297788}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:32,945] Trial 90 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 7.609583443913509, 'logreg__l1_ratio': 0.8884058720602227}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:34,941] Trial 92 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 7.698486250717437, 'logreg__l1_ratio': 0.8812366765002352}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:35,991] Trial 94 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 7.677545672769694, 'logreg__l1_ratio': 0.929770576395906}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:36,184] Trial 93 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.75115279304295, 'logreg__l1_ratio': 0.9309937557531202}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:37,684] Trial 95 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.792965939714563, 'logreg__l1_ratio': 0.9357427737277653}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:39,784] Trial 96 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 8.85309974053444, 'logreg__l1_ratio': 0.9280095616171794}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:40,474] Trial 97 finished with value: 0.9451612903225808 and parameters: {'logreg__C': 9.02092053005271, 'logreg__l1_ratio': 0.8340887215472937}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:40,615] Trial 98 finished with value: 0.9483870967741936 and parameters: {'logreg__C': 9.040247298694434, 'logreg__l1_ratio': 0.8396205896472523}. Best is trial 13 with value: 0.9483870967741936. [I 2026-04-04 11:28:40,838] Trial 99 finished with value: 0.9387096774193548 and parameters: {'logreg__C': 8.94208480613651, 'logreg__l1_ratio': 0.14168209969846735}. Best is trial 13 with value: 0.9483870967741936.
Best params: {'logreg__C': 8.023551809067264, 'logreg__l1_ratio': 0.9644938113569576}
In [10]:
# récupération des meilleurs paramètres
best_params = study.best_params
print(best_params)
{'logreg__C': 8.023551809067264, 'logreg__l1_ratio': 0.9644938113569576}
Instanciation du meilleur modèle¶
In [11]:
# outil de clonage
from sklearn.base import clone
# dupliquer le modèle
best_pipe = clone(pipeline)
# introduire les nouveaux paramètres
best_pipe.set_params(**best_params)
# vérification
best_pipe.get_params()
Out[11]:
{'memory': None,
'steps': [('scaler', StandardScaler()),
('logreg',
LogisticRegression(C=8.023551809067264, l1_ratio=0.9644938113569576,
max_iter=5000, solver='saga'))],
'transform_input': None,
'verbose': False,
'scaler': StandardScaler(),
'logreg': LogisticRegression(C=8.023551809067264, l1_ratio=0.9644938113569576,
max_iter=5000, solver='saga'),
'scaler__copy': True,
'scaler__with_mean': True,
'scaler__with_std': True,
'logreg__C': 8.023551809067264,
'logreg__class_weight': None,
'logreg__dual': False,
'logreg__fit_intercept': True,
'logreg__intercept_scaling': 1,
'logreg__l1_ratio': 0.9644938113569576,
'logreg__max_iter': 5000,
'logreg__n_jobs': None,
'logreg__penalty': 'deprecated',
'logreg__random_state': None,
'logreg__solver': 'saga',
'logreg__tol': 0.0001,
'logreg__verbose': 0,
'logreg__warm_start': False}
In [12]:
# entraînement
best_pipe.fit(XTrain,yTrain)
# évaluation
best_pipe.score(XTest,yTest)
Out[12]:
0.9175