.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ngb_area.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_ngb_area.py: ===================== 2.2 NGBoost (Area) ===================== This file shows how to use ngboost for modeling microbial cell area data. .. GENERATED FROM PYTHON SOURCE LINES 7-29 .. code-block:: Python import shap import numpy as np import seaborn as sns from ngboost import NGBRegressor import matplotlib.pyplot as plt from easy_mpl import plot, bar_chart from easy_mpl.utils import make_clrs_from_cmap from ai4water import Model from ai4water.utils import TrainTestSplit from ai4water.postprocessing import PartialDependencePlot from utils import plot_stds, version_info, SAVE from utils import read_data, shap_scatter, ci_from_dist, plot_1d_pdp from utils import set_rcParams, COLUMN_MAPS_, residual_plot, regression_plot .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: Python for lib,ver in version_info().items(): print(lib, ver) .. rst-class:: sphx-glr-script-out .. code-block:: none python 3.9.20 (main, Nov 5 2024, 16:07:55) [GCC 11.4.0] os posix ai4water 1.07 easy_mpl 0.21.4 SeqMetrics 2.0.0 tensorflow 2.10.1 keras.api._v2.keras 2.10.0 numpy 1.21.6 pandas 1.5.3 matplotlib 3.7.1 h5py 3.13.0 sklearn 1.3.1 seaborn 0.13.2 ngboost 0.4.1 shap 0.41.0 .. GENERATED FROM PYTHON SOURCE LINES 35-38 .. code-block:: Python set_rcParams() .. GENERATED FROM PYTHON SOURCE LINES 39-47 .. code-block:: Python data = read_data(target='Area (ABD) Mean') input_features = data.columns.tolist()[0:-1] output_features = data.columns.tolist()[-1:] print(input_features) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Time (min)', 'Ini. CC', 'Sonic. PD', 'h20 Conc.', 'Volume (mL)', 'Solution pH'] .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python print(output_features) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Area (ABD) Mean'] .. GENERATED FROM PYTHON SOURCE LINES 51-59 .. code-block:: Python TrainX, TestX, TrainY, TestY = TrainTestSplit(seed=313).split_by_random( data[input_features], data[output_features] ) print(TrainX.shape, TestX.shape, TrainY.shape, TestY.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (219, 6) (95, 6) (219, 1) (95, 1) .. GENERATED FROM PYTHON SOURCE LINES 60-62 Model Building ================= .. GENERATED FROM PYTHON SOURCE LINES 62-72 .. code-block:: Python model = Model( model=NGBRegressor(early_stopping_rounds=50), mode="regression", category="ML", input_features=input_features, output_features=output_features, #wandb_config=dict(project="flowcam", entity="atherabbas", monitor="val_loss") ) .. rst-class:: sphx-glr-script-out .. code-block:: none building ML model for regression problem using NGBRegressor .. GENERATED FROM PYTHON SOURCE LINES 73-75 Model training ================== .. GENERATED FROM PYTHON SOURCE LINES 75-82 .. code-block:: Python rgr = model.fit( TrainX, TrainY.values, X_val=TestX, Y_val=TestY, ) .. rst-class:: sphx-glr-script-out .. code-block:: none [iter 0] loss=4.3134 val_loss=4.1396 scale=2.0000 norm=24.6733 [iter 100] loss=3.0922 val_loss=3.1274 scale=2.0000 norm=5.1099 [iter 200] loss=2.3100 val_loss=2.4381 scale=2.0000 norm=3.4496 == Early stopping achieved. == Best iteration / VAL250 (val_loss=2.3367) .. GENERATED FROM PYTHON SOURCE LINES 83-85 Prediction on training data ============================ .. GENERATED FROM PYTHON SOURCE LINES 85-93 .. code-block:: Python train_p = model.predict( TrainX.values, TrainY.values, log_on_wb=model.use_wb, prefix="train", plots=["prediction", "edf"], #max_iter=rgr.best_val_loss_itr ) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_001.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_001.png, /auto_examples/images/sphx_glr_ngb_area_001_2_00x.png 2.00x :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_002.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_002.png, /auto_examples/images/sphx_glr_ngb_area_002_2_00x.png 2.00x :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 94-102 .. code-block:: Python print(model.evaluate( TrainX, TrainY.values, metrics=["r2", "nse", "rmse", "mae"], # max_iter=rgr.best_val_loss_itr # todo why using it is not having any impact )) .. rst-class:: sphx-glr-script-out .. code-block:: none {'r2': 0.9738464496169206, 'nse': 0.973570732272537, 'rmse': 2.8966983272991245, 'mae': 1.5128984605585558} .. GENERATED FROM PYTHON SOURCE LINES 103-104 plotting mean prediction and variances (std deviation) .. GENERATED FROM PYTHON SOURCE LINES 104-115 .. code-block:: Python train_dist = rgr.pred_dist(TrainX.iloc[0:50]) train_std = train_dist.dist.std() train_mean = train_dist.dist.mean() plot_stds( train_mean, train_std, label="Area" ) .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_003.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_003.png, /auto_examples/images/sphx_glr_ngb_area_003_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-117 get negative log likelihood .. GENERATED FROM PYTHON SOURCE LINES 117-120 .. code-block:: Python print(-train_dist.logpdf(TrainY).mean()) .. rst-class:: sphx-glr-script-out .. code-block:: none 72.85926706877068 .. GENERATED FROM PYTHON SOURCE LINES 121-123 Prediction on test data ============================ .. GENERATED FROM PYTHON SOURCE LINES 123-131 .. code-block:: Python test_p = model.predict( TestX.values, TestY.values, log_on_wb=model.use_wb, plots=["prediction", "edf"], #max_iter=rgr.best_val_loss_itr ) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_004.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_004.png, /auto_examples/images/sphx_glr_ngb_area_004_2_00x.png 2.00x :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_005.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_005.png, /auto_examples/images/sphx_glr_ngb_area_005_2_00x.png 2.00x :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 132-137 .. code-block:: Python print(model.evaluate( TestX, TestY.values, metrics=["r2", "nse", "rmse", "mae"], # max_iter=rgr.best_val_loss_itr )) .. rst-class:: sphx-glr-script-out .. code-block:: none {'r2': 0.9447943624501315, 'nse': 0.9089081863987385, 'rmse': 6.031697562612959, 'mae': 2.667182827533436} .. GENERATED FROM PYTHON SOURCE LINES 138-148 .. code-block:: Python test_dist_50 = rgr.pred_dist(TestX.iloc[0:50]) test_std = test_dist_50.dist.std() test_mean = test_dist_50.dist.mean() plot_stds( test_mean, test_std, label="Area" ) .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_006.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_006.png, /auto_examples/images/sphx_glr_ngb_area_006_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 149-150 get negative log likelihood .. GENERATED FROM PYTHON SOURCE LINES 150-155 .. code-block:: Python test_dist = rgr.pred_dist(TestX) print(-test_dist.logpdf(TestY).mean()) .. rst-class:: sphx-glr-script-out .. code-block:: none 76.80971549871242 .. GENERATED FROM PYTHON SOURCE LINES 156-167 .. code-block:: Python residual_plot( TrainY.values, train_p, TestY.values, test_p, ) if SAVE: plt.savefig("results/figures/residue_ngb_area", dpi=600, bbox_inches="tight") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_007.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_007.png, /auto_examples/images/sphx_glr_ngb_area_007_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 168-169 Regression plot of training and test combined .. GENERATED FROM PYTHON SOURCE LINES 169-180 .. code-block:: Python ax = regression_plot( TrainY.values, train_p, TestY.values, test_p, label="Area") ax.set_xlim([10, 145]) ax.set_ylim([10, 125]) if SAVE: plt.savefig("results/figures/reg_ngb_area", dpi=600, bbox_inches="tight") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_008.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_008.png, /auto_examples/images/sphx_glr_ngb_area_008_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none *c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*. Please use the *color* keyword-argument or provide a 2D array with a single row if you intend to specify the same RGB or RGBA value for all points. .. GENERATED FROM PYTHON SOURCE LINES 181-184 Feature Importance ===================== Feature importance for loc trees .. GENERATED FROM PYTHON SOURCE LINES 184-187 .. code-block:: Python feature_importance_loc = rgr.feature_importances_[0] .. GENERATED FROM PYTHON SOURCE LINES 188-189 Feature importance for scale trees .. GENERATED FROM PYTHON SOURCE LINES 189-207 .. code-block:: Python feature_importance_scale = rgr.feature_importances_[1] bar_chart( feature_importance_loc, labels=input_features, color="skyblue", sort=True, ax_kws=dict( title="loc", ylabel="Input Features", xlabel="Importance", ), show=False ) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_009.png :alt: loc :srcset: /auto_examples/images/sphx_glr_ngb_area_009.png, /auto_examples/images/sphx_glr_ngb_area_009_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 208-225 .. code-block:: Python bar_chart( feature_importance_scale, labels=input_features, color="skyblue", sort=True, ax_kws=dict( title="scale", ylabel="Input Features", xlabel="Importance", ), show=False ) plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_010.png :alt: scale :srcset: /auto_examples/images/sphx_glr_ngb_area_010.png, /auto_examples/images/sphx_glr_ngb_area_010_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 226-229 SHAP ======= SHAP plot for loc trees .. GENERATED FROM PYTHON SOURCE LINES 229-235 .. code-block:: Python explainer = shap.TreeExplainer(rgr, model_output=0) shap_values = explainer.shap_values(TrainX.values) plot(shap_values.sum(axis=1) - TrainY.values.reshape(-1)) .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_011.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_011.png, /auto_examples/images/sphx_glr_ngb_area_011_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 236-237 The default cmap is boring so use a different color map .. GENERATED FROM PYTHON SOURCE LINES 237-253 .. code-block:: Python cm = sns.diverging_palette(0,100, as_cmap=True) feature_names = [COLUMN_MAPS_.get(fname, fname) for fname in input_features] shap.summary_plot(shap_values, TrainX.values, feature_names=feature_names, cmap=cm, alpha=0.7, show=False ) if SAVE: plt.savefig(f"results/figures/ngb_shap_loc_area.png", dpi=600, bbox_inches="tight") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_012.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_012.png, /auto_examples/images/sphx_glr_ngb_area_012_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none No data for colormapping provided via 'c'. Parameters 'vmin', 'vmax' will be ignored .. GENERATED FROM PYTHON SOURCE LINES 254-271 .. code-block:: Python sv_bar = np.mean(np.abs(shap_values), axis=0) bar_chart( sv_bar, feature_names, orient="horizontal", color=["#bb74b0", "#39a9e0", "#25b77c", "#9fa437", "#f07671", "#d7845b"], sort=True, show=False, ax_kws=dict(xlabel="mean(|SHAP value|)") ) if SAVE: plt.savefig(f"results/figures/ngb_shap_loc_bar_area.png", dpi=600, bbox_inches="tight") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_013.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_013.png, /auto_examples/images/sphx_glr_ngb_area_013_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 272-273 SHAP plot for scale trees .. GENERATED FROM PYTHON SOURCE LINES 273-279 .. code-block:: Python explainer = shap.TreeExplainer(rgr, model_output=1) shap_values = explainer.shap_values(TrainX.values) plot(shap_values.sum(axis=1) - TrainY.values.reshape(-1)) .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_014.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_014.png, /auto_examples/images/sphx_glr_ngb_area_014_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 280-294 .. code-block:: Python feature_names = [COLUMN_MAPS_.get(fname, fname) for fname in input_features] shap.summary_plot(shap_values, TrainX.values, feature_names=feature_names, cmap=cm, alpha=0.7, show=False ) if SAVE: plt.savefig(f"results/figures/ngb_shap_scale_area.png", dpi=600, bbox_inches="tight") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_015.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_015.png, /auto_examples/images/sphx_glr_ngb_area_015_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 295-311 .. code-block:: Python sv_bar = np.mean(np.abs(shap_values), axis=0) bar_chart( sv_bar, feature_names, orient="horizontal", color=["#bb74b0", "#39a9e0", "#25b77c", "#9fa437", "#f07671", "#d7845b"], sort=True, show=False, ax_kws=dict(xlabel="mean(|SHAP value|)") ) if SAVE: plt.savefig(f"results/figures/ngb_shap_scale_bar_area.png", dpi=600, bbox_inches="tight") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_016.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_016.png, /auto_examples/images/sphx_glr_ngb_area_016_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 312-321 .. code-block:: Python feature_name = 'Time (min)' shap_scatter( shap_values[:, 0], TrainX.loc[:, feature_name], color_feature=TrainX.loc[:, 'Ini. CC'], feature_name=feature_name ) .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_017.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_017.png, /auto_examples/images/sphx_glr_ngb_area_017_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 322-330 .. code-block:: Python feature_name = 'Time (min)' shap_scatter( shap_values[:, 0], TrainX.loc[:, feature_name], color_feature=TrainX.loc[:, 'Sonic. PD'], feature_name=feature_name ) .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_018.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_018.png, /auto_examples/images/sphx_glr_ngb_area_018_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 331-339 .. code-block:: Python feature_name = 'Time (min)' shap_scatter( shap_values[:, 0], TrainX.loc[:, feature_name], color_feature=TrainX.loc[:, 'h20 Conc.'], feature_name=feature_name ) .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_019.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_019.png, /auto_examples/images/sphx_glr_ngb_area_019_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 340-348 .. code-block:: Python feature_name = 'Time (min)' shap_scatter( shap_values[:, 0], TrainX.loc[:, feature_name], color_feature=TrainX.loc[:, 'Volume (mL)'], feature_name=feature_name ) .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_020.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_020.png, /auto_examples/images/sphx_glr_ngb_area_020_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 349-358 .. code-block:: Python feature_name = 'Time (min)' shap_scatter( shap_values[:, 0], TrainX.loc[:, feature_name], color_feature=TrainX.loc[:, 'Solution pH'], feature_name=feature_name ) .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_021.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_021.png, /auto_examples/images/sphx_glr_ngb_area_021_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 359-361 Partial Dependence Plot ========================= .. GENERATED FROM PYTHON SOURCE LINES 361-371 .. code-block:: Python pdp = PartialDependencePlot( model.predict, TrainX, num_points=20, feature_names=TrainX.columns.tolist(), show=False, save=False ) .. GENERATED FROM PYTHON SOURCE LINES 372-375 .. code-block:: Python plot_1d_pdp(pdp, TrainX.values, 'Time (min)') .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_022.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_022.png, /auto_examples/images/sphx_glr_ngb_area_022_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none This figure includes Axes that are not compatible with tight_layout, so results might be incorrect. .. GENERATED FROM PYTHON SOURCE LINES 376-379 .. code-block:: Python plot_1d_pdp(pdp, TrainX.values, 'Ini. CC') .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_023.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_023.png, /auto_examples/images/sphx_glr_ngb_area_023_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none This figure includes Axes that are not compatible with tight_layout, so results might be incorrect. .. GENERATED FROM PYTHON SOURCE LINES 380-383 .. code-block:: Python plot_1d_pdp(pdp, TrainX.values, 'Sonic. PD') .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_024.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_024.png, /auto_examples/images/sphx_glr_ngb_area_024_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none This figure includes Axes that are not compatible with tight_layout, so results might be incorrect. .. GENERATED FROM PYTHON SOURCE LINES 384-387 .. code-block:: Python plot_1d_pdp(pdp, TrainX.values, 'h20 Conc.') .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_025.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_025.png, /auto_examples/images/sphx_glr_ngb_area_025_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none This figure includes Axes that are not compatible with tight_layout, so results might be incorrect. .. GENERATED FROM PYTHON SOURCE LINES 388-391 .. code-block:: Python plot_1d_pdp(pdp, TrainX.values, 'Volume (mL)') .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_026.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_026.png, /auto_examples/images/sphx_glr_ngb_area_026_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none This figure includes Axes that are not compatible with tight_layout, so results might be incorrect. .. GENERATED FROM PYTHON SOURCE LINES 392-395 .. code-block:: Python plot_1d_pdp(pdp, TrainX.values, 'Solution pH') .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_027.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_027.png, /auto_examples/images/sphx_glr_ngb_area_027_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none This figure includes Axes that are not compatible with tight_layout, so results might be incorrect. .. GENERATED FROM PYTHON SOURCE LINES 396-400 .. code-block:: Python if model.use_wb: model.wb_finish() .. GENERATED FROM PYTHON SOURCE LINES 401-402 Plot probability distribution for individual samples. .. GENERATED FROM PYTHON SOURCE LINES 402-412 .. code-block:: Python clrs = make_clrs_from_cmap(cm="tab20", num_cols=10) y_pred_all = rgr.predict(data[input_features]) Y_dists = rgr.pred_dist(data[input_features]) y_range = np.linspace(data[output_features].min().item(), data[output_features].max().item(), len(data)) dist_values = Y_dists.pdf(y_range.reshape(-1,1)).transpose()# plot index 0 and 114 indices = [28, 35, 45, 54, 90, 100, 150, 200, 250, 300] .. GENERATED FROM PYTHON SOURCE LINES 413-437 .. code-block:: Python fig, all_axes = plt.subplots(10, 1, sharex="all", figsize=(6, 8)) idx1 = 0 for idx, ax in zip(indices, all_axes.flat): ax = plot(y_range, dist_values[idx], color=clrs[idx1], ax=ax, show=False) ax.axvline(y_pred_all[idx], color="darkgray") ax.text(x=0.5, y=0.7, s=f"Sample ID: {idx}", transform=ax.transAxes, fontsize=12, weight="bold") ax.set_yticks([]) idx1 += 1 xticks = np.array(ax.get_xticks()).astype(int) ax.set_xticklabels(xticks, weight="bold", fontsize=12) ax.set_xlabel("Area (mean)", fontsize=14, weight="bold") if SAVE: plt.savefig("results/figures/local_pdfs_area", dpi=600, bbox_inches="tight") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_028.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_028.png, /auto_examples/images/sphx_glr_ngb_area_028_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none FixedFormatter should only be used together with FixedLocator .. GENERATED FROM PYTHON SOURCE LINES 438-452 .. code-block:: Python ci_from_dist( Y_dists, 0.95, data[output_features].values, "Cell Count", fill_color = "forestgreen", line_color = "forestgreen" ) if SAVE: plt.savefig("results/figures/ci_95_ngb_area", dpi=600, bbox_inches="tight") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_029.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_029.png, /auto_examples/images/sphx_glr_ngb_area_029_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none FixedFormatter should only be used together with FixedLocator FixedFormatter should only be used together with FixedLocator .. GENERATED FROM PYTHON SOURCE LINES 453-465 .. code-block:: Python ci_from_dist( Y_dists, 0.9, data[output_features].values, "Area", fill_color = np.array([217, 140, 122])/255, line_color = np.array([180, 27, 40])/255 ) if SAVE: plt.savefig("results/figures/ci_90_ngb_area", dpi=600, bbox_inches="tight") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_ngb_area_030.png :alt: ngb area :srcset: /auto_examples/images/sphx_glr_ngb_area_030.png, /auto_examples/images/sphx_glr_ngb_area_030_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none FixedFormatter should only be used together with FixedLocator FixedFormatter should only be used together with FixedLocator .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 14.944 seconds) .. _sphx_glr_download_auto_examples_ngb_area.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ngb_area.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ngb_area.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ngb_area.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_