.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_mpg_svr.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_mpg_svr.py: ====================================== Plotting Bagging Regression Error Bars ====================================== This example demonstrates using `forestci` to calculate the error bars of the predictions of a :class:`sklearn.ensemble.BaggingRegressor` object. The data used here are the Auto MPG dataset by R. Quinlan, bundled under the Creative Commons Attribution 4.0 International license. See ``data/README.md`` for attribution and licensing details. .. GENERATED FROM PYTHON SOURCE LINES 13-70 .. image-sg:: /auto_examples/images/sphx_glr_plot_mpg_svr_001.png :alt: plot mpg svr :srcset: /auto_examples/images/sphx_glr_plot_mpg_svr_001.png :class: sphx-glr-single-img .. code-block:: Python # Regression Forest Example from pathlib import Path import numpy as np from matplotlib import pyplot as plt from sklearn.ensemble import BaggingRegressor from sklearn.svm import SVR import sklearn.model_selection as xval import forestci as fci # Load the bundled Auto MPG data data_path = Path.cwd() / "data" / "auto_mpg.csv" if not data_path.exists(): # Also support running ``python examples/plot_mpg_svr.py`` from the repo root. data_path = Path.cwd() / "examples" / "data" / "auto_mpg.csv" mpg_data = np.genfromtxt( data_path, delimiter=",", skip_header=1, ) # Separate the predictors and target, removing rows with missing values mpg_data = mpg_data[~np.isnan(mpg_data).any(axis=1)] mpg_X = mpg_data[:, :-1] mpg_y = mpg_data[:, -1] # Split the data into training and test sets X_train, X_test, y_train, y_test = xval.train_test_split( mpg_X, mpg_y, test_size=0.25, random_state=42 ) # Create a bagged SVR model n_estimators = 1000 bagger = BaggingRegressor( estimator=SVR(), n_estimators=n_estimators, random_state=42 ) bagger.fit(X_train, y_train) y_pred = bagger.predict(X_test) target_range = [mpg_y.min(), mpg_y.max()] # Plot predictions without error bars plt.scatter(y_test, y_pred) plt.plot(target_range, target_range, "k--") plt.xlabel("Reported MPG") plt.ylabel("Predicted MPG") plt.show() # Calculate the variance variance = fci.random_forest_error(bagger, X_train.shape, X_test) # Plot error bars for predictions using unbiased variance plt.errorbar(y_test, y_pred, yerr=np.sqrt(variance), fmt="o") plt.plot(target_range, target_range, "k--") plt.xlabel("Reported MPG") plt.ylabel("Predicted MPG") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.116 seconds) .. _sphx_glr_download_auto_examples_plot_mpg_svr.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mpg_svr.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mpg_svr.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_mpg_svr.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_