%22%22%22How%20to%20Choose%20and%20Configure%20a%20Sampler.%0A%0AControl%20the%20optimization%20algorithm%20and%20get%20reproducible%20results%20with%20Optuna%20samplers.%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.svm%20import%20SVC%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%20OptunaSearchCV%2C%0A%20%20%20%20%20%20%20%20SVC%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%20How%20to%20Choose%20and%20Configure%20a%20Sampler%0A%0A%20%20%20%20This%20notebook%20shows%20how%20to%20choose%20an%20Optuna%20sampler%20and%20configure%0A%20%20%20%20it%20for%20reproducible%20hyperparameter%20searches.%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%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%20n_features%3D5%2C%20n_informative%3D3%2C%20random_state%3D0%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.%20Configure%20TPESampler%20with%20a%20Seed%0A%0A%20%20%20%20Wrap%20%60TPESampler%60%20with%20the%20%60Sampler%60%20wrapper%20and%20pass%20%60seed%3D%60%0A%20%20%20%20for%20deterministic%20optimization.%20Results%20are%20reproducible%20when%0A%20%20%20%20%60n_jobs%3D1%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(FloatDistribution%2C%20OptunaSearchCV%2C%20SVC%2C%20Sampler%2C%20X%2C%20optuna%2C%20y)%3A%0A%20%20%20%20tpe_search%20%3D%20OptunaSearchCV(%0A%20%20%20%20%20%20%20%20SVC()%2C%0A%20%20%20%20%20%20%20%20%7B%22C%22%3A%20FloatDistribution(0.1%2C%2010.0%2C%20log%3DTrue)%7D%2C%0A%20%20%20%20%20%20%20%20sampler%3DSampler(sampler%3Doptuna.samplers.TPESampler%2C%20seed%3D42)%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%20%20%20%20n_jobs%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20tpe_search.fit(X%2C%20y)%0A%20%20%20%20return%20(tpe_search%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20tpe_search)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Sampler%3A**%20TPESampler%0A%20%20%20%20**Best%20C%3A**%20%60%7Btpe_search.best_params_%5B'C'%5D%3A.4f%7D%60%0A%20%20%20%20**Best%20score%3A**%20%60%7Btpe_search.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.%20Switch%20to%20RandomSampler%0A%0A%20%20%20%20Swap%20the%20sampler%20class%20to%20%60RandomSampler%60%20for%20uniform%20coverage%0A%20%20%20%20of%20the%20search%20space.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(FloatDistribution%2C%20OptunaSearchCV%2C%20SVC%2C%20Sampler%2C%20X%2C%20optuna%2C%20y)%3A%0A%20%20%20%20random_search%20%3D%20OptunaSearchCV(%0A%20%20%20%20%20%20%20%20SVC()%2C%0A%20%20%20%20%20%20%20%20%7B%22C%22%3A%20FloatDistribution(0.1%2C%2010.0%2C%20log%3DTrue)%7D%2C%0A%20%20%20%20%20%20%20%20sampler%3DSampler(sampler%3Doptuna.samplers.RandomSampler%2C%20seed%3D42)%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%20%20%20%20n_jobs%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20random_search.fit(X%2C%20y)%0A%20%20%20%20return%20(random_search%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20random_search)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Sampler%3A**%20RandomSampler%0A%20%20%20%20**Best%20C%3A**%20%60%7Brandom_search.best_params_%5B'C'%5D%3A.4f%7D%60%0A%20%20%20%20**Best%20score%3A**%20%60%7Brandom_search.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%203.%20Verify%20Reproducibility%0A%0A%20%20%20%20Run%20the%20same%20search%20again%20with%20the%20same%20seed.%20The%20results%0A%20%20%20%20should%20match%20exactly.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(FloatDistribution%2C%20OptunaSearchCV%2C%20SVC%2C%20Sampler%2C%20X%2C%20optuna%2C%20y)%3A%0A%20%20%20%20repeat_search%20%3D%20OptunaSearchCV(%0A%20%20%20%20%20%20%20%20SVC()%2C%0A%20%20%20%20%20%20%20%20%7B%22C%22%3A%20FloatDistribution(0.1%2C%2010.0%2C%20log%3DTrue)%7D%2C%0A%20%20%20%20%20%20%20%20sampler%3DSampler(sampler%3Doptuna.samplers.TPESampler%2C%20seed%3D42)%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%20%20%20%20n_jobs%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20repeat_search.fit(X%2C%20y)%0A%20%20%20%20return%20(repeat_search%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20repeat_search%2C%20tpe_search)%3A%0A%20%20%20%20_match%20%3D%20tpe_search.best_params_%5B%22C%22%5D%20%3D%3D%20repeat_search.best_params_%5B%22C%22%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Original%20best%20C%3A**%20%60%7Btpe_search.best_params_%5B'C'%5D%3A.4f%7D%60%0A%20%20%20%20**Repeated%20best%20C%3A**%20%60%7Brepeat_search.best_params_%5B'C'%5D%3A.4f%7D%60%0A%20%20%20%20**Results%20match%3A**%20%60%7B_match%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
e370d6a5a934f4fb0f22831a30777fbe