.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\plot_user_guide.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_user_guide.py: =============== Base User guide =============== .. GENERATED FROM PYTHON SOURCE LINES 7-12 .. code-block:: Python # Author: Iulii Vasilev # # License: BSD 3 clause .. GENERATED FROM PYTHON SOURCE LINES 13-15 First, we will import modules and load data .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: Python import survivors.datasets as ds import survivors.constants as cnt X, y, features, categ, sch_nan = ds.load_pbc_dataset() bins = cnt.get_bins(time=y[cnt.TIME_NAME], cens=y[cnt.CENS_NAME]) print(bins) .. rst-class:: sphx-glr-script-out .. code-block:: none [ 41 42 43 ... 4189 4190 4191] .. GENERATED FROM PYTHON SOURCE LINES 24-26 Build Nonparametric KaplanMeier model and visualize survival function .. GENERATED FROM PYTHON SOURCE LINES 26-39 .. code-block:: Python import survivors.visualize as vis from survivors.external import KaplanMeier km = KaplanMeier() km.fit(durations=y["time"], right_censor=y["cens"]) sf_km = km.survival_function_at_times(times=bins) vis.plot_survival_function(sf_km, bins) bins_short = [50, 100, 1000, 2000, 3000] sf_km_short = km.survival_function_at_times(times=bins_short) vis.plot_survival_function(sf_km_short, bins_short) .. image-sg:: /auto_examples/images/sphx_glr_plot_user_guide_001.png :alt: plot user guide :srcset: /auto_examples/images/sphx_glr_plot_user_guide_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 40-42 Build Tree .. GENERATED FROM PYTHON SOURCE LINES 42-54 .. code-block:: Python from survivors.tree import CRAID cr = CRAID(criterion='logrank', depth=2, min_samples_leaf=0.1, signif=0.05, categ=categ, leaf_model="base") cr.fit(X, y) sf_cr = cr.predict_at_times(X, bins=bins, mode="surv") chf_cr = cr.predict_at_times(X, bins=bins, mode="hazard") print(chf_cr.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (418, 4151) .. GENERATED FROM PYTHON SOURCE LINES 55-57 Plot dependencies .. GENERATED FROM PYTHON SOURCE LINES 57-67 .. code-block:: Python import matplotlib.pyplot as plt cr.visualize(target=cnt.TIME_NAME, mode="surv") image = plt.imread(f'{cr.name}.png') fig, ax = plt.subplots(figsize=(10, 7)) ax.imshow(image) ax.axis('off') plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_user_guide_002.png :alt: plot user guide :srcset: /auto_examples/images/sphx_glr_plot_user_guide_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-70 Individual prediction .. GENERATED FROM PYTHON SOURCE LINES 70-76 .. code-block:: Python print("Target:", y[0]) print(cr.predict(X, target=cnt.TIME_NAME)[0]) print(cr.predict(X, target=cnt.CENS_NAME)[0]) print(cr.predict(X, target="depth")[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none Target: (True, 400.) 847.4363636363636 0.9272727272727272 2.0 .. GENERATED FROM PYTHON SOURCE LINES 77-79 Building ensembles of survival trees .. GENERATED FROM PYTHON SOURCE LINES 79-90 .. code-block:: Python from survivors.ensemble import BootstrapCRAID bstr = BootstrapCRAID(n_estimators=10, size_sample=0.7, ens_metric_name='IBS_REMAIN', max_features=0.3, criterion='peto', depth=10, min_samples_leaf=0.01, categ=categ, leaf_model="base") bstr.fit(X, y) sf_bstr = bstr.predict_at_times(X, bins=bins, mode="surv") .. rst-class:: sphx-glr-script-out .. code-block:: none fitted: 10 models. .. GENERATED FROM PYTHON SOURCE LINES 91-93 Evaluation of models .. GENERATED FROM PYTHON SOURCE LINES 93-105 .. code-block:: Python import survivors.metrics as metr mean_ibs = metr.ibs(y, y, sf_bstr, bins, axis=-1) mean_ibs # 0.071 ibs_by_obs = metr.ibs(y, y, sf_bstr, bins, axis=0) ibs_by_obs # [0.0138, 0.038, ..., 0.0000, 0.0007] ibs_by_time = metr.ibs(y, y, sf_bstr, bins, axis=1) ibs_by_time # [0.0047, 0.0037, ..., 0.0983, 0.3533] print(ibs_by_time.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (4151,) .. GENERATED FROM PYTHON SOURCE LINES 106-108 Predict comparison .. GENERATED FROM PYTHON SOURCE LINES 108-112 .. code-block:: Python vis.plot_func_comparison(y[0], [sf_km, sf_cr[0], sf_bstr[0]], ["KM", "CRAID", "BootstrapCRAID"]) .. image-sg:: /auto_examples/images/sphx_glr_plot_user_guide_003.png :alt: Prediction for terminal event with time=400.0 :srcset: /auto_examples/images/sphx_glr_plot_user_guide_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 113-115 Quality comparison in time .. GENERATED FROM PYTHON SOURCE LINES 115-120 .. code-block:: Python vis.plot_metric_comparison(y[0], [sf_km, sf_cr[0], sf_bstr[0]], ["KM", "CRAID", "BootstrapCRAID"], bins, metr.ibs_remain) vis.plot_metric_comparison(y[0], [sf_km, sf_cr[0], sf_bstr[0]], ["KM", "CRAID", "BootstrapCRAID"], bins, metr.auprc) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_user_guide_004.png :alt: ibs_remain(t) for terminal event with time=400.0 :srcset: /auto_examples/images/sphx_glr_plot_user_guide_004.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_user_guide_005.png :alt: auprc(t) for terminal event with time=400.0 :srcset: /auto_examples/images/sphx_glr_plot_user_guide_005.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.627 seconds) .. _sphx_glr_download_auto_examples_plot_user_guide.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_user_guide.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_user_guide.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_