{ "cells": [ { "cell_type": "markdown", "id": "89206fb8", "metadata": {}, "source": [ "# Methods for Boolean arrays\n", "\n", "Boolean values have been converted to 1 (`True`) and 0 (`False`) in the previous methods. Therefore, `sum` is often used to count the `True` values in a Boolean array:" ] }, { "cell_type": "code", "execution_count": 1, "id": "72b3a506", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:28:03.170094Z", "iopub.status.busy": "2026-05-21T17:28:03.169688Z", "iopub.status.idle": "2026-05-21T17:28:03.204951Z", "shell.execute_reply": "2026-05-21T17:28:03.204603Z", "shell.execute_reply.started": "2026-05-21T17:28:03.170074Z" } }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "d7908101", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:28:03.205391Z", "iopub.status.busy": "2026-05-21T17:28:03.205280Z", "iopub.status.idle": "2026-05-21T17:28:03.214706Z", "shell.execute_reply": "2026-05-21T17:28:03.214441Z", "shell.execute_reply.started": "2026-05-21T17:28:03.205383Z" } }, "outputs": [], "source": [ "rng = np.random.default_rng()\n", "data = rng.normal(size=(7, 3))" ] }, { "cell_type": "markdown", "id": "42925e49", "metadata": {}, "source": [ "Number of positive values:" ] }, { "cell_type": "code", "execution_count": 3, "id": "afeb251f", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:28:03.215073Z", "iopub.status.busy": "2026-05-21T17:28:03.214984Z", "iopub.status.idle": "2026-05-21T17:28:03.218667Z", "shell.execute_reply": "2026-05-21T17:28:03.218340Z", "shell.execute_reply.started": "2026-05-21T17:28:03.215065Z" } }, "outputs": [ { "data": { "text/plain": [ "np.int64(12)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(data >= 0).sum()" ] }, { "cell_type": "markdown", "id": "c73241c0", "metadata": {}, "source": [ "Number of negative values:" ] }, { "cell_type": "code", "execution_count": 4, "id": "9f9dd3c4", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:28:03.219473Z", "iopub.status.busy": "2026-05-21T17:28:03.219350Z", "iopub.status.idle": "2026-05-21T17:28:03.221905Z", "shell.execute_reply": "2026-05-21T17:28:03.221634Z", "shell.execute_reply.started": "2026-05-21T17:28:03.219463Z" } }, "outputs": [ { "data": { "text/plain": [ "np.int64(9)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(data < 0).sum()" ] }, { "cell_type": "markdown", "id": "e417b114", "metadata": {}, "source": [ "There are two additional methods, `any` and `all`, which are particularly useful for Boolean arrays:\n", "\n", "* `any` checks whether one or more values in an array are true\n", "* `all` checks whether each value is true" ] }, { "cell_type": "code", "execution_count": 5, "id": "e6052cdb", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:28:03.222452Z", "iopub.status.busy": "2026-05-21T17:28:03.222304Z", "iopub.status.idle": "2026-05-21T17:28:03.224595Z", "shell.execute_reply": "2026-05-21T17:28:03.224365Z", "shell.execute_reply.started": "2026-05-21T17:28:03.222442Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[False, False, False],\n", " [False, True, True],\n", " [False, False, False],\n", " [False, True, False],\n", " [False, True, False],\n", " [ True, True, True],\n", " [ True, 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": "cf41bfea", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:28:03.226239Z", "iopub.status.busy": "2026-05-21T17:28:03.226153Z", "iopub.status.idle": "2026-05-21T17:28:03.228190Z", "shell.execute_reply": "2026-05-21T17:28:03.227989Z", "shell.execute_reply.started": "2026-05-21T17:28:03.226231Z" } }, "outputs": [ { "data": { "text/plain": [ "np.True_" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bools.any()" ] }, { "cell_type": "code", "execution_count": 7, "id": "26dc76d2", "metadata": { "execution": { "iopub.execute_input": "2026-05-21T17:28:03.228466Z", "iopub.status.busy": "2026-05-21T17:28:03.228410Z", "iopub.status.idle": "2026-05-21T17:28:03.230466Z", "shell.execute_reply": "2026-05-21T17:28:03.230246Z", "shell.execute_reply.started": "2026-05-21T17:28:03.228459Z" } }, "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 }