{ "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": { "execution": { "iopub.execute_input": "2026-05-21T17:25:40.055393Z", "iopub.status.busy": "2026-05-21T17:25:40.055098Z", "iopub.status.idle": "2026-05-21T17:25:40.090561Z", "shell.execute_reply": "2026-05-21T17:25:40.090253Z", "shell.execute_reply.started": "2026-05-21T17:25:40.055368Z" } }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "b8030f19", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:25:40.091118Z", "iopub.status.busy": "2026-05-21T17:25:40.090963Z", "iopub.status.idle": "2026-05-21T17:25:40.103097Z", "shell.execute_reply": "2026-05-21T17:25:40.102773Z", "shell.execute_reply.started": "2026-05-21T17:25:40.091106Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.2892129 , -0.67574399, -1.18853755],\n", " [ 0.30108742, 0.27464408, 0.05025632],\n", " [ 0.43357058, 0.88041496, -1.39839326],\n", " [-2.83019669, 0.34271817, -0.46611059],\n", " [ 1.49941927, 0.57624576, 0.00614989],\n", " [-2.65594888, 1.63339514, -0.26199767],\n", " [-1.31868958, 1.97079266, -0.25957685]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rng = np.random.default_rng()\n", "data = rng.normal(size=(7, 3))\n", "data" ] }, { "cell_type": "markdown", "id": "35a96a47", "metadata": {}, "source": [ "Anzahl der positiven Werte:" ] }, { "cell_type": "code", "execution_count": 3, "id": "28c9eba4", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:25:40.103635Z", "iopub.status.busy": "2026-05-21T17:25:40.103528Z", "iopub.status.idle": "2026-05-21T17:25:40.106046Z", "shell.execute_reply": "2026-05-21T17:25:40.105781Z", "shell.execute_reply.started": "2026-05-21T17:25:40.103627Z" } }, "outputs": [ { "data": { "text/plain": [ "np.int64(12)" ] }, "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": { "execution": { "iopub.execute_input": "2026-05-21T17:25:40.106497Z", "iopub.status.busy": "2026-05-21T17:25:40.106376Z", "iopub.status.idle": "2026-05-21T17:25:40.108729Z", "shell.execute_reply": "2026-05-21T17:25:40.108436Z", "shell.execute_reply.started": "2026-05-21T17:25:40.106485Z" } }, "outputs": [ { "data": { "text/plain": [ "np.int64(9)" ] }, "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": { "execution": { "iopub.execute_input": "2026-05-21T17:25:40.109371Z", "iopub.status.busy": "2026-05-21T17:25:40.109263Z", "iopub.status.idle": "2026-05-21T17:25:40.112103Z", "shell.execute_reply": "2026-05-21T17:25:40.111800Z", "shell.execute_reply.started": "2026-05-21T17:25:40.109362Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[ True, False, False],\n", " [ True, True, False],\n", " [ True, True, False],\n", " [False, True, False],\n", " [False, True, True],\n", " [False, False, False],\n", " [False, True, True]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data2 = rng.normal(size=(7, 3))\n", "bools = data > data2\n", "\n", "bools" ] }, { "cell_type": "code", "execution_count": 6, "id": "22887677", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:25:40.113563Z", "iopub.status.busy": "2026-05-21T17:25:40.113466Z", "iopub.status.idle": "2026-05-21T17:25:40.115864Z", "shell.execute_reply": "2026-05-21T17:25:40.115449Z", "shell.execute_reply.started": "2026-05-21T17:25:40.113555Z" } }, "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": { "execution": { "iopub.execute_input": "2026-05-21T17:25:40.116404Z", "iopub.status.busy": "2026-05-21T17:25:40.116290Z", "iopub.status.idle": "2026-05-21T17:25:40.118730Z", "shell.execute_reply": "2026-05-21T17:25:40.118372Z", "shell.execute_reply.started": "2026-05-21T17:25:40.116390Z" } }, "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 }