{ "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": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "d7908101", "metadata": {}, "outputs": [], "source": [ "data = np.random.randn(7, 3)" ] }, { "cell_type": "markdown", "id": "42925e49", "metadata": {}, "source": [ "Number of positive values:" ] }, { "cell_type": "code", "execution_count": 3, "id": "afeb251f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.int64(7)" ] }, "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": {}, "outputs": [ { "data": { "text/plain": [ "np.int64(14)" ] }, "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": {}, "outputs": [ { "data": { "text/plain": [ "array([[ True, False, True],\n", " [ True, False, False],\n", " [False, True, False],\n", " [False, False, True],\n", " [False, False, True],\n", " [ True, False, False],\n", " [ True, True, False]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data2 = np.random.randn(7, 3)\n", "\n", "bools = data > data2\n", "\n", "bools" ] }, { "cell_type": "code", "execution_count": 6, "id": "cf41bfea", "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": "26dc76d2", "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 }