%22%22%22How%20to%20Handle%20Failing%20Trials.%0A%0AControl%20what%20happens%20when%20a%20hyperparameter%20combination%20causes%20fitting%20to%20fail.%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%20math%0A%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%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%20make_classification%2C%0A%20%20%20%20%20%20%20%20math%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%20Handle%20Failing%20Trials%0A%0A%20%20%20%20This%20notebook%20shows%20how%20to%20control%20what%20happens%20when%20some%0A%20%20%20%20hyperparameter%20combinations%20fail%20during%20fitting.%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(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%201.%20Default%20Behavior%3A%20NaN%20for%20Failures%0A%0A%20%20%20%20By%20default%2C%20%60error_score%3Dnp.nan%60%20records%20%60NaN%60%20for%20failing%0A%20%20%20%20trials%20and%20continues%20the%20search.%20Here%2C%20PCA%20%60n_components%60%0A%20%20%20%20values%20above%205%20exceed%20the%20feature%20count%20and%20fail.%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%20make_classification%2C%0A)%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%0A%20%20%20%20pipe%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%20%23%20n_components%20%3E%205%20will%20fail%20(dataset%20has%205%20features)%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%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(0.1%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%20search.fit(X%2C%20y)%0A%20%20%20%20return%20(search%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(math%2C%20mo%2C%20search)%3A%0A%20%20%20%20_scores%20%3D%20search.cv_results_%5B%22mean_test_score%22%5D%0A%20%20%20%20_n_failed%20%3D%20sum(1%20for%20s%20in%20_scores%20if%20math.isnan(s))%0A%20%20%20%20_n_total%20%3D%20len(_scores)%0A%20%20%20%20_best%20%3D%20search.best_score_%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Total%20trials%3A**%20%7B_n_total%7D%0A%20%20%20%20**Failed%20trials%20(NaN)%3A**%20%7B_n_failed%7D%0A%20%20%20%20**Successful%20trials%3A**%20%7B_n_total%20-%20_n_failed%7D%0A%20%20%20%20**Best%20score%3A**%20%60%7B_best%3A.3f%7D%60%0A%0A%20%20%20%20The%20search%20completed%20despite%20failures.%20Failed%20trials%20appear%20as%0A%20%20%20%20%60NaN%60%20in%20%60cv_results_%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.%20Raise%20on%20Failure%0A%0A%20%20%20%20Set%20%60error_score%3D%22raise%22%60%20to%20stop%20the%20search%20immediately%20when%0A%20%20%20%20a%20trial%20fails.%20This%20is%20useful%20during%20development%20to%20catch%0A%20%20%20%20unexpected%20parameter%20combinations.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20search%20%3D%20OptunaSearchCV(%0A%20%20%20%20%20%20%20%20pipe%2C%20param_distributions%2C%0A%20%20%20%20%20%20%20%20error_score%3D%22raise%22%2C%20%20%23%20stops%20on%20first%20failure%0A%20%20%20%20)%0A%20%20%20%20%60%60%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.%20Filter%20Failed%20Trials%0A%0A%20%20%20%20Inspect%20which%20trials%20failed%20and%20which%20parameter%20combinations%0A%20%20%20%20caused%20the%20failures.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(math%2C%20mo%2C%20search)%3A%0A%20%20%20%20_scores%20%3D%20search.cv_results_%5B%22mean_test_score%22%5D%0A%20%20%20%20_params%20%3D%20search.cv_results_%5B%22params%22%5D%0A%20%20%20%20_failed%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%7B%22params%22%3A%20p%2C%20%22mean_test_score%22%3A%20s%7D%0A%20%20%20%20%20%20%20%20for%20p%2C%20s%20in%20zip(_params%2C%20_scores)%0A%20%20%20%20%20%20%20%20if%20math.isnan(s)%0A%20%20%20%20%5D%0A%20%20%20%20mo.ui.table(_failed)%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
0d5254346a35ce45fb0a7d1675f0c106