{ "cells": [ { "cell_type": "markdown", "id": "4f955299", "metadata": {}, "source": [ "# Methoden für boolesche Arrays\n", "\n", "Boolesche Werte wurden in den vorangegangenen Methoden in 1 (`True`) und 0 (`False`) umgewandelt. Daher wird `sum` oft zum Zählen der `True`-Werte in einem booleschen Array verwendet:" ] }, { "cell_type": "code", "execution_count": 1, "id": "69fd9f95", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "b8030f19", "metadata": {}, "outputs": [], "source": [ "data = np.random.randn(7, 3)" ] }, { "cell_type": "markdown", "id": "35a96a47", "metadata": {}, "source": [ "Anzahl der positiven Werte:" ] }, { "cell_type": "code", "execution_count": 3, "id": "28c9eba4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.int64(8)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(data > 0).sum()" ] }, { "cell_type": "markdown", "id": "8335f7e1", "metadata": {}, "source": [ "Anzahl der negativen Werte:" ] }, { "cell_type": "code", "execution_count": 4, "id": "8fcc6bc8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.int64(13)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(data < 0).sum()" ] }, { "cell_type": "markdown", "id": "8bcc5984", "metadata": {}, "source": [ "Es gibt zwei zusätzliche Methoden, `any` und `all`, die besonders für boolesche Arrays nützlich sind:\n", "\n", "* `any` prüft, ob ein oder mehrere Werte in einem Array wahr sind\n", "* `all` prüft, ob jeder Wert wahr ist" ] }, { "cell_type": "code", "execution_count": 5, "id": "99e05a71", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[False, False, False],\n", " [ True, True, False],\n", " [False, True, True],\n", " [ True, True, True],\n", " [False, False, False],\n", " [False, False, True],\n", " [False, True, True]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data2 = np.random.randn(7, 3)\n", "bools = data > data2\n", "\n", "bools" ] }, { "cell_type": "code", "execution_count": 6, "id": "22887677", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.True_" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bools.any()" ] }, { "cell_type": "code", "execution_count": 7, "id": "bc1b7a96", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.False_" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bools.all()" ] } ], "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 }