{"id":180,"date":"2024-07-22T12:25:06","date_gmt":"2024-07-22T16:25:06","guid":{"rendered":"https:\/\/www.econai.tech\/?page_id=180"},"modified":"2024-08-23T09:16:45","modified_gmt":"2024-08-23T13:16:45","slug":"ensemble-learning","status":"publish","type":"page","link":"https:\/\/tomomitanaka.ai\/?page_id=180","title":{"rendered":"Ensemble Learning"},"content":{"rendered":"\n<div class=\"wp-block-jin-gb-block-box-with-headline kaisetsu-box1\"><div class=\"kaisetsu-box1-title\">Safety by Design Expert&#8217;s Note<\/div>\n<p>Ensemble learning is crucial for safety-critical AI systems because:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It improves prediction reliability and stability<\/li>\n\n\n\n<li>Combines models to mitigate individual failures and biases<\/li>\n\n\n\n<li>Offers comprehensive risk analysis through model diversity<\/li>\n\n\n\n<li>Provides insights into model behavior via cross-validation and feature importance<\/li>\n\n\n\n<li>Balances complexity and interpretability for safer system design<\/li>\n<\/ol>\n<\/div>\n\n\n\n<p>In our journey through machine learning models for house price prediction, we&#8217;ve explored various algorithms and techniques to improve their performance. <\/p>\n\n\n\n<p>Now, let&#8217;s dive into a powerful approach that combines the strengths of multiple models: Ensemble Learning.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Contents<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Understanding Ensemble Learning<\/li>\n\n\n\n<li>Types of Ensemble Methods<\/li>\n\n\n\n<li>Common Ensemble Techniques<\/li>\n\n\n\n<li>Implementing Ensemble Methods<\/li>\n\n\n\n<li>Case Study: Ensemble Models for House Price Prediction<\/li>\n\n\n\n<li>Best Practices and Considerations<\/li>\n\n\n\n<li>Conclusion<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">1. Understanding Ensemble Learning<\/h3>\n\n\n\n<p>Ensemble learning is a powerful machine learning technique that combines multiple models to improve predictive performance. Rather than relying on a single model to make predictions, ensemble methods aggregate the predictions from several models to produce a final outcome. This approach often leads to better accuracy, robustness, and generalization, making it a popular choice in many machine learning tasks.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Why Use Ensemble Learning?<\/strong><\/h4>\n\n\n\n<p>The core idea behind ensemble learning is that a group of models, when combined, can outperform any individual model within the group. This is particularly useful when individual models have different strengths and weaknesses. By leveraging their collective wisdom, ensemble methods can mitigate errors that a single model might make, leading to more reliable predictions. The primary advantages of ensemble learning include:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Improved Accuracy:<\/strong> By averaging or voting on predictions from multiple models, ensembles often achieve higher accuracy than individual models.<\/li>\n\n\n\n<li><strong>Reduced Overfitting:<\/strong> Ensemble methods, especially when using diverse models, tend to generalize better to new data, reducing the risk of overfitting.<\/li>\n\n\n\n<li><strong>Increased Robustness:<\/strong> Ensembles are less sensitive to noisy data and can handle variability in the input data better than individual models.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Types of Ensemble Methods<\/h3>\n\n\n\n<p>There are two main types of ensemble methods: <strong>Sequential Ensemble Methods<\/strong> and <strong>Parallel Ensemble Methods<\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Sequential Ensemble Methods<\/h4>\n\n\n\n<p>Sequential ensemble methods generate base learners sequentially, where each subsequent learner is trained to correct the errors made by the previous ones. The idea is to focus more on difficult cases that were misclassified or poorly predicted by earlier models. As a result, the model gradually improves as each learner in the sequence attempts to reduce the overall error.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Parallel Ensemble Methods<\/h4>\n\n\n\n<p>Parallel ensemble methods generate multiple base learners simultaneously, independent of each other. The final model aggregates the predictions of all learners, usually through averaging (for regression) or majority voting (for classification). This method leverages the diversity among the models, which helps in reducing variance and improving overall robustness.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Common Ensemble Techniques<\/h3>\n\n\n\n<p>There are several types of ensemble learning techniques, each with its own strengths and use cases. The most common ones include:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Sequential Ensemble Methods<\/h4>\n\n\n\n<p><strong>Boosting:<\/strong> Boosting sequentially trains models, where each new model tries to correct the errors made by the previous ones. The final prediction is a weighted sum of the predictions from all models. Popular boosting algorithms include AdaBoost, Gradient Boosting, and XGBoost. Boosting is particularly effective for reducing bias and improving the performance of weak learners.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Parallel Ensemble Methods<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Bagging (Bootstrap Aggregating)<\/strong><\/h5>\n\n\n\n<p>Bagging involves training multiple instances of the same model on different subsets of the training data, typically generated through random sampling with replacement. The most famous example of bagging is the Random Forest algorithm, which builds a collection of decision trees and averages their predictions. Bagging helps reduce variance, leading to more stable and accurate predictions.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Stacking<\/strong><\/h5>\n\n\n\n<p>Typically considered a Parallel Ensemble Method, but can have sequential elements.<br>Stacking, or stacked generalization, involves training multiple models and then combining their predictions using a meta-model (often a simple linear model). Each base model contributes to the final prediction, and the meta-model learns the best way to combine them. Stacking can achieve very high accuracy by leveraging the strengths of different models.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Voting<\/strong><\/h5>\n\n\n\n<p>Voting is one of the simplest ensemble methods where multiple models are trained, and their predictions are combined using a majority vote (for classification) or an average (for regression). The final prediction is determined by the consensus among the models. Voting can be either hard (majority vote) or soft (weighted probabilities).<\/p>\n\n\n\n<div class=\"wp-block-jin-gb-block-chat-block balloon-box balloon-left clearfix has-ccc-ballon has-efefef-bgballon\"><div class=\"balloon-icon maru\"><img decoding=\"async\" src=\"https:\/\/www.econai.tech\/wp-content\/uploads\/2024\/08\/jessica-pamp-2XeP0c-J5B4-unsplash.jpg\"\/><\/div><span class=\"icon-name\"><\/span><div class=\"balloon-serif\"><div class=\"balloon-content\">\n<p><strong>Stacking<\/strong><br>Stacking is an ensemble technique where multiple models are trained, and their predictions are combined using another model (meta-model) to make the final prediction. This approach leverages the strengths of different models to improve accuracy.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-jin-gb-block-chat-block balloon-box balloon-left clearfix has-ccc-ballon has-efefef-bgballon\"><div class=\"balloon-icon maru\"><img decoding=\"async\" src=\"https:\/\/www.econai.tech\/wp-content\/uploads\/2024\/08\/jessica-pamp-2XeP0c-J5B4-unsplash.jpg\"\/><\/div><span class=\"icon-name\"><\/span><div class=\"balloon-serif\"><div class=\"balloon-content\">\n<p><strong>Voting<\/strong><br>Voting is a simple ensemble method where multiple models are trained, and their predictions are combined by majority vote (for classification) or by averaging (for regression). This method helps in reducing the risk of errors from any single model.<\/p>\n<\/div><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. Implementing Ensemble Methods<\/h3>\n\n\n\n<p>Let&#8217;s look at how to implement some ensemble methods using scikit-learn:<\/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.ensemble import RandomForestRegressor, GradientBoostingRegressor\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import cross_val_score\n\n# Random Forest\nrf = RandomForestRegressor(n_estimators=100, random_state=42)\nrf_scores = cross_val_score(rf, X, y, cv=5)\nprint(&quot;Random Forest scores:&quot;, rf_scores.mean())\n\n# Gradient Boosting\ngb = GradientBoostingRegressor(n_estimators=100, random_state=42)\ngb_scores = cross_val_score(gb, X, y, cv=5)\nprint(&quot;Gradient Boosting scores:&quot;, gb_scores.mean())\n\n# Stacking\nfrom sklearn.ensemble import StackingRegressor\n\nestimators = [\n    ('rf', RandomForestRegressor(n_estimators=100, random_state=42)),\n    ('gb', GradientBoostingRegressor(n_estimators=100, random_state=42))\n]\nstacking_regressor = StackingRegressor(\n    estimators=estimators,\n    final_estimator=LinearRegression()\n)\nstacking_scores = cross_val_score(stacking_regressor, X, y, cv=5)\nprint(&quot;Stacking scores:&quot;, stacking_scores.mean())\" 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.ensemble <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> RandomForestRegressor, GradientBoostingRegressor<\/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\"> LinearRegression<\/span><\/span>\n<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<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Random Forest<\/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 style=\"color: #D4D4D4\">rf_scores = cross_val_score(rf, X, 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>\n<span class=\"line\"><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Random Forest scores:&quot;<\/span><span style=\"color: #D4D4D4\">, rf_scores.mean())<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Gradient Boosting<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">gb = GradientBoostingRegressor(<\/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 style=\"color: #D4D4D4\">gb_scores = cross_val_score(gb, X, 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>\n<span class=\"line\"><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Gradient Boosting scores:&quot;<\/span><span style=\"color: #D4D4D4\">, gb_scores.mean())<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Stacking<\/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\"> StackingRegressor<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">estimators = [<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    (<\/span><span style=\"color: #CE9178\">&#39;rf&#39;<\/span><span style=\"color: #D4D4D4\">, 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 style=\"color: #D4D4D4\">    (<\/span><span style=\"color: #CE9178\">&#39;gb&#39;<\/span><span style=\"color: #D4D4D4\">, GradientBoostingRegressor(<\/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 style=\"color: #D4D4D4\">]<\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">stacking_regressor = StackingRegressor(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">estimators<\/span><span style=\"color: #D4D4D4\">=estimators,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">final_estimator<\/span><span style=\"color: #D4D4D4\">=LinearRegression()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">stacking_scores = cross_val_score(stacking_regressor, X, 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>\n<span class=\"line\"><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Stacking scores:&quot;<\/span><span style=\"color: #D4D4D4\">, stacking_scores.mean())<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>This code demonstrates the use of three ensemble methods: <strong>Random Forest<\/strong>, <strong>Gradient Boosting<\/strong>, and <strong>Stacking<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Random Forest<\/strong> averages predictions from multiple decision trees to improve accuracy and reduce overfitting.<\/li>\n\n\n\n<li><strong>Gradient Boosting<\/strong> builds trees sequentially, correcting previous errors to enhance performance.<\/li>\n\n\n\n<li><strong>Stacking<\/strong> combines predictions from both models using a final Linear Regression model, potentially boosting overall accuracy.<\/li>\n<\/ul>\n\n\n\n<p>Each model&#8217;s performance is evaluated using cross-validation, with mean scores printed for comparison.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Case Study: Ensemble Models for House Price Prediction<\/h3>\n\n\n\n<p>Let&#8217;s apply ensemble learning to our house price prediction task and compare the results with our previous models:<\/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=\"# Define models\nrf = RandomForestRegressor(n_estimators=100, random_state=42)\ngb = GradientBoostingRegressor(n_estimators=100, random_state=42)\nsvm = SVR(kernel='linear', C=834.2988013047346, epsilon=0.7722447692966574, gamma=0.0006235377135673155)\nen = ElasticNet(random_state=42)\n\nestimators = [\n    ('rf', rf),\n    ('gb', gb),\n    ('svm', svm),\n    ('en', en)\n]\n\nstacking_regressor = StackingRegressor(\n    estimators=estimators,\n    final_estimator=ElasticNet(random_state=42)\n)\n\nvoting_regressor = VotingRegressor(estimators=estimators)\n\nmodels = [rf, gb, svm, en, stacking_regressor, voting_regressor]\nmodel_names = ['Random Forest', 'Gradient Boosting', 'SVM (tuned)', 'Elastic Net', 'Stacking', 'Voting']\" 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: #6A9955\"># Define models<\/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 style=\"color: #D4D4D4\">gb = GradientBoostingRegressor(<\/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 style=\"color: #D4D4D4\">svm = SVR(<\/span><span style=\"color: #9CDCFE\">kernel<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&#39;linear&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">C<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">834.2988013047346<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">epsilon<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">0.7722447692966574<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">gamma<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #B5CEA8\">0.0006235377135673155<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">en = ElasticNet(<\/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: #D4D4D4\">estimators = [<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    (<\/span><span style=\"color: #CE9178\">&#39;rf&#39;<\/span><span style=\"color: #D4D4D4\">, rf),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    (<\/span><span style=\"color: #CE9178\">&#39;gb&#39;<\/span><span style=\"color: #D4D4D4\">, gb),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    (<\/span><span style=\"color: #CE9178\">&#39;svm&#39;<\/span><span style=\"color: #D4D4D4\">, svm),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    (<\/span><span style=\"color: #CE9178\">&#39;en&#39;<\/span><span style=\"color: #D4D4D4\">, en)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">]<\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">stacking_regressor = StackingRegressor(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">estimators<\/span><span style=\"color: #D4D4D4\">=estimators,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">final_estimator<\/span><span style=\"color: #D4D4D4\">=ElasticNet(<\/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 style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">voting_regressor = VotingRegressor(<\/span><span style=\"color: #9CDCFE\">estimators<\/span><span style=\"color: #D4D4D4\">=estimators)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">models = [rf, gb, svm, en, stacking_regressor, voting_regressor]<\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">model_names = [<\/span><span style=\"color: #CE9178\">&#39;Random Forest&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;Gradient Boosting&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;SVM (tuned)&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;Elastic Net&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;Stacking&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;Voting&#39;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>This code defines several models, including <strong>Random Forest<\/strong>, <strong>Gradient Boosting<\/strong>, a tuned <strong>SVM<\/strong>, and <strong>Elastic Net<\/strong>. These models are combined into two ensemble methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Stacking Regressor<\/strong>: Uses predictions from the individual models as inputs for a final Elastic Net estimator, capturing diverse patterns by leveraging multiple models.<\/li>\n\n\n\n<li><strong>Voting Regressor<\/strong>: Averages predictions from the models to reduce variance and improve accuracy.<\/li>\n<\/ul>\n\n\n\n<p>By comparing these ensembles with individual models, we can identify the best approach for our house price prediction task.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Results<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><\/td><td class=\"has-text-align-right\" data-align=\"right\">MAE<br>(thousand)<\/td><td class=\"has-text-align-right\" data-align=\"right\">MSE <br>(million)<\/td><td class=\"has-text-align-right\" data-align=\"right\">RMSE<br>(thousand)<\/td><td class=\"has-text-align-right\" data-align=\"right\">MAPE<\/td><td class=\"has-text-align-right\" data-align=\"right\">MedAE<br>(thousand)<\/td><td class=\"has-text-align-right\" data-align=\"right\">R\u00b2<\/td><\/tr><tr><td>Random Forest<\/td><td class=\"has-text-align-right\" data-align=\"right\">17.6<br>(\u00b12.4)<\/td><td class=\"has-text-align-right\" data-align=\"right\">937 <br>(\u00b1550)<\/td><td class=\"has-text-align-right\" data-align=\"right\">30.6 (\u00b123.4)<\/td><td class=\"has-text-align-right\" data-align=\"right\">10.14% (\u00b11.06%)<\/td><td class=\"has-text-align-right\" data-align=\"right\">11.0 (\u00b11.8)<\/td><td class=\"has-text-align-right\" data-align=\"right\">0.851<br>(\u00b10.072)<\/td><\/tr><tr><td>Gradient Boosting<\/td><td class=\"has-text-align-right\" data-align=\"right\">16.4<br>(\u00b11.8)<\/td><td class=\"has-text-align-right\" data-align=\"right\">841<br>(\u00b1453)<\/td><td class=\"has-text-align-right\" data-align=\"right\">29.0<br>(\u00b121,2)<\/td><td class=\"has-text-align-right\" data-align=\"right\">9.43%<br>(\u00b11.08%)<\/td><td class=\"has-text-align-right\" data-align=\"right\">10,1<br>(\u00b11.2)<\/td><td class=\"has-text-align-right\" data-align=\"right\">0.868<br>(\u00b10.052)<\/td><\/tr><tr><td>SVM (tuned)<\/td><td class=\"has-text-align-right\" data-align=\"right\">16.2<br>(\u00b12.8)<\/td><td class=\"has-text-align-right\" data-align=\"right\">855<br>(\u00b1614)<\/td><td class=\"has-text-align-right\" data-align=\"right\">29.2<br>(\u00b124.8)<\/td><td class=\"has-text-align-right\" data-align=\"right\">9.43%<br>(\u00b11.09%)<\/td><td class=\"has-text-align-right\" data-align=\"right\">10.5<br>(\u00b11.9)<\/td><td class=\"has-text-align-right\" data-align=\"right\">0.867<br>(\u00b10.071)<\/td><\/tr><tr><td>Elastic Net<\/td><td class=\"has-text-align-right\" data-align=\"right\">18.0<br>(\u00b12.7)<\/td><td class=\"has-text-align-right\" data-align=\"right\">972<br>(\u00b1643)<\/td><td class=\"has-text-align-right\" data-align=\"right\">31.2<br>(\u00b125.4)<\/td><td class=\"has-text-align-right\" data-align=\"right\">10.24<br>(\u00b11.07%)<\/td><td class=\"has-text-align-right\" data-align=\"right\">12.4<br>(\u00b11.9)<\/td><td class=\"has-text-align-right\" data-align=\"right\">0.848<br>(\u00b10.073)<\/td><\/tr><tr><td>Stacking<\/td><td class=\"has-text-align-right\" data-align=\"right\">15.8<br>(\u00b12.5)<\/td><td class=\"has-text-align-right\" data-align=\"right\">797<br>(\u00b1598)<\/td><td class=\"has-text-align-right\" data-align=\"right\"><strong>28.2<\/strong><br>(\u00b124.5)<\/td><td class=\"has-text-align-right\" data-align=\"right\">9.31<br>(\u00b11.10%)<\/td><td class=\"has-text-align-right\" data-align=\"right\">10.3<br>(\u00b11.4)<\/td><td class=\"has-text-align-right\" data-align=\"right\"><strong>0.876<\/strong><br>(\u00b10.073)<\/td><\/tr><tr><td>Voting<\/td><td class=\"has-text-align-right\" data-align=\"right\"><strong>15.6<\/strong><br>(\u00b12.3)<\/td><td class=\"has-text-align-right\" data-align=\"right\"><strong>795<\/strong><br>(\u00b1526)<\/td><td class=\"has-text-align-right\" data-align=\"right\"><strong>28.2<\/strong><br>(\u00b122.9)<\/td><td class=\"has-text-align-right\" data-align=\"right\"><strong>8.91<\/strong><br>(\u00b10.97%)<\/td><td class=\"has-text-align-right\" data-align=\"right\"><strong>10.0<\/strong><br>(\u00b11.6)<\/td><td class=\"has-text-align-right\" data-align=\"right\"><strong>0.876<\/strong><br>(\u00b10.061)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The results of various models and ensemble techniques are presented, comparing their performance across multiple evaluation metrics. <\/p>\n\n\n\n<p>Let&#8217;s break down the key observations:<\/p>\n\n\n\n<p><strong>Mean Absolute Error (MAE)<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>Voting Regressor<\/strong> achieves the lowest MAE, closely followed by the <strong>Stacking Regressor<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Mean Squared Error (MSE)<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>Voting Regressor<\/strong> and <strong>Stacking Regressor<\/strong> also performed best with MSEs, indicating these models made fewer large errors.<\/li>\n<\/ul>\n\n\n\n<p><strong>Root Mean Squared Error (RMSE)<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Consistent with the MSE results, the <strong>Voting<\/strong> and <strong>Stacking Regressors<\/strong> achieved the lowest RMSE.<\/li>\n<\/ul>\n\n\n\n<p><strong>Mean Absolute Percentage Error (MAPE)<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>Voting Regressor<\/strong> has the lowest MAPE, indicating it had the most accurate percentage-based predictions.<\/li>\n<\/ul>\n\n\n\n<p><strong>Median Absolute Error (MedAE)<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>Voting Regressor<\/strong> again leads with the lowest MedAE, showing that its typical predictions were very close to the actual values.<\/li>\n<\/ul>\n\n\n\n<p><strong>R-squared (R\u00b2)<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>Voting<\/strong> and <strong>Stacking Regressors<\/strong> achieved the highest R\u00b2 values at 0.876, indicating they explained the most variance in the data.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Summary<\/strong><\/h4>\n\n\n\n<p>The ensemble methods, particularly the <strong>Voting Regressor<\/strong>, consistently outperformed the individual models across most metrics, showing its effectiveness in combining predictions to improve overall accuracy. <\/p>\n\n\n\n<p>The <strong>Voting Regressor<\/strong> emerged as the most reliable model, delivering the lowest errors and highest explanatory power. This demonstrates the power of ensemble learning in producing robust, high-performance predictive models.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. Best Practices and Considerations<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Consider Interpretability<\/strong><\/h4>\n\n\n\n<p>While ensemble models often improve performance, they can be less interpretable than individual models. It&#8217;s important to weigh the benefits of accuracy against the need for model transparency.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Diversity is Key<\/strong><\/h4>\n\n\n\n<p>Ensure that the base models are diverse, particularly in their error patterns. This diversity is crucial for the ensemble to effectively combine their strengths and minimize weaknesses.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Balance Complexity<\/strong><\/h4>\n\n\n\n<p>Adding more models to an ensemble doesn&#8217;t always lead to better results. Consider the trade-off between improved accuracy and increased computational cost, and avoid unnecessary complexity.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Use Cross-Validation<\/strong><\/h4>\n\n\n\n<p>Cross-validation is essential, especially in stacking, to prevent overfitting and ensure the ensemble model generalizes well to unseen data.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Leverage Feature Importance<\/strong><\/h4>\n\n\n\n<p>Ensemble methods like Random Forest offer valuable insights into feature importance, helping you understand which variables contribute most to the predictions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. Conclusion<\/h3>\n\n\n\n<p>In this post, we explored the power of ensemble learning, which combines multiple models to improve predictive performance. By using techniques like Bagging, Boosting, Stacking, and Voting, ensemble learning enhances accuracy and reduces overfitting.<\/p>\n\n\n\n<p>Our house price prediction case study showed that ensemble methods, particularly the Voting Regressor, outperformed individual models, demonstrating their practical value.<\/p>\n\n\n\n<p>As we refine our models, it\u2019s crucial to balance performance with interpretability, as ensemble methods can sometimes complicate understanding model behavior.<\/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<h5 class=\"wp-block-heading\">Ensemble Techniques for Safety-Critical Contexts<\/h5>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>1. Boosting for Bias Reduction:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Benefit:<\/strong> Boosting (e.g., AdaBoost, Gradient Boosting) focuses on correcting difficult errors, reducing bias and improving accuracy in critical scenarios where mistakes can&#8217;t be overlooked.<\/li>\n<\/ul>\n\n\n\n<p><strong>2. Bagging for Robustness:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Benefit:<\/strong> Bagging (e.g., Random Forest) enhances stability by training models on different data subsets, making the ensemble resilient to data variability and noise, crucial for reliable performance in safety-critical applications.<\/li>\n<\/ul>\n\n\n\n<p><strong>3. Stacking for Comprehensive Decisions:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Benefit:<\/strong> Stacking combines multiple models, using a meta-model to refine predictions, ensuring decisions are well-rounded and reliable in safety-critical environments.<\/li>\n<\/ul>\n\n\n\n<p><strong>4. Voting for Increased Confidence:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Benefit:<\/strong> Voting ensembles aggregate predictions, boosting decision confidence and reducing the risk of unsafe outcomes by avoiding reliance on a single model.<\/li>\n<\/ul>\n\n\n\n<p><strong>Conclusion:<\/strong> Ensemble techniques like boosting, bagging, stacking, and voting offer tailored benefits for safety-critical AI systems, enhancing reliability, robustness, and safety in diverse conditions.<\/p>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our journey through machine learning models for house price prediction, we#8217;ve explored various algorithms and techniques to improve their performance. Now, let#8217;s dive into a powerful approach that combines the strengths of multiple models: Ensemble Learning. Contents 1. Understanding Ensemble Learning Ensemble learning is a powerful machine learning technique that combines multiple models to<\/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-180","page","type-page","status-publish","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/pages\/180","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=180"}],"version-history":[{"count":153,"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/pages\/180\/revisions"}],"predecessor-version":[{"id":5516,"href":"https:\/\/tomomitanaka.ai\/index.php?rest_route=\/wp\/v2\/pages\/180\/revisions\/5516"}],"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=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}