%22%22%22OptunaSearchCV%20Quickstart.%0A%0ARun%20a%20short%20Optuna-powered%20hyperparameter%20search%20and%20interpret%20the%20best%0Aparameters%20and%20score.%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%20import%20optuna%0A%20%20%20%20from%20optuna.distributions%20import%20FloatDistribution%0A%20%20%20%20from%20sklearn.datasets%20import%20make_classification%0A%20%20%20%20from%20sklearn.linear_model%20import%20LogisticRegression%0A%0A%20%20%20%20from%20sklearn_optuna%20import%20OptunaSearchCV%2C%20Sampler%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%20LogisticRegression%2C%0A%20%20%20%20%20%20%20%20OptunaSearchCV%2C%0A%20%20%20%20%20%20%20%20Sampler%2C%0A%20%20%20%20%20%20%20%20make_classification%2C%0A%20%20%20%20%20%20%20%20optuna%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%20OptunaSearchCV%20Quickstart%0A%0A%20%20%20%20In%20this%20notebook%2C%20we%20will%20run%20a%20hyperparameter%20search%20using%0A%20%20%20%20%60OptunaSearchCV%60%20and%20inspect%20the%20best%20parameters%20and%20score.%0A%20%20%20%20Along%20the%20way%2C%20we%20will%20define%20a%20search%20space%20with%20Optuna%0A%20%20%20%20distributions%20and%20see%20how%20the%20results%20API%20works.%0A%0A%20%20%20%20**Prerequisites%3A**%20Basic%20familiarity%20with%20scikit-learn's%20fit%2Fpredict%20API.%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%201.%20Prepare%20Data%20and%20Estimator%0A%0A%20%20%20%20We%20start%20by%20creating%20a%20classification%20dataset%20and%20initializing%20a%0A%20%20%20%20LogisticRegression%20estimator.%20%60OptunaSearchCV%60%20will%20tune%20its%0A%20%20%20%20hyperparameters%20automatically.%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%3D6%2C%0A%20%20%20%20%20%20%20%20n_informative%3D3%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%0Adef%20_(LogisticRegression)%3A%0A%20%20%20%20estimator%20%3D%20LogisticRegression(max_iter%3D200)%0A%20%20%20%20return%20(estimator%2C)%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.%20Define%20a%20Search%20Space%0A%0A%20%20%20%20Now%20we%20define%20the%20hyperparameter%20search%20space.%20We%20use%0A%20%20%20%20%60FloatDistribution%60%20with%20log-scale%20for%20the%20regularization%0A%20%20%20%20parameter%20%60C%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(FloatDistribution)%3A%0A%20%20%20%20param_distributions%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22C%22%3A%20FloatDistribution(1e-2%2C%2010.0%2C%20log%3DTrue)%2C%0A%20%20%20%20%7D%0A%20%20%20%20return%20(param_distributions%2C)%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%203.%20Run%20OptunaSearchCV%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(OptunaSearchCV%2C%20Sampler%2C%20X%2C%20estimator%2C%20optuna%2C%20param_distributions%2C%20y)%3A%0A%20%20%20%20search%20%3D%20OptunaSearchCV(%0A%20%20%20%20%20%20%20%20estimator%2C%0A%20%20%20%20%20%20%20%20param_distributions%2C%0A%20%20%20%20%20%20%20%20n_trials%3D10%2C%0A%20%20%20%20%20%20%20%20sampler%3DSampler(sampler%3Doptuna.samplers.TPESampler%2C%20seed%3D0)%2C%0A%20%20%20%20%20%20%20%20cv%3D3%2C%0A%20%20%20%20%20%20%20%20n_jobs%3D1%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%20params%3A**%20%7Bsearch.best_params_%7D%0A%20%20%20%20**Best%20score%3A**%20%7Bsearch.best_score_%3A.3f%7D%0A%0A%20%20%20%20Notice%20that%20%60best_params_%60%20and%20%60best_score_%60%20work%20exactly%20like%0A%20%20%20%20scikit-learn's%20%60GridSearchCV%60%20--%20the%20same%20attributes%2C%20the%20same%20format.%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%20What%20We%20Built%0A%0A%20%20%20%20We%20ran%20a%20hyperparameter%20search%20with%20%60OptunaSearchCV%60%20and%20found%0A%20%20%20%20the%20best%20regularization%20parameter%20for%20a%20LogisticRegression.%0A%20%20%20%20Along%20the%20way%2C%20we%3A%0A%0A%20%20%20%20-%20Defined%20a%20search%20space%20with%20%60FloatDistribution%60%0A%20%20%20%20-%20Ran%20the%20search%20with%20%60OptunaSearchCV.fit()%60%0A%20%20%20%20-%20Inspected%20%60best_params_%60%20and%20%60best_score_%60%0A%0A%20%20%20%20**Next%20steps%3A**%0A%0A%20%20%20%20-%20How%20to%20resume%20optimization%20from%20prior%20trials%3A%0A%20%20%20%20%20%20%5BView%5D(%2Fexamples%2Fstudy_management%2F)%20%C2%B7%20%5BOpen%20in%20marimo%5D(%2Fexamples%2Fstudy_management%2Fedit%2F)%0A%20%20%20%20-%20How%20to%20stop%20optimization%20early%20with%20callbacks%3A%0A%20%20%20%20%20%20%5BView%5D(%2Fexamples%2Fcallbacks%2F)%20%C2%B7%20%5BOpen%20in%20marimo%5D(%2Fexamples%2Fcallbacks%2Fedit%2F)%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
67e0a496ffb8eb17750717d0e7c61682