{ "cells": [ { "cell_type": "markdown", "id": "ae32f312", "metadata": {}, "source": [ "# Conditional logic as array operations – `where`\n", "\n", "The [numpy.where](https://numpy.org/doc/stable/reference/generated/numpy.where.html) function is a vectorised version of `if` and `else`." ] }, { "cell_type": "markdown", "id": "86aefffc", "metadata": {}, "source": [ "In the following example, we first create a Boolean array and two arrays with values:" ] }, { "cell_type": "code", "execution_count": 1, "id": "94a0f898", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "fcbbf600", "metadata": {}, "outputs": [], "source": [ "cond = ([False, True, False, True, False, False, False])\n", "data1 = np.random.randn(1, 7)\n", "data2 = np.random.randn(1, 7)" ] }, { "cell_type": "markdown", "id": "22e59173", "metadata": {}, "source": [ "Now we want to take the values from `data1` if the corresponding value in `cond` is `True` and otherwise take the value from `data2`. With Python’s `if-else`, this could look like this:" ] }, { "cell_type": "code", "execution_count": 3, "id": "f3da08da", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([-1.44855826, 1.36998598, -0.20317678, 1.34608124, 0.40381171,\n", " -0.53214436, -0.39467458])]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = [(x if c else y) for x, y, c in zip(data1, data2, cond)]\n", "\n", "result" ] }, { "cell_type": "markdown", "id": "8dd95e6b", "metadata": {}, "source": [ "However, this has the following two problems:\n", "\n", "* with large arrays the function will not be very fast\n", "* this will not work with multidimensional arrays" ] }, { "cell_type": "markdown", "id": "7540d25f", "metadata": {}, "source": [ "With `np.where` you can work around these problems in a single function call:" ] }, { "cell_type": "code", "execution_count": 4, "id": "62fd7e9d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1.44855826, 0.52391667, -0.20317678, 0.23328353, 0.40381171,\n", " -0.53214436, -0.39467458]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = np.where(cond, data1, data2)\n", "\n", "result" ] }, { "cell_type": "markdown", "id": "e00c5267", "metadata": {}, "source": [ "The second and third arguments of `np.where` do not have to be arrays; one or both can also be scalars. A typical use of `where` in data analysis is to create a new array of values based on another array. Suppose you have a matrix of randomly generated data and you want to make all the negative values positive values:" ] }, { "cell_type": "code", "execution_count": 5, "id": "4fbe852d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.09739726, 1.0954641 , 1.21257909, -0.06470122],\n", " [ 0.65963544, 1.23582335, 0.47142984, 1.10924854],\n", " [-0.11219385, -0.59830829, 0.1750536 , 1.22600517],\n", " [ 0.97477413, -0.5904872 , 0.26752476, 0.19260319]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = np.random.randn(4, 4)\n", "\n", "data" ] }, { "cell_type": "code", "execution_count": 6, "id": "aacc3846", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[False, False, False, True],\n", " [False, False, False, False],\n", " [ True, True, False, False],\n", " [False, True, False, False]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data < 0" ] }, { "cell_type": "code", "execution_count": 7, "id": "429ea702", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.09739726, 1.0954641 , 1.21257909, 0.06470122],\n", " [0.65963544, 1.23582335, 0.47142984, 1.10924854],\n", " [0.11219385, 0.59830829, 0.1750536 , 1.22600517],\n", " [0.97477413, 0.5904872 , 0.26752476, 0.19260319]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.where(data < 0, data * -1, data)" ] } ], "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 }