{ "cells": [ { "cell_type": "markdown", "id": "af50604f", "metadata": {}, "source": [ "# IPython-Beispiele" ] }, { "cell_type": "markdown", "id": "768c687f", "metadata": {}, "source": [ "## Ausführen von Python-Code" ] }, { "cell_type": "markdown", "id": "881b77e4", "metadata": {}, "source": [ "### Python-Version anzeigen" ] }, { "cell_type": "code", "execution_count": 1, "id": "7fc23244", "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": "795d06c9", "metadata": {}, "source": [ "### Versionen von Python-Paketen anzeigen\n", "\n", "Die meisten Python-Pakete bieten hierfür eine Methode `__version__`:" ] }, { "cell_type": "code", "execution_count": 2, "id": "dd2d48f5", "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": "3c2255ff", "metadata": {}, "source": [ "Alternativ könnt ihr auch `version` aus `importlib_metadata` verwenden:" ] }, { "cell_type": "code", "execution_count": 3, "id": "2b13ffaf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.2.3\n" ] } ], "source": [ "from importlib.metadata import version\n", "\n", "\n", "print(version(\"pandas\"))" ] }, { "cell_type": "markdown", "id": "8378b094", "metadata": {}, "source": [ "### Informationen über das Host-Betriebssystem und die Versionen installierter Python-Pakete" ] }, { "cell_type": "code", "execution_count": 4, "id": "f8a8472a", "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.1.0\n", "Version : Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:11 PDT 2024; root:xnu-11215.41.3~2/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.0.2\n", "pytz : 2024.2\n", "dateutil : 2.9.0.post0\n", "pip : None\n", "Cython : 3.0.11\n", "sphinx : None\n", "IPython : 8.29.0\n", "adbc-driver-postgresql: None\n", "adbc-driver-sqlite : None\n", "bs4 : 4.12.3\n", "blosc : None\n", "bottleneck : None\n", "dataframe-api-compat : None\n", "fastparquet : None\n", "fsspec : 2024.10.0\n", "html5lib : None\n", "hypothesis : 6.116.0\n", "gcsfs : None\n", "jinja2 : 3.1.4\n", "lxml.etree : 5.3.0\n", "matplotlib : 3.9.2\n", "numba : None\n", "numexpr : None\n", "odfpy : None\n", "openpyxl : 3.1.5\n", "pandas_gbq : None\n", "psycopg2 : None\n", "pymysql : None\n", "pyarrow : 18.0.0\n", "pyreadstat : None\n", "pytest : 8.3.3\n", "python-calamine : None\n", "pyxlsb : None\n", "s3fs : 2024.10.0\n", "scipy : 1.14.1\n", "sqlalchemy : None\n", "tables : None\n", "tabulate : None\n", "xarray : None\n", "xlrd : None\n", "xlsxwriter : None\n", "zstandard : None\n", "tzdata : 2024.2\n", "qtpy : None\n", "pyqt5 : None\n" ] } ], "source": [ "pd.show_versions()" ] }, { "cell_type": "markdown", "id": "cbd67c04", "metadata": {}, "source": [ "### Nur Python-Versionen ≥ 3.9 verwenden" ] }, { "cell_type": "code", "execution_count": 5, "id": "b05cadb2", "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "\n", "assert sys.version_info[:2] >= (3, 9)" ] }, { "cell_type": "markdown", "id": "bbbf90d1", "metadata": {}, "source": [ "## Shell-Kommandos" ] }, { "cell_type": "code", "execution_count": 6, "id": "a03b7313", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python 3.13.0\n" ] } ], "source": [ "!python3 -V" ] }, { "cell_type": "code", "execution_count": 7, "id": "4fca9214", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/veit/.cache/uv/archive-v0/XZHbu_8b1Dy5_kyKvyRMu/bin/python3: No module named pip\n" ] } ], "source": [ "!python3 -m pip --version" ] }, { "cell_type": "markdown", "id": "80e00a17", "metadata": {}, "source": [ "## Tab-Vervollständigung" ] }, { "cell_type": "markdown", "id": "a357b63a", "metadata": {}, "source": [ "… für Objekte mit Methoden und Attributen:\n", "\n", "![Tab-Vervollständigung für Objekte](tab-completion-for-objects.png)" ] }, { "cell_type": "markdown", "id": "af02468c", "metadata": {}, "source": [ "… und auch für Module:\n", "\n", "![Tab-Vervollständigung für Module](tab-completion-for-modules.png)" ] }, { "cell_type": "markdown", "id": "38da6c4f", "metadata": {}, "source": [ "
\n", " \n", "**Bemerkung:**\n", "\n", "Wie ihr jetzt vielleicht verwundert festgestellt habt, wird die oben verwendete Methode `__version__` in der Auswahl nicht angeboten. IPython blendet diese _privaten_ Methoden und Attribute, , die mit Unterstrichen beginnen, zunächst aus. Sie können jedoch auch mit einem Tabulator vervollständigt werden, wenn ihr zunächst einen Unterstrich eingebt. Alternativ könnt ihr diese Einstellung auch in der IPython-Konfiguration ändern.\n", "
" ] }, { "cell_type": "markdown", "id": "901536b0", "metadata": {}, "source": [ "… für fast alles:\n", "\n", "![Tab-Vervollständigung für fast alles](tab-completion-for-anything.png)" ] }, { "cell_type": "markdown", "id": "55c9f0e5", "metadata": {}, "source": [ "## Informationen über ein Objekt anzeigen\n", "\n", "Mit einem Fragezeichen (`?`) könnt ihr euch Informationen über ein Objekt anzeigen lassen, wenn es z.B. eine Methode `multiply` mit folgendem Docstring gibt:" ] }, { "cell_type": "code", "execution_count": 8, "id": "6abb2eb9", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 9, "id": "7d7738c9", "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 ~/cusy/trn/jupyter-tutorial/uvenvs/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": "7cbc97ee", "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": "03de3c8e", "metadata": {}, "source": [ "Durch die Verwendung von `??` wird auch der Quellcode der Funktion angezeigt, sofern dies möglich ist:" ] }, { "cell_type": "code", "execution_count": 10, "id": "3d751993", "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 ~/cusy/trn/jupyter-tutorial/uvenvs/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", "…\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "np.mean??" ] }, { "cell_type": "markdown", "id": "b19f73c6", "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": "662d7fef", "metadata": {}, "source": [ "`?` kann auch zur Suche im IPython-Namensraum verwendet werden. Dabei kann eine Reihe von Zeichen mit dem Platzhalter (`*`) dargestellt werden. Um z.B. eine Liste aller Funktionen im NumPy-Namensraum der obersten Ebene erhalten, die `mean` enthalten:" ] }, { "cell_type": "code", "execution_count": 11, "id": "ef009e53", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.mean\n", "np.nanmean" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "np.*mean*?" ] }, { "cell_type": "markdown", "id": "8d61f1c2", "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 }