{ "cells": [ { "cell_type": "markdown", "id": "052d5df9", "metadata": {}, "source": [ "# IPython examples" ] }, { "cell_type": "markdown", "id": "ddab5ceb", "metadata": {}, "source": [ "## Running Python code" ] }, { "cell_type": "markdown", "id": "bf1366c2", "metadata": {}, "source": [ "### Show Python version" ] }, { "cell_type": "code", "execution_count": 1, "id": "350b0b1f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "sys.version_info(major=3, minor=13, micro=0, releaselevel='final', serial=0)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "\n", "\n", "sys.version_info" ] }, { "cell_type": "markdown", "id": "85985c4b", "metadata": {}, "source": [ "### Show versions of Python packages\n", "\n", "Most Python packages provide a `__version__` method for this:" ] }, { "cell_type": "code", "execution_count": 2, "id": "07dbedd0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2.2.3'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "\n", "pd.__version__" ] }, { "cell_type": "markdown", "id": "f8603063", "metadata": {}, "source": [ "Alternatively, you can use `version` from `importlib_metadata`:" ] }, { "cell_type": "code", "execution_count": 3, "id": "59e1742e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.2.3\n" ] } ], "source": [ "from importlib.metadata import version\n", "\n", "print(version(\"pandas\"))" ] }, { "cell_type": "markdown", "id": "190d253a", "metadata": {}, "source": [ "### Information about the host operating system and the versions of installed Python packages" ] }, { "cell_type": "code", "execution_count": 4, "id": "65edc94b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "INSTALLED VERSIONS\n", "------------------\n", "commit : 0691c5cf90477d3503834d983f69350f250a6ff7\n", "python : 3.13.0\n", "python-bits : 64\n", "OS : Darwin\n", "OS-release : 24.0.0\n", "Version : Darwin Kernel Version 24.0.0: Tue Sep 24 23:37:36 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T6020\n", "machine : arm64\n", "processor : arm\n", "byteorder : little\n", "LC_ALL : None\n", "LANG : de_DE.UTF-8\n", "LOCALE : de_DE.UTF-8\n", "\n", "pandas : 2.2.3\n", "numpy : 2.1.2\n", "pytz : 2024.2\n", "dateutil : 2.9.0.post0\n", "pip : None\n", "…" ] } ], "source": [ "pd.show_versions()" ] }, { "cell_type": "markdown", "id": "cf1ad941", "metadata": {}, "source": [ "### Only use Python versions ≥ 3.9" ] }, { "cell_type": "code", "execution_count": 5, "id": "9525b5f6", "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "\n", "assert sys.version_info[:2] >= (3, 9)" ] }, { "cell_type": "markdown", "id": "c01a8834", "metadata": {}, "source": [ "## Shell commands" ] }, { "cell_type": "code", "execution_count": 6, "id": "64f57ba2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python 3.13.0\n" ] } ], "source": [ "!python3 -V" ] }, { "cell_type": "markdown", "id": "e3a8bd57", "metadata": {}, "source": [ "## Tab completion" ] }, { "cell_type": "markdown", "id": "bd5fee20", "metadata": {}, "source": [ "… for objects with methods and attributes:\n", "\n", "![Tab completion for objects](tab-completion-for-objects.png)" ] }, { "cell_type": "markdown", "id": "ede967a8", "metadata": {}, "source": [ "… and also for modules:\n", "\n", "![Tab completion for modules](tab-completion-for-modules.png)" ] }, { "cell_type": "markdown", "id": "2c53fb80", "metadata": {}, "source": [ "
\n", "\n", "**Note:**\n", "\n", "As you may have noticed in surprise, the `__version__` method used above is not offered in the selection. IPython initially hides these private methods and attributes that begin with underscores. However, they can also be completed with a tabulator if you first enter an underscore. Alternatively, you can change this setting in the IPython configuration.\n", "
" ] }, { "cell_type": "markdown", "id": "8303578e", "metadata": {}, "source": [ "… for almost everything:\n", "\n", "![Tab completion for almost everything](tab-completion-for-anything.png)" ] }, { "cell_type": "markdown", "id": "7259a471", "metadata": {}, "source": [ "## Displaying information about an object\n", "\n", "With a question mark (`?`) you can display information about an object if, for example, there is a method `multiply` with the following docstring:" ] }, { "cell_type": "code", "execution_count": 8, "id": "61159a4f", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 9, "id": "0234c59b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m \n", "\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mno\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mwhere\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mno\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mCall signature:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mType:\u001b[0m _ArrayFunctionDispatcher\n", "\u001b[0;31mString form:\u001b[0m \n", "\u001b[0;31mFile:\u001b[0m ~/sandbox/py313/.venv/lib/python3.13/site-packages/numpy/_core/fromnumeric.py\n", "\u001b[0;31mDocstring:\u001b[0m \n", "Compute the arithmetic mean along the specified axis.\n", "\n", "Returns the average of the array elements. The average is taken over\n", "the flattened array by default, otherwise over the specified axis.\n", "`float64` intermediate and return values are used for integer inputs.\n", "\n", "…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "np.mean?" ] }, { "cell_type": "markdown", "id": "e133a850", "metadata": {}, "source": [ "```rst\n", "Signature:\n", "np.mean(\n", " a,\n", " axis=None,\n", " dtype=None,\n", " out=None,\n", " keepdims=,\n", " *,\n", " where=,\n", ")\n", "Docstring:\n", "Compute the arithmetic mean along the specified axis.\n", "\n", "Returns the average of the array elements. The average is taken over\n", "the flattened array by default, otherwise over the specified axis.\n", "`float64` intermediate and return values are used for integer inputs.\n", "\n", "Parameters\n", "----------\n", "a : array_like\n", " Array containing numbers whose mean is desired. If `a` is not an\n", " array, a conversion is attempted.\n", "axis : None or int or tuple of ints, optional\n", " Axis or axes along which the means are computed. The default is to\n", " compute the mean of the flattened array.\n", "\n", " .. versionadded:: 1.7.0\n", "\n", " If this is a tuple of ints, a mean is performed over multiple axes,\n", " instead of a single axis or all the axes as before.\n", "dtype : data-type, optional\n", " Type to use in computing the mean. For integer inputs, the default\n", " is `float64`; for floating point inputs, it is the same as the\n", " input dtype.\n", "out : ndarray, optional\n", " Alternate output array in which to place the result. The default\n", " is ``None``; if provided, it must have the same shape as the\n", " expected output, but the type will be cast if necessary.\n", " See :ref:`ufuncs-output-type` for more details.\n", "\n", "keepdims : bool, optional\n", " If this is set to True, the axes which are reduced are left\n", " in the result as dimensions with size one. With this option,\n", " the result will broadcast correctly against the input array.\n", "\n", " If the default value is passed, then `keepdims` will not be\n", " passed through to the `mean` method of sub-classes of\n", " `ndarray`, however any non-default value will be. If the\n", " sub-class' method does not implement `keepdims` any\n", " exceptions will be raised.\n", "\n", "where : array_like of bool, optional\n", " Elements to include in the mean. See `~numpy.ufunc.reduce` for details.\n", "\n", " .. versionadded:: 1.20.0\n", "\n", "Returns\n", "-------\n", "m : ndarray, see dtype parameter above\n", " If `out=None`, returns a new array containing the mean values,\n", " otherwise a reference to the output array is returned.\n", "\n", "See Also\n", "--------\n", "average : Weighted average\n", "std, var, nanmean, nanstd, nanvar\n", "\n", "Notes\n", "-----\n", "The arithmetic mean is the sum of the elements along the axis divided\n", "by the number of elements.\n", "\n", "Note that for floating-point input, the mean is computed using the\n", "same precision the input has. Depending on the input data, this can\n", "cause the results to be inaccurate, especially for `float32` (see\n", "example below). Specifying a higher-precision accumulator using the\n", "`dtype` keyword can alleviate this issue.\n", "\n", "By default, `float16` results are computed using `float32` intermediates\n", "for extra precision.\n", "\n", "Examples\n", "--------\n", ">>> a = np.array([[1, 2], [3, 4]])\n", ">>> np.mean(a)\n", "2.5\n", ">>> np.mean(a, axis=0)\n", "array([2., 3.])\n", ">>> np.mean(a, axis=1)\n", "array([1.5, 3.5])\n", "\n", "In single precision, `mean` can be inaccurate:\n", "\n", ">>> a = np.zeros((2, 512*512), dtype=np.float32)\n", ">>> a[0, :] = 1.0\n", ">>> a[1, :] = 0.1\n", ">>> np.mean(a)\n", "0.54999924\n", "\n", "Computing the mean in float64 is more accurate:\n", "\n", ">>> np.mean(a, dtype=np.float64)\n", "0.55000000074505806 # may vary\n", "\n", "Specifying a where argument:\n", ">>> a = np.array([[5, 9, 13], [14, 10, 12], [11, 15, 19]])\n", ">>> np.mean(a)\n", "12.0\n", ">>> np.mean(a, where=[[True], [False], [False]])\n", "9.0\n", "File: ~/spack/var/spack/environments/python-38/.spack-env/view/lib/python3.8/site-packages/numpy/core/fromnumeric.py\n", "Type: function\n", "```" ] }, { "cell_type": "markdown", "id": "0361df51", "metadata": {}, "source": [ "By using `??` the source code of the function is also displayed, if this is possible:" ] }, { "cell_type": "code", "execution_count": 10, "id": "87e12669", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m \n", "\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mno\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mwhere\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mno\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mCall signature:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mType:\u001b[0m _ArrayFunctionDispatcher\n", "\u001b[0;31mString form:\u001b[0m \n", "\u001b[0;31mFile:\u001b[0m ~/sandbox/py313/.venv/lib/python3.13/site-packages/numpy/_core/fromnumeric.py\n", "\u001b[0;31mSource:\u001b[0m \n", "\u001b[0;34m@\u001b[0m\u001b[0marray_function_dispatch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_mean_dispatcher\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;32mdef\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_NoValue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mwhere\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_NoValue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;34m\"\"\"\u001b[0m\n", "\u001b[0;34m Compute the arithmetic mean along the specified axis.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m Returns the average of the array elements. The average is taken over\u001b[0m\n", "\u001b[0;34m the flattened array by default, otherwise over the specified axis.\u001b[0m\n", "\u001b[0;34m `float64` intermediate and return values are used for integer inputs.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m Parameters\u001b[0m\n", "\u001b[0;34m ----------\u001b[0m\n", "\u001b[0;34m a : array_like\u001b[0m\n", "\u001b[0;34m Array containing numbers whose mean is desired. If `a` is not an\u001b[0m\n", "\u001b[0;34m array, a conversion is attempted.\u001b[0m\n", "\u001b[0;34m axis : None or int or tuple of ints, optional\u001b[0m\n", "\u001b[0;34m Axis or axes along which the means are computed. The default is to\u001b[0m\n", "\u001b[0;34m compute the mean of the flattened array.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m .. versionadded:: 1.7.0\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m If this is a tuple of ints, a mean is performed over multiple axes,\u001b[0m\n", "\u001b[0;34m instead of a single axis or all the axes as before.\u001b[0m\n", "\u001b[0;34m dtype : data-type, optional\u001b[0m\n", "\u001b[0;34m Type to use in computing the mean. For integer inputs, the default\u001b[0m\n", "\u001b[0;34m is `float64`; for floating point inputs, it is the same as the\u001b[0m\n", "\u001b[0;34m input dtype.\u001b[0m\n", "\u001b[0;34m out : ndarray, optional\u001b[0m\n", "\u001b[0;34m Alternate output array in which to place the result. The default\u001b[0m\n", "\u001b[0;34m is ``None``; if provided, it must have the same shape as the\u001b[0m\n", "\u001b[0;34m expected output, but the type will be cast if necessary.\u001b[0m\n", "\u001b[0;34m See :ref:`ufuncs-output-type` for more details.\u001b[0m\n", "\u001b[0;34m See :ref:`ufuncs-output-type` for more details.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m keepdims : bool, optional\u001b[0m\n", "\u001b[0;34m If this is set to True, the axes which are reduced are left\u001b[0m\n", "\u001b[0;34m in the result as dimensions with size one. With this option,\u001b[0m\n", "\u001b[0;34m the result will broadcast correctly against the input array.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m If the default value is passed, then `keepdims` will not be\u001b[0m\n", "\u001b[0;34m passed through to the `mean` method of sub-classes of\u001b[0m\n", "\u001b[0;34m `ndarray`, however any non-default value will be. If the\u001b[0m\n", "\u001b[0;34m sub-class' method does not implement `keepdims` any\u001b[0m\n", "\u001b[0;34m exceptions will be raised.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m where : array_like of bool, optional\u001b[0m\n", "\u001b[0;34m Elements to include in the mean. See `~numpy.ufunc.reduce` for details.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m .. versionadded:: 1.20.0\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m Returns\u001b[0m\n", "\u001b[0;34m -------\u001b[0m\n", "\u001b[0;34m m : ndarray, see dtype parameter above\u001b[0m\n", "\u001b[0;34m If `out=None`, returns a new array containing the mean values,\u001b[0m\n", "\u001b[0;34m otherwise a reference to the output array is returned.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m See Also\u001b[0m\n", "\u001b[0;34m --------\u001b[0m\n", "\u001b[0;34m average : Weighted average\u001b[0m\n", "\u001b[0;34m std, var, nanmean, nanstd, nanvar\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m Notes\u001b[0m\n", "\u001b[0;34m -----\u001b[0m\n", "\u001b[0;34m The arithmetic mean is the sum of the elements along the axis divided\u001b[0m\n", "\u001b[0;34m by the number of elements.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m Note that for floating-point input, the mean is computed using the\u001b[0m\n", "\u001b[0;34m same precision the input has. Depending on the input data, this can\u001b[0m\n", "\u001b[0;34m cause the results to be inaccurate, especially for `float32` (see\u001b[0m\n", "\u001b[0;34m example below). Specifying a higher-precision accumulator using the\u001b[0m\n", "\u001b[0;34m `dtype` keyword can alleviate this issue.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m By default, `float16` results are computed using `float32` intermediates\u001b[0m\n", "\u001b[0;34m for extra precision.\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m Examples\u001b[0m\n", "\u001b[0;34m --------\u001b[0m\n", "\u001b[0;34m >>> import numpy as np\u001b[0m\n", "\u001b[0;34m >>> a = np.array([[1, 2], [3, 4]])\u001b[0m\n", "\u001b[0;34m >>> np.mean(a)\u001b[0m\n", "\u001b[0;34m 2.5\u001b[0m\n", "\u001b[0;34m >>> np.mean(a, axis=0)\u001b[0m\n", "\u001b[0;34m array([2., 3.])\u001b[0m\n", "\u001b[0;34m >>> np.mean(a, axis=1)\u001b[0m\n", "\u001b[0;34m array([1.5, 3.5])\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m In single precision, `mean` can be inaccurate:\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m >>> a = np.zeros((2, 512*512), dtype=np.float32)\u001b[0m\n", "\u001b[0;34m >>> a[0, :] = 1.0\u001b[0m\n", "\u001b[0;34m >>> a[1, :] = 0.1\u001b[0m\n", "\u001b[0;34m >>> np.mean(a)\u001b[0m\n", "\u001b[0;34m 0.54999924\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m Computing the mean in float64 is more accurate:\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m >>> np.mean(a, dtype=np.float64)\u001b[0m\n", "\u001b[0;34m 0.55000000074505806 # may vary\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m Specifying a where argument:\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m >>> a = np.array([[5, 9, 13], [14, 10, 12], [11, 15, 19]])\u001b[0m\n", "\u001b[0;34m >>> np.mean(a)\u001b[0m\n", "\u001b[0;34m 12.0\u001b[0m\n", "\u001b[0;34m >>> np.mean(a, where=[[True], [False], [False]])\u001b[0m\n", "\u001b[0;34m 9.0\u001b[0m\n", "\u001b[0;34m\u001b[0m\n", "\u001b[0;34m \"\"\"\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mkeepdims\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_NoValue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'keepdims'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mwhere\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_NoValue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'where'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwhere\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mmu\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mmean\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mAttributeError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mpass\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0m_methods\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_mean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mClass docstring:\u001b[0m\n", "Class to wrap functions with checks for __array_function__ overrides.\n", "\n", "All arguments are required, and can only be passed by position.\n", "\n", "Parameters\n", "----------\n", "dispatcher : function or None\n", " The dispatcher function that returns a single sequence-like object\n", " of all arguments relevant. It must have the same signature (except\n", " the default values) as the actual implementation.\n", " If ``None``, this is a ``like=`` dispatcher and the\n", " ``_ArrayFunctionDispatcher`` must be called with ``like`` as the\n", " first (additional and positional) argument.\n", "implementation : function\n", " Function that implements the operation on NumPy arrays without\n", " overrides. Arguments passed calling the ``_ArrayFunctionDispatcher``\n", " will be forwarded to this (and the ``dispatcher``) as if using\n", " ``*args, **kwargs``.\n", "\n", "Attributes\n", "----------\n", "_implementation : function\n", " The original implementation passed in." ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "np.mean??" ] }, { "cell_type": "markdown", "id": "93e9e896", "metadata": {}, "source": [ "```rst\n", "Signature:\n", "np.mean(\n", " a,\n", " axis=None,\n", " dtype=None,\n", " out=None,\n", " keepdims=,\n", " *,\n", " where=,\n", ")\n", "Source: \n", "@array_function_dispatch(_mean_dispatcher)\n", "def mean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, *,\n", " where=np._NoValue):\n", " \"\"\"\n", " Compute the arithmetic mean along the specified axis.\n", "\n", " Returns the average of the array elements. The average is taken over\n", " the flattened array by default, otherwise over the specified axis.\n", " `float64` intermediate and return values are used for integer inputs.\n", "\n", " Parameters\n", " ----------\n", " a : array_like\n", " Array containing numbers whose mean is desired. If `a` is not an\n", " array, a conversion is attempted.\n", " axis : None or int or tuple of ints, optional\n", " Axis or axes along which the means are computed. The default is to\n", " compute the mean of the flattened array.\n", "\n", " .. versionadded:: 1.7.0\n", "\n", " If this is a tuple of ints, a mean is performed over multiple axes,\n", " instead of a single axis or all the axes as before.\n", " dtype : data-type, optional\n", " Type to use in computing the mean. For integer inputs, the default\n", " is `float64`; for floating point inputs, it is the same as the\n", " input dtype.\n", " out : ndarray, optional\n", " Alternate output array in which to place the result. The default\n", " is ``None``; if provided, it must have the same shape as the\n", " expected output, but the type will be cast if necessary.\n", " See :ref:`ufuncs-output-type` for more details.\n", "\n", " keepdims : bool, optional\n", " If this is set to True, the axes which are reduced are left\n", " in the result as dimensions with size one. With this option,\n", " the result will broadcast correctly against the input array.\n", "\n", " If the default value is passed, then `keepdims` will not be\n", " passed through to the `mean` method of sub-classes of\n", " `ndarray`, however any non-default value will be. If the\n", " sub-class' method does not implement `keepdims` any\n", " exceptions will be raised.\n", "\n", " where : array_like of bool, optional\n", " Elements to include in the mean. See `~numpy.ufunc.reduce` for details.\n", "\n", " .. versionadded:: 1.20.0\n", "\n", " Returns\n", " -------\n", " m : ndarray, see dtype parameter above\n", " If `out=None`, returns a new array containing the mean values,\n", " otherwise a reference to the output array is returned.\n", "\n", " See Also\n", " --------\n", " average : Weighted average\n", " std, var, nanmean, nanstd, nanvar\n", "\n", " Notes\n", " -----\n", " The arithmetic mean is the sum of the elements along the axis divided\n", " by the number of elements.\n", "\n", " Note that for floating-point input, the mean is computed using the\n", " same precision the input has. Depending on the input data, this can\n", " cause the results to be inaccurate, especially for `float32` (see\n", " example below). Specifying a higher-precision accumulator using the\n", " `dtype` keyword can alleviate this issue.\n", "\n", " By default, `float16` results are computed using `float32` intermediates\n", " for extra precision.\n", "\n", " Examples\n", " --------\n", " >>> a = np.array([[1, 2], [3, 4]])\n", " >>> np.mean(a)\n", " 2.5\n", " >>> np.mean(a, axis=0)\n", " array([2., 3.])\n", " >>> np.mean(a, axis=1)\n", " array([1.5, 3.5])\n", "\n", " In single precision, `mean` can be inaccurate:\n", "\n", " >>> a = np.zeros((2, 512*512), dtype=np.float32)\n", " >>> a[0, :] = 1.0\n", " >>> a[1, :] = 0.1\n", " >>> np.mean(a)\n", " 0.54999924\n", "\n", " Computing the mean in float64 is more accurate:\n", "\n", " >>> np.mean(a, dtype=np.float64)\n", " 0.55000000074505806 # may vary\n", "\n", " Specifying a where argument:\n", " >>> a = np.array([[5, 9, 13], [14, 10, 12], [11, 15, 19]])\n", " >>> np.mean(a)\n", " 12.0\n", " >>> np.mean(a, where=[[True], [False], [False]])\n", " 9.0\n", "\n", " \"\"\"\n", " kwargs = {}\n", " if keepdims is not np._NoValue:\n", " kwargs['keepdims'] = keepdims\n", " if where is not np._NoValue:\n", " kwargs['where'] = where\n", " if type(a) is not mu.ndarray:\n", " try:\n", " mean = a.mean\n", " except AttributeError:\n", " pass\n", " else:\n", " return mean(axis=axis, dtype=dtype, out=out, **kwargs)\n", "\n", " return _methods._mean(a, axis=axis, dtype=dtype,\n", " out=out, **kwargs)\n", "File: ~/spack/var/spack/environments/python-38/.spack-env/view/lib/python3.8/site-packages/numpy/core/fromnumeric.py\n", "Type: function\n", "```" ] }, { "cell_type": "markdown", "id": "52223f5b", "metadata": {}, "source": [ "`?` can also be used to search in the IPython namespace. In doing so, a series of characters can be represented with the wildcard (`*`). For example, to get a list of all functions in the top-level NumPy namespace that contain `mean`:" ] }, { "cell_type": "code", "execution_count": 11, "id": "60d493b2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.mean\n", "np.nanmean" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "np.*mean*?" ] }, { "cell_type": "markdown", "id": "4757ac65", "metadata": {}, "source": [ "```rst\n", "np.mean\n", "np.nanmean\n", "```" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }