%22%22%22How%20to%20Tune%20Pipeline%20Parameters.%0A%0ATune%20hyperparameters%20across%20multiple%20pipeline%20steps%20with%20OptunaSearchCV.%0A%22%22%22%0A%0A%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22optuna%22%2C%0A%23%20%20%20%20%20%22scikit-learn%22%2C%0A%23%20%20%20%20%20%22sklearn-optuna%22%2C%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.19.9%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20from%20optuna.distributions%20import%20FloatDistribution%2C%20IntDistribution%0A%20%20%20%20from%20sklearn.datasets%20import%20make_classification%0A%20%20%20%20from%20sklearn.decomposition%20import%20PCA%0A%20%20%20%20from%20sklearn.linear_model%20import%20LogisticRegression%0A%20%20%20%20from%20sklearn.pipeline%20import%20Pipeline%0A%20%20%20%20from%20sklearn.preprocessing%20import%20StandardScaler%0A%0A%20%20%20%20from%20sklearn_optuna%20import%20OptunaSearchCV%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20FloatDistribution%2C%0A%20%20%20%20%20%20%20%20IntDistribution%2C%0A%20%20%20%20%20%20%20%20LogisticRegression%2C%0A%20%20%20%20%20%20%20%20OptunaSearchCV%2C%0A%20%20%20%20%20%20%20%20PCA%2C%0A%20%20%20%20%20%20%20%20Pipeline%2C%0A%20%20%20%20%20%20%20%20StandardScaler%2C%0A%20%20%20%20%20%20%20%20make_classification%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20How%20to%20Tune%20Pipeline%20Parameters%0A%0A%20%20%20%20This%20notebook%20shows%20how%20to%20tune%20hyperparameters%20of%20estimators%0A%20%20%20%20inside%20a%20scikit-learn%20%60Pipeline%60%20using%20%60OptunaSearchCV%60.%0A%0A%20%20%20%20**Prerequisites%3A**%20Familiarity%20with%20the%0A%20%20%20%20OptunaSearchCV%20quickstart%0A%20%20%20%20(%5BView%5D(%2Fexamples%2Fquickstart%2F)%20%C2%B7%20%5BOpen%20in%20marimo%5D(%2Fexamples%2Fquickstart%2Fedit%2F))%0A%20%20%20%20and%20sklearn%20pipelines.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(make_classification)%3A%0A%20%20%20%20X%2C%20y%20%3D%20make_classification(%0A%20%20%20%20%20%20%20%20n_samples%3D200%2C%0A%20%20%20%20%20%20%20%20n_features%3D10%2C%0A%20%20%20%20%20%20%20%20n_informative%3D5%2C%0A%20%20%20%20%20%20%20%20n_redundant%3D0%2C%0A%20%20%20%20%20%20%20%20random_state%3D42%2C%0A%20%20%20%20)%0A%20%20%20%20return%20X%2C%20y%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%201.%20Tune%20Sub-Estimator%20Parameters%0A%0A%20%20%20%20Use%20%60__%60%20(double%20underscore)%20syntax%20to%20address%20parameters%20of%0A%20%20%20%20estimators%20nested%20inside%20a%20pipeline.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20FloatDistribution%2C%0A%20%20%20%20LogisticRegression%2C%0A%20%20%20%20OptunaSearchCV%2C%0A%20%20%20%20Pipeline%2C%0A%20%20%20%20StandardScaler%2C%0A%20%20%20%20X%2C%0A%20%20%20%20y%2C%0A)%3A%0A%20%20%20%20pipe%20%3D%20Pipeline(%5B%0A%20%20%20%20%20%20%20%20(%22scaler%22%2C%20StandardScaler())%2C%0A%20%20%20%20%20%20%20%20(%22clf%22%2C%20LogisticRegression(max_iter%3D200))%2C%0A%20%20%20%20%5D)%0A%0A%20%20%20%20search%20%3D%20OptunaSearchCV(%0A%20%20%20%20%20%20%20%20pipe%2C%0A%20%20%20%20%20%20%20%20param_distributions%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22clf__C%22%3A%20FloatDistribution(1e-2%2C%2010.0%2C%20log%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20n_trials%3D10%2C%0A%20%20%20%20%20%20%20%20cv%3D3%2C%0A%20%20%20%20)%0A%20%20%20%20search.fit(X%2C%20y)%0A%20%20%20%20return%20(search%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20search)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Best%20C%3A**%20%60%7Bsearch.best_params_%5B'clf__C'%5D%3A.4f%7D%60%0A%20%20%20%20**Best%20Score%3A**%20%60%7Bsearch.best_score_%3A.3f%7D%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%202.%20Tune%20Across%20Multiple%20Steps%0A%0A%20%20%20%20Address%20parameters%20from%20different%20pipeline%20steps%20in%20the%20same%0A%20%20%20%20search.%20Here%2C%20tune%20PCA's%20%60n_components%60%20alongside%20the%0A%20%20%20%20classifier's%20%60C%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20FloatDistribution%2C%0A%20%20%20%20IntDistribution%2C%0A%20%20%20%20LogisticRegression%2C%0A%20%20%20%20OptunaSearchCV%2C%0A%20%20%20%20PCA%2C%0A%20%20%20%20Pipeline%2C%0A%20%20%20%20X%2C%0A%20%20%20%20y%2C%0A)%3A%0A%20%20%20%20multi_pipe%20%3D%20Pipeline(%5B%0A%20%20%20%20%20%20%20%20(%22pca%22%2C%20PCA())%2C%0A%20%20%20%20%20%20%20%20(%22clf%22%2C%20LogisticRegression(max_iter%3D200))%2C%0A%20%20%20%20%5D)%0A%0A%20%20%20%20multi_search%20%3D%20OptunaSearchCV(%0A%20%20%20%20%20%20%20%20multi_pipe%2C%0A%20%20%20%20%20%20%20%20param_distributions%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22pca__n_components%22%3A%20IntDistribution(2%2C%208)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22clf__C%22%3A%20FloatDistribution(1e-2%2C%2010.0%2C%20log%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20n_trials%3D15%2C%0A%20%20%20%20%20%20%20%20cv%3D3%2C%0A%20%20%20%20)%0A%20%20%20%20multi_search.fit(X%2C%20y)%0A%20%20%20%20return%20(multi_search%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20multi_search)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Best%20PCA%20components%3A**%20%60%7Bmulti_search.best_params_%5B'pca__n_components'%5D%7D%60%0A%20%20%20%20**Best%20C%3A**%20%60%7Bmulti_search.best_params_%5B'clf__C'%5D%3A.4f%7D%60%0A%20%20%20%20**Best%20Score%3A**%20%60%7Bmulti_search.best_score_%3A.3f%7D%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
ea69f0bd4963fb20f17e9aa7b5e03ed5