diff --git a/where/README.md b/where/README.md new file mode 100644 index 0000000000..4f6888c851 --- /dev/null +++ b/where/README.md @@ -0,0 +1,3 @@ +# How to Use Conditional Expressions With NumPy `where()` + +This Jupyter Notebook file contains all code and question solutions used in the Real Python tutorial [How to Use Conditional Expressions With NumPy `where()`](https://realpython.com/numpy-where-conditional-expressions/). diff --git a/where/all_code.ipynb b/where/all_code.ipynb new file mode 100644 index 0000000000..295d6900f0 --- /dev/null +++ b/where/all_code.ipynb @@ -0,0 +1,628 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "39189e01-024b-4d21-a2a6-d4377b6419ec", + "metadata": {}, + "source": [ + "**How to Write Conditional Expressions With NumPy where()**" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "de2949dd-4940-48b9-87df-f8186f39b5a9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[3.1688358 , 3.9091694 , 1.66405549, 3.61976783],\n", + " [7.33400434, 3.25797286, 9.65148913, 0.76115911],\n", + " [2.71053173, 6.02410179, 7.46355805, 1.30949485]])" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import numpy as np\n", + "\n", + "test_array = np.array(\n", + " [\n", + " [3.1688358, 3.9091694, 1.66405549, -3.61976783],\n", + " [7.33400434, -3.25797286, -9.65148913, -0.76115911],\n", + " [2.71053173, -6.02410179, 7.46355805, 1.30949485],\n", + " ]\n", + ")\n", + "\n", + "np.where(test_array < 0, test_array * -1, test_array)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "beaacd16-b3a1-471a-bf3b-8b8a7fac22a2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[3.1688358 , 3.9091694 , 1.66405549, 3.61976783],\n", + " [7.33400434, 3.25797286, 9.65148913, 0.76115911],\n", + " [2.71053173, 6.02410179, 7.46355805, 1.30949485]])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.abs(test_array)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "c4da3b48-bb08-46f1-a936-704bcfa84b52", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[False, False, False, True],\n", + " [False, True, True, True],\n", + " [False, True, False, False]])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "test_array < 0" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "deedb822-3268-45da-aa2c-0be09deaa142", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[-0.68506937, 5.66604672, 0.58421751, 4.8946752 ],\n", + " [-4.2481913 , -1.13216087, 0.63353423, 2.63621566],\n", + " [-1.11812408, 7.15968405, -7.5696163 , -2.37413593]])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "test_array = np.random.uniform(low=-10, high=10, size=(3, 4))\n", + "test_array" + ] + }, + { + "cell_type": "markdown", + "id": "83ccd413-7729-4bef-ae76-a91be907e3df", + "metadata": {}, + "source": [ + "**How to Use Multiple Conditional Expressions**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c558731-51fd-4c11-9887-0a8ca18519ee", + "metadata": {}, + "outputs": [], + "source": [ + "# Warning - this code will fail\n", + "\n", + "import numpy as np\n", + "\n", + "test_array = np.array(\n", + " [\n", + " [3.1688358, 3.9091694, 1.66405549, -3.61976783],\n", + " [7.33400434, -3.25797286, -9.65148913, -0.76115911],\n", + " [2.71053173, -6.02410179, 7.46355805, 1.30949485],\n", + " ]\n", + ")\n", + "\n", + "np.where(\n", + " (test_array > -2) and (test_array < 3),\n", + " test_array * -1,\n", + " test_array,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "43e5d159-d5bc-43b2-b331-f94928c74120", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ True, True, True, False],\n", + " [ True, False, False, True],\n", + " [ True, False, True, True]])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "test_array > -2" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c827e3a6-ea51-419b-a6ba-211a8f15ff4f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[False, False, True, True],\n", + " [False, True, True, True],\n", + " [ True, True, False, True]])" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "test_array < 3" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d2b77e1c-a094-4ccc-afca-16f5f3a0f64e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[False, False, True, False],\n", + " [False, False, False, True],\n", + " [ True, False, False, True]])" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(test_array > -2) & (test_array < 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "51612ed4-a948-4e19-8fcd-d9dd48423c02", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 3.1688358 , 3.9091694 , -1.66405549, -3.61976783],\n", + " [ 7.33400434, -3.25797286, -9.65148913, 0.76115911],\n", + " [-2.71053173, -6.02410179, 7.46355805, -1.30949485]])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.where(\n", + " (test_array > -2) & (test_array < 3),\n", + " test_array * -1,\n", + " test_array,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "9cf73369-c343-4d4e-a88c-167204e0f908", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[-3.1688358 , -3.9091694 , 1.66405549, 3.61976783],\n", + " [-7.33400434, 3.25797286, 9.65148913, -0.76115911],\n", + " [ 2.71053173, 6.02410179, -7.46355805, 1.30949485]])" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.where(\n", + " (test_array <= -2) | (test_array >= 3),\n", + " test_array * -1,\n", + " test_array,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "939304ad-1c06-4298-8288-75fa30b6d912", + "metadata": {}, + "source": [ + "**Question 1 Solutions**" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "939b8962-8ff3-4ba0-9d10-4baf066f8d5a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[9, 9, 9, 9],\n", + " [9, 9, 9, 9],\n", + " [9, 9, 9, 1],\n", + " [9, 3, 9, 5],\n", + " [9, 7, 9, 9]])" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Solution 1_1\n", + "\n", + "question_1 = np.arange(\n", + " -10,\n", + " 10,\n", + ").reshape(5, 4)\n", + "\n", + "np.where(\n", + " (question_1 < 0) | (question_1 % 2 == 0),\n", + " 9,\n", + " question_1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9dc34378-9c7f-4d15-bf72-48d48f321895", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[-10, 81, -8, 49],\n", + " [ -6, 25, -4, 9],\n", + " [ -2, 1, 0, 1],\n", + " [ 2, 3, 4, 5],\n", + " [ 6, 7, 8, 9]])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Solution 1_2\n", + "\n", + "np.where(\n", + " (question_1 < 0) & (question_1 % 2 != 0),\n", + " np.square(question_1),\n", + " question_1,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "142fd909-0fae-4a25-8cbb-f5d14f227727", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[-11, -10, -9, -8],\n", + " [ -7, -6, -5, -4],\n", + " [ -3, -2, -1, -10],\n", + " [ 1, 2, -10, -10],\n", + " [-10, 6, 7, 8]])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Solution 1_3\n", + "\n", + "np.where(\n", + " ((question_1 > 3) & (question_1 < 7)) | (question_1 == 1),\n", + " -10,\n", + " question_1 - 1,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "efd139d0-613d-4b0e-be7a-da42a92ee8f9", + "metadata": {}, + "source": [ + "**How to Use Array Broadcasting in Conditional Expressions**" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "2e7e9ce8-1354-4f8b-8298-e815341f135b", + "metadata": {}, + "outputs": [], + "source": [ + "booking_data = np.array(\n", + " [\n", + " [np.nan, np.nan, 1],\n", + " [1, 1, np.nan],\n", + " [1, np.nan, 1],\n", + " [1, 1, 1],\n", + " ]\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "330db591-1015-4942-853d-47ac654d8e3e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4, 3)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "booking_data.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "60206972-ed39-450c-94ce-17394355e963", + "metadata": {}, + "outputs": [], + "source": [ + "meal_prices = np.array([5.1, 8.2, 20.3]).reshape(1, 3)\n", + "no_charge = 0" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "ac7480dc-1562-4738-a72b-db397dc4ec72", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0. , 0. , 20.3],\n", + " [ 5.1, 8.2, 0. ],\n", + " [ 5.1, 0. , 20.3],\n", + " [ 5.1, 8.2, 20.3]])" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "booking_prices = np.where(booking_data == 1, meal_prices, no_charge)\n", + "booking_prices" + ] + }, + { + "cell_type": "markdown", + "id": "531840c1-a752-4d8c-b194-14c7bccd39ae", + "metadata": {}, + "source": [ + "**Question 2 Solutions**" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "6d192256-457d-42f8-b4b0-4897158fdb3b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([['LOW', 'LOW', 'LOW', 'LOW'],\n", + " ['LOW', 'LOW', 'LOW', 'HIGH'],\n", + " ['HIGH', 'HIGH', 'HIGH', 'HIGH']], dtype=' 6, high, low)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "eb725a3e-3c0b-43f3-aa57-6fe5bdcbb4c9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([['LOW', 'LOW', 'LOW', 'LOW'],\n", + " ['LOW', 'LOW', 'LOW', 'HIGH'],\n", + " ['HIGH', 'HIGH', 'HIGH', 'HIGH']], dtype=' 6, high, low)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "51aebc35-c9f2-481f-890e-ce17f4b6f574", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([['LOW', 'LOW', 'LOW', 'LOW'],\n", + " ['LOW', 'LOW', 'LOW', 'HIGH'],\n", + " ['HIGH', 'HIGH', 'HIGH', 'HIGH']], dtype=' 6, high, low)" + ] + }, + { + "cell_type": "markdown", + "id": "3bff96b5-4322-4833-8527-c0d6819e9320", + "metadata": {}, + "source": [ + "**How Not to Use np.where() - A Final Quirk**" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "bbfc4cc2-c483-4f27-be47-6769e0fdfaa1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(array([0, 1, 2]), array([0, 1, 2]))" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import numpy as np\n", + "\n", + "mostly_zeroes = np.array([[9, 0, 0], [0, 8, 0], [0, 0, 7]])\n", + "np.where(mostly_zeroes != 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "a92ad38b-3cc2-4bea-999a-e9a43a8444aa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(array([0, 1, 2]), array([0, 1, 2]))" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.nonzero(mostly_zeroes)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/where/requirements.txt b/where/requirements.txt new file mode 100644 index 0000000000..4aa499620a --- /dev/null +++ b/where/requirements.txt @@ -0,0 +1 @@ +numpy==2.1.0