{"id":4005,"date":"2024-08-17T12:25:12","date_gmt":"2024-08-17T16:25:12","guid":{"rendered":"https:\/\/www.econai.tech\/?page_id=4005"},"modified":"2024-08-22T11:36:49","modified_gmt":"2024-08-22T15:36:49","slug":"cross-validation","status":"publish","type":"page","link":"https:\/\/tomomitanaka.ai\/?page_id=4005","title":{"rendered":"Cross-Validation"},"content":{"rendered":"\n<div class=\"wp-block-jin-gb-block-box-with-headline kaisetsu-box1\"><div class=\"kaisetsu-box1-title\"><strong>Safety by Design Expert\u2019s Note<\/strong><\/div>\n<p>As safety by design experts, understanding cross-validation is crucial for ensuring the reliability and robustness of machine learning models used in safety-critical applications. This post will help you:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Evaluate model performance more accurately, reducing the risk of deploying unreliable models<\/li>\n\n\n\n<li>Identify potential biases or inconsistencies in model predictions across different data subsets<\/li>\n\n\n\n<li>Implement rigorous testing methodologies for AI systems in safety-critical environments<\/li>\n\n\n\n<li>Make more informed decisions about model selection and hyperparameter tuning<\/li>\n<\/ol>\n<\/div>\n\n\n\n<p>Cross-validation is a fundamental technique in machine learning that helps ensure the robustness and generalizability of your models. <\/p>\n\n\n\n<p>As we&#8217;ve explored various algorithms and methods in our journey through predictive modeling, understanding and effectively applying cross-validation is essential to avoid overfitting and to gauge the true performance of your models.<\/p>\n\n\n\n<p>In this post, we\u2019ll dive deep into cross-validation, exploring its significance, different techniques, and practical implementation. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Contents<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>What is Cross-Validation?<\/li>\n\n\n\n<li>Why Use Cross-Validation?<\/li>\n\n\n\n<li>Types of Cross-Validation<\/li>\n\n\n\n<li>Implementing Cross-Validation in Python<\/li>\n\n\n\n<li>Case Study: Cross-Validation in House Price Prediction<\/li>\n\n\n\n<li>Conclusion<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is Cross-Validation?<\/h3>\n\n\n\n<p>Cross-validation is a statistical method used to estimate the performance of machine learning models. <\/p>\n\n\n\n<p>Instead of splitting the data into just one training and one testing set, cross-validation repeatedly splits the data into different subsets to ensure that the model&#8217;s performance is consistent across different data samples.<\/p>\n\n\n\n<p>This technique provides a more accurate measure of a model&#8217;s performance by evaluating it on various parts of the dataset, reducing the chances of overfitting, and ensuring that the model generalizes well to unseen data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Why Use Cross-Validation?<\/h3>\n\n\n\n<p>The main goal of cross-validation is to assess how well your model will perform on an independent dataset. <\/p>\n\n\n\n<p>When you split your data into just one training and one testing set, the model might perform well on the testing data simply because it\u2019s optimized for that particular split. However, this doesn\u2019t guarantee that the model will perform well on new, unseen data.<\/p>\n\n\n\n<p>Cross-validation mitigates this issue by repeatedly splitting the data into training and testing sets in different ways, ensuring that the model is evaluated on various samples. <\/p>\n\n\n\n<p>This process provides a more reliable estimate of the model\u2019s performance and helps in selecting the best model and hyperparameters.<\/p>\n\n\n\n<div class=\"wp-block-jin-gb-block-box-with-headline kaisetsu-box1\"><div class=\"kaisetsu-box1-title\">Key Benefits of Cross-Validation<\/div>\n<p><strong>Improved Model Evaluation<\/strong>: Provides a more accurate estimate of a model&#8217;s ability to generalize.<\/p>\n\n\n\n<p><strong>Reduction of Overfitting<\/strong>: By testing the model on multiple data subsets, cross-validation helps prevent overfitting.<\/p>\n\n\n\n<p><strong>Hyperparameter Tuning<\/strong>: Cross-validation is essential for selecting the optimal hyperparameters, ensuring the model performs well across different data splits.<\/p>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. Types of Cross-Validation<\/h3>\n\n\n\n<p>Several types of cross-validation techniques can be used depending on the size of the dataset, the model complexity, and the computational resources available. Here are the most commonly used methods:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">K-Fold Cross-Validation<\/h4>\n\n\n\n<p>In K-Fold Cross-Validation, the dataset is divided into K equally sized folds. The model is trained on K-1 folds and tested on the remaining fold. This process is repeated K times, with each fold being used as the test set once. The final performance metric is the average of the metrics from each fold. K-Fold is the most common cross-validation method due to its balance between bias and variance.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Stratified K-Fold Cross-Validation<\/h4>\n\n\n\n<p>Stratified K-Fold ensures that each fold has the same proportion of class labels as the original dataset, making it particularly useful for classification tasks with imbalanced datasets.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Leave-One-Out Cross-Validation (LOOCV)<\/h4>\n\n\n\n<p>In LOOCV, each data point is used as a single test instance while the remaining data forms the training set. This method is exhaustive and can provide a thorough evaluation but is computationally expensive, especially for large datasets.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Time Series Cross-Validation<\/h4>\n\n\n\n<p>For time series data, where the order of data points is crucial, traditional cross-validation methods can\u2019t be applied directly. Time Series Cross-Validation involves using past data to predict future data, maintaining the temporal order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Implementing Cross-Validation in Python<\/h3>\n\n\n\n<p>Let\u2019s implement cross-validation using scikit-learn to evaluate different models. We\u2019ll use K-Fold Cross-Validation for this example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"from sklearn.model_selection import cross_val_score, KFold\nfrom sklearn.linear_model import Ridge\nfrom sklearn.ensemble import RandomForestRegressor\n\n# Define models\nridge = Ridge(alpha=1.0)\nrf = RandomForestRegressor(n_estimators=100, random_state=42)\n\n# Define K-Fold Cross-Validation\nkf = KFold(n_splits=5, shuffle=True, random_state=42)\n\n# Evaluate Ridge Regression\nridge_scores = cross_val_score(ridge, X, y, cv=kf, scoring='neg_mean_squared_error')\nprint(f'Ridge MSE: {-ridge_scores.mean():.4f} (+\/- {ridge_scores.std() * 2:.4f})')\n\n# Evaluate Random Forest\nrf_scores = cross_val_score(rf, X, y, cv=kf, scoring='neg_mean_squared_error')\nprint(f'Random Forest MSE: {-rf_scores.mean():.4f} (+\/- {rf_scores.std() * 2:.4f})')\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> sklearn.model_selection <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> cross_val_score, KFold<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> sklearn.linear_model <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> Ridge<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> sklearn.ensemble <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> RandomForestRegressor<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Define models<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">ridge = Ridge(<\/span><span style=\"color: #9CDCFE\">alpha<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">1.0<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">rf = RandomForestRegressor(<\/span><span style=\"color: #9CDCFE\">n_estimators<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">random_state<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">42<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Define K-Fold Cross-Validation<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">kf = KFold(<\/span><span style=\"color: #9CDCFE\">n_splits<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">shuffle<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">random_state<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">42<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Evaluate Ridge Regression<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">ridge_scores = cross_val_score(ridge, X, y, <\/span><span style=\"color: #9CDCFE\">cv<\/span><span style=\"color: #D4D4D4\">=kf, <\/span><span style=\"color: #9CDCFE\">scoring<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&#39;neg_mean_squared_error&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&#39;Ridge MSE: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">-ridge_scores.mean()<\/span><span style=\"color: #569CD6\">:.4f}<\/span><span style=\"color: #CE9178\"> (+\/- <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">ridge_scores.std() * <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #569CD6\">:.4f}<\/span><span style=\"color: #CE9178\">)&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Evaluate Random Forest<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">rf_scores = cross_val_score(rf, X, y, <\/span><span style=\"color: #9CDCFE\">cv<\/span><span style=\"color: #D4D4D4\">=kf, <\/span><span style=\"color: #9CDCFE\">scoring<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&#39;neg_mean_squared_error&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">f<\/span><span style=\"color: #CE9178\">&#39;Random Forest MSE: <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">-rf_scores.mean()<\/span><span style=\"color: #569CD6\">:.4f}<\/span><span style=\"color: #CE9178\"> (+\/- <\/span><span style=\"color: #569CD6\">{<\/span><span style=\"color: #D4D4D4\">rf_scores.std() * <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #569CD6\">:.4f}<\/span><span style=\"color: #CE9178\">)&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this code snippet, we\u2019re using K-Fold Cross-Validation to evaluate Ridge Regression and Random Forest models. The cross_val_score function computes the score for each fold, and we calculate the mean and standard deviation of the scores to assess model performance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Case Study: Cross-Validation in House Price Prediction<\/h3>\n\n\n\n<p>I used the following code to evaluate the performance of Ridge Regression and Elastic Net, using custom scoring metrics in a cross-validation setting.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Custom MAPE Function<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"def custom_mape(y_true, y_pred):\n    return np.mean(np.abs((y_true - y_pred) \/ y_true)) * 100\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">custom_mape<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">y_true<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">y_pred<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> np.mean(np.abs((y_true - y_pred) \/ y_true)) * <\/span><span style=\"color: #B5CEA8\">100<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This function calculates the Mean Absolute Percentage Error (MAPE), which measures the accuracy of predictions as a percentage. The lower the MAPE, the better the model&#8217;s predictions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Custom Scoring Function<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"def custom_scoring():\n    return {\n        'MAE': 'neg_mean_absolute_error',\n        'MSE': 'neg_mean_squared_error',\n        'MAPE': make_scorer(custom_mape, greater_is_better=False),\n        'MedAE': 'neg_median_absolute_error',\n        'R2': 'r2',\n        'RMSE': make_scorer(lambda y, y_pred: np.sqrt(mean_squared_error(y, y_pred)), greater_is_better=False)\n    }\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">custom_scoring<\/span><span style=\"color: #D4D4D4\">():<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #CE9178\">&#39;MAE&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #CE9178\">&#39;neg_mean_absolute_error&#39;<\/span><span style=\"color: #D4D4D4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #CE9178\">&#39;MSE&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #CE9178\">&#39;neg_mean_squared_error&#39;<\/span><span style=\"color: #D4D4D4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #CE9178\">&#39;MAPE&#39;<\/span><span style=\"color: #D4D4D4\">: make_scorer(custom_mape, <\/span><span style=\"color: #9CDCFE\">greater_is_better<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #569CD6\">False<\/span><span style=\"color: #D4D4D4\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #CE9178\">&#39;MedAE&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #CE9178\">&#39;neg_median_absolute_error&#39;<\/span><span style=\"color: #D4D4D4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #CE9178\">&#39;R2&#39;<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #CE9178\">&#39;r2&#39;<\/span><span style=\"color: #D4D4D4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #CE9178\">&#39;RMSE&#39;<\/span><span style=\"color: #D4D4D4\">: make_scorer(<\/span><span style=\"color: #569CD6\">lambda<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">y<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">y_pred<\/span><span style=\"color: #D4D4D4\">: np.sqrt(mean_squared_error(y, y_pred)), <\/span><span style=\"color: #9CDCFE\">greater_is_better<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #569CD6\">False<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This function defines a set of custom scoring metrics to be used in cross-validation. It includes:<\/p>\n\n\n\n<p><strong>MAE (Mean Absolute Error):<\/strong> Evaluates the average absolute difference between predicted and actual values.<\/p>\n\n\n\n<p><strong>MSE (Mean Squared Error):<\/strong> Measures the average squared difference between predicted and actual values.<\/p>\n\n\n\n<p><strong>MAPE (Mean Absolute Percentage Error):<\/strong> Custom scorer using the <code>custom_mape<\/code> function.<\/p>\n\n\n\n<p><strong>MedAE (Median Absolute Error):<\/strong> The median of absolute errors.<\/p>\n\n\n\n<p><strong>R2 (R-squared):<\/strong> Measures the proportion of variance explained by the model.<\/p>\n\n\n\n<p><strong>RMSE (Root Mean Squared Error):<\/strong> Custom scorer that calculates the square root of MSE, which is useful for interpreting errors in the same units as the target variable.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Ridge Regression with Cross-Validation<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"ridge_params = {'alpha': [0.1, 1, 10, 100, 1000]}\nridge = GridSearchCV(Ridge(random_state=42), ridge_params, cv=5, scoring='neg_mean_squared_error')\nridge.fit(X_processed, y)\nbest_ridge = ridge.best_estimator_\nridge_scores = cross_validate(best_ridge, X_processed, y, cv=5, scoring=custom_scoring())\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">ridge_params = {<\/span><span style=\"color: #CE9178\">&#39;alpha&#39;<\/span><span style=\"color: #D4D4D4\">: [<\/span><span style=\"color: #B5CEA8\">0.1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1000<\/span><span style=\"color: #D4D4D4\">]}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">ridge = GridSearchCV(Ridge(<\/span><span style=\"color: #9CDCFE\">random_state<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">42<\/span><span style=\"color: #D4D4D4\">), ridge_params, <\/span><span style=\"color: #9CDCFE\">cv<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">scoring<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&#39;neg_mean_squared_error&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">ridge.fit(X_processed, y)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">best_ridge = ridge.best_estimator_<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">ridge_scores = cross_validate(best_ridge, X_processed, y, <\/span><span style=\"color: #9CDCFE\">cv<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">scoring<\/span><span style=\"color: #D4D4D4\">=custom_scoring())<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Parameter Tuning:<\/strong> GridSearchCV is used to perform hyperparameter tuning on the Ridge regression model over a range of alpha values.<br><strong>Cross-Validation:<\/strong> The best model from the grid search (best_ridge) is then evaluated using cross-validation with the custom scoring metrics defined earlier.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Elastic Net with Cross-Validation<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"elastic_net_params = {\n    'alpha': [0.001, 0.01, 0.1, 1, 10, 100],\n    'l1_ratio': [0.1, 0.3, 0.5, 0.7, 0.9],\n    'max_iter': [100000],\n    'tol': [1e-4]\n}\nelastic_net = GridSearchCV(ElasticNet(random_state=42), elastic_net_params, cv=5, scoring='neg_mean_squared_error', n_jobs=-1)\nelastic_net.fit(X_processed, y)\nbest_elastic_net = elastic_net.best_estimator_\nelastic_net_scores = cross_validate(best_elastic_net, X_processed, y, cv=5, scoring=custom_scoring())\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">elastic_net_params = {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #CE9178\">&#39;alpha&#39;<\/span><span style=\"color: #D4D4D4\">: [<\/span><span style=\"color: #B5CEA8\">0.001<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">0.01<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">0.1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">100<\/span><span style=\"color: #D4D4D4\">],<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #CE9178\">&#39;l1_ratio&#39;<\/span><span style=\"color: #D4D4D4\">: [<\/span><span style=\"color: #B5CEA8\">0.1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">0.3<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">0.5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">0.7<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">0.9<\/span><span style=\"color: #D4D4D4\">],<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #CE9178\">&#39;max_iter&#39;<\/span><span style=\"color: #D4D4D4\">: [<\/span><span style=\"color: #B5CEA8\">100000<\/span><span style=\"color: #D4D4D4\">],<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #CE9178\">&#39;tol&#39;<\/span><span style=\"color: #D4D4D4\">: [<\/span><span style=\"color: #B5CEA8\">1e-4<\/span><span style=\"color: #D4D4D4\">]<\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">elastic_net = GridSearchCV(ElasticNet(<\/span><span style=\"color: #9CDCFE\">random_state<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">42<\/span><span style=\"color: #D4D4D4\">), elastic_net_params, <\/span><span style=\"color: #9CDCFE\">cv<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">scoring<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&#39;neg_mean_squared_error&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n_jobs<\/span><span style=\"color: #D4D4D4\">=-<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">elastic_net.fit(X_processed, y)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">best_elastic_net = elastic_net.best_estimator_<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">elastic_net_scores = cross_validate(best_elastic_net, X_processed, y, <\/span><span style=\"color: #9CDCFE\">cv<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">scoring<\/span><span style=\"color: #D4D4D4\">=custom_scoring())<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Elastic Net is tuned using GridSearchCV over a range of alpha, l1_ratio, max_iter, and tol parameters.<br>The best Elastic Net model is then evaluated using cross-validation and the custom scoring metrics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. Conclusion<\/h3>\n\n\n\n<p>Cross-validation is a critical step in the machine learning pipeline that ensures your model is robust and generalizes well to new data. <\/p>\n\n\n\n<p>By leveraging techniques like K-Fold Cross-Validation, you can confidently evaluate and select the best model, minimizing the risk of overfitting and underfitting.<\/p>\n\n\n\n<p>As you continue to refine your models and explore more advanced techniques, remember that cross-validation is your ally in achieving reliable and trustworthy machine learning solutions.<\/p>\n\n\n\n<div class=\"wp-block-jin-gb-block-icon-box jin-icon-caution jin-iconbox\"><div class=\"jin-iconbox-icons\"><i class=\"jic jin-ifont-caution jin-icons\"><\/i><\/div><div class=\"jin-iconbox-main\">\n<p><strong>Why Is Cross-Validation Crucial for Safety-Critical Applications?<\/strong><\/p>\n\n\n\n<p>1. <strong>Robust Performance Estimation:<\/strong><br>In safety-critical systems, unreliable performance can lead to catastrophic outcomes. Cross-validation provides a more accurate and robust estimate of a model&#8217;s performance across various data subsets, reducing the risk of overestimating a model&#8217;s capabilities.<\/p>\n\n\n\n<p>2. <strong>Identification of Instabilities:<\/strong><br>Cross-validation helps identify models that perform inconsistently across different data subsets. In safety-critical applications, such instabilities could lead to unpredictable behavior in real-world scenarios.<\/p>\n\n\n\n<p>3. <strong>Mitigation of Overfitting:<\/strong><br>Overfitted models may perform well on test data but fail dramatically in real-world situations. Cross-validation helps detect and prevent overfitting, ensuring the model generalizes well to new, unseen data in critical applications.<\/p>\n\n\n\n<p>4. <strong>Validation of Model Robustness:<\/strong><br>Safety-critical applications require models that perform consistently under various conditions. Cross-validation tests the model&#8217;s performance across different data splits, simulating diverse real-world scenarios.<\/p>\n\n\n\n<p>5. <strong>Hyperparameter Optimization:<\/strong><br>Proper tuning of model hyperparameters is crucial for optimal performance. Cross-validation allows for more reliable hyperparameter optimization, ensuring the model is well-configured for safety-critical tasks.<\/p>\n\n\n\n<p>6. <strong>Detection of Data Sensitivities:<\/strong><br>Cross-validation can reveal if a model&#8217;s performance is overly sensitive to specific data subsets. In safety-critical applications, understanding these sensitivities is crucial for assessing potential failure modes.<\/p>\n\n\n\n<p>7. <strong>Compliance with Safety Standards:<\/strong><br>Many safety standards and regulations require thorough validation of AI\/ML models. Cross-validation provides a systematic approach to model evaluation that can help meet these regulatory requirements.<\/p>\n\n\n\n<p>8. <strong>Confidence in Model Deployment:<\/strong><br>By providing a comprehensive evaluation of model performance, cross-validation increases confidence in the model&#8217;s readiness for deployment in high-stakes, safety-critical environments.<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Cross-validation is a fundamental technique in machine learning that helps ensure the robustness and generalizability of your models. As we#8217;ve explored various algorithms and methods in our journey through predictive modeling, understanding and effectively applying cross-validation is essential to avoid overfitting and to gauge the true performance of your models. In this post, we\u2019ll dive<\/p>\n","protected":false},"author":1,"featured_media":21,"parent":107,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4005","page","type-page","status-publish","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/pages\/4005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4005"}],"version-history":[{"count":32,"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/pages\/4005\/revisions"}],"predecessor-version":[{"id":5339,"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/pages\/4005\/revisions\/5339"}],"up":[{"embeddable":true,"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/pages\/107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/media\/21"}],"wp:attachment":[{"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}