.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/mean_vs_median.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_mean_vs_median.py: ================ Mean vs Median ================ This script compares the errors based on mean and median. It shows that the errors based on mean are more sensitive to outliers than the errors based upon median. .. GENERATED FROM PYTHON SOURCE LINES 8-19 .. code-block:: Python import os import sys import numpy as np import easy_mpl from easy_mpl import plot import SeqMetrics from SeqMetrics import mse, med_seq_error, mae, median_abs_error, mape, mdape, me, mde .. GENERATED FROM PYTHON SOURCE LINES 20-26 .. code-block:: Python print('python version: ', sys.version) print('OS Name: ', os.name) print('numpy: ', np.__version__) print('easy_mpl: ', easy_mpl.__version__) print('SeqMetrics: ', SeqMetrics.__version__) .. rst-class:: sphx-glr-script-out .. code-block:: none python version: 3.8.20 (default, Nov 25 2025, 20:14:35) [GCC 9.4.0] OS Name: posix numpy: 1.24.4 easy_mpl: 0.21.5 SeqMetrics: 2.0.1 .. GENERATED FROM PYTHON SOURCE LINES 27-29 Consider that we have two outputs from our model. One output is normal and the other has an outlier. We will compare the errors based on mean and median. .. GENERATED FROM PYTHON SOURCE LINES 29-35 .. code-block:: Python true = np.random.random(100) pred = np.random.random(100) pred_o = pred.copy() pred_o[2] = 40 .. GENERATED FROM PYTHON SOURCE LINES 36-38 First see that how the array ``pred_o`` has just one outlier. The outlier is at index 2. .. GENERATED FROM PYTHON SOURCE LINES 38-43 .. code-block:: Python plot(true,'--.', show=False, label="True") plot(pred,'--.', show=False, label="Predicted") plot(pred_o, '--.', show=False, label="Predicted with outlier") .. image-sg:: /auto_examples/images/sphx_glr_mean_vs_median_001.png :alt: mean vs median :srcset: /auto_examples/images/sphx_glr_mean_vs_median_001.png, /auto_examples/images/sphx_glr_mean_vs_median_001_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 44-46 See the change in mean and median errors. The change in mean error is very high but the change in median error is very low. .. GENERATED FROM PYTHON SOURCE LINES 46-50 .. code-block:: Python print("Simple Error") print("Mean: ", round(me(true, pred), 2), '-->', round(me(true, pred_o), 2)) print("Median: ", round(mde(true, pred), 2), '-->', round(mde(true, pred_o), 2)) .. rst-class:: sphx-glr-script-out .. code-block:: none Simple Error Mean: 0.01 --> -0.38 Median: 0.04 --> 0.04 .. GENERATED FROM PYTHON SOURCE LINES 51-53 Similarly the change in mean squared error is very large but the change in median squared error is very small. .. GENERATED FROM PYTHON SOURCE LINES 53-58 .. code-block:: Python print("Squared Error") print(round(mse(true, pred), 2), '-->', round(mse(true, pred_o), 2)) print(round(med_seq_error(true, pred),2), '-->', round(med_seq_error(true, pred_o), 2)) .. rst-class:: sphx-glr-script-out .. code-block:: none Squared Error 0.17 --> 15.72 0.1 --> 0.1 .. GENERATED FROM PYTHON SOURCE LINES 59-64 .. code-block:: Python print("Asbolute Error") print("Mean: ", round(mae(true, pred), 2), '-->', round(mae(true, pred_o), 2)) print("Median: ", round(median_abs_error(true, pred), 2), '-->', round(median_abs_error(true, pred_o), 2)) .. rst-class:: sphx-glr-script-out .. code-block:: none Asbolute Error Mean: 0.34 --> 0.73 Median: 0.32 --> 0.32 .. GENERATED FROM PYTHON SOURCE LINES 65-70 .. code-block:: Python print("Abolute Percentage Error") print("Mean:", round(mape(true, pred), 2), '-->', round(mape(true, pred_o), 2)) print("Median:", round(mdape(true, pred), 2), '-->', round(mdape(true, pred_o), 2)) .. rst-class:: sphx-glr-script-out .. code-block:: none Abolute Percentage Error Mean: 165.63 --> 234.51 Median: 52.26 --> 53.15 .. GENERATED FROM PYTHON SOURCE LINES 71-73 So the erros based on mean are more sensitive to outliers than the errors based upon median. Therefore, if the data has outliers and we want to ignore them, we should use median based errors. On the other hand, if we want to focus on the outliers, we should use mean based errors. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.606 seconds) .. _sphx_glr_download_auto_examples_mean_vs_median.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: mean_vs_median.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: mean_vs_median.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: mean_vs_median.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_