{ "cells": [ { "cell_type": "markdown", "id": "5572cc0a", "metadata": {}, "source": [ "--- \n", " \n", "\n", "

Department of Data Science

\n", "

Course: Tools and Techniques for Data Science

\n", "\n", "---\n", "

Instructor: Muhammad Arif Butt, Ph.D.

" ] }, { "cell_type": "markdown", "id": "04ba630a", "metadata": {}, "source": [ "

Lecture 3.3 (NumPy-03)

" ] }, { "cell_type": "markdown", "id": "0e54f511", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "a5a92293", "metadata": {}, "source": [ "# _Basic Operations on NumPy Arrays.ipynb_" ] }, { "cell_type": "markdown", "id": "3d4c9b2f", "metadata": {}, "source": [ " " ] }, { "cell_type": "code", "execution_count": null, "id": "9a04e870", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "7aeadcc5", "metadata": {}, "source": [ "# Learning agenda of this notebook\n", "- Operations on numPy arrays are done element-wise. This means that you don't explicitly have to write for-loops in order to do these operations!\n", "- A universal function, or ufunc, is a function that performs element-wise operations on data in ndarrays. You can think of them as fast vectorized wrappers for simple functions that take one or more scalar values and produce one or more scalar results.\n", "\n", "\n", " \n", " \n", "\n", "

















\n", "\n", "1. Scalar Math\n", " - On 1-D Arrays\n", " - On 2-D Arrays\n", "2. Arithmetic operations\n", " - On 1-D Arrays\n", " - On 2-D Arrays\n", "3. More Mathematical Operations\n", " - On 1-D Arrays\n", " - On 2-D Arrays\n", "4. Aggregate Functions\n", " - On 1-D Arrays\n", " - On 2-D Arrays\n", "5. Comparing NumPy Arrays\n", " - On 1-D Arrays\n", " - On 2-D Arrays\n", "6. Bonus: Searching 1-D and 2-D NumPy Arrays using `numpy.where()`" ] }, { "cell_type": "code", "execution_count": null, "id": "ad592c7e", "metadata": {}, "outputs": [], "source": [ "# To install this library in Jupyter notebook\n", "#import sys\n", "#!{sys.executable} -m pip install numpy" ] }, { "cell_type": "code", "execution_count": null, "id": "bca9f700", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "np.__version__ , np.__path__" ] }, { "cell_type": "code", "execution_count": null, "id": "5662cd04", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "0906e715", "metadata": {}, "source": [ "## 1. Scalar Math" ] }, { "cell_type": "markdown", "id": "26f8690c", "metadata": {}, "source": [ "### a. Scalar Math on 1-D Arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "08b13d56", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "# Create a 1-D array having 4 random integers from 1 to 9\n", "arr = np.random.randint(1,10, size=4)\n", "print(\"arr: \", arr)\n", "\n", "# After the operation a new `ndarray` is returned\n", "print(\"np.add(arr,2): \", arr + 2) # np.add(arr, 2))\n", "print(\"np.subtract(arr,2): \", arr - 2) # np.subtract(arr, 2))\n", "print(\"np.multiply(arr,2): \", arr * 2) # np.multiply(arr, 2))\n", "print(\"np.divide(arr,2): \", arr / 2) # np.divide(arr, 2))\n", "print(\"np.mod(arr,2): \", arr % 2) # np.mod(arr, 2))\n", "\n", "arr3 = arr + 9\n", "print(id(arr), id(arr3))" ] }, { "cell_type": "code", "execution_count": null, "id": "43f5d009", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "ef0533ab", "metadata": {}, "source": [ "### b. Scalar Math on 2-D Arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "a7206060", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "# Create a 2x2 matrix with random integers between 1 to 9\n", "matrix = np.random.randint(1,10, size=(2, 2))\n", "print(\"Original matrix:\\n\", matrix)\n", "\n", "# After the operation a new `ndarray` is returned\n", "print(\"np.add(matrix,2): \\n\", matrix + 2) # np.add(matrix, 2))\n", "print(\"np.subtract(matrix,2): \\n\", matrix - 2) # np.subtract(matrix, 2))\n", "print(\"np.multiply(matrix,2): \\n\", matrix * 2) # np.multiply(matrix, 2))\n", "print(\"np.divide(matrix,2): \\n\", matrix / 2) # np.divide(matrix, 2))\n", "print(\"np.mod(matrix,2): \\n\", matrix % 2) # np.mod(matrix, 2))" ] }, { "cell_type": "code", "execution_count": null, "id": "916ba2ee", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "f7900157", "metadata": {}, "source": [ "## 2. Arithmetic Operations" ] }, { "cell_type": "markdown", "id": "28f196d5", "metadata": {}, "source": [ "### a. Arithmetic Operations on 1-D Arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "30d3f30f", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "# Create two 1-D arrays each having 4 random integers from 1 to 9\n", "arr1 = np.random.randint(1,10, size=4)\n", "arr2 = np.random.randint(1,10, size=4)\n", "print(\"arr1: \", arr1)\n", "print(\"arr2: \", arr2)\n", "\n", "# After the operation a new `ndarray` is returned\n", "print(\"np.add(arr1, arr2) = \", arr1 + arr2) # np.add(arr1, arr2)\n", "print(\"np.subtract(arr1, arr2) = \", arr1 - arr2) # np.subtract(arr1, arr2)\n", "print(\"np.multiply(arr1, arr2) = \", arr1 * arr2) # np.multiply(arr1, arr2)\n", "print(\"np.divide(arr1, arr2) = \", arr1 / arr2) # np.divide(arr1, arr2)\n", "print(\"np.mod(arr1, arr2) = \", arr1 % arr2) # np.mod(arr1, arr2) # np.remainder(arr1, arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "a5b7b049", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "f39da597", "metadata": {}, "source": [ "### b. Arithmetic Operations on 2-D Arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "3cd42b3c", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "# Create a 2x2 matrix with random integers between 1 to 9\n", "matrix1 = np.random.randint(1,10, size=(2, 2))\n", "matrix2 = np.random.randint(1, 10, size=(2, 2))\n", "print(\"matrix1: \\n\", matrix1)\n", "print(\"matrix2: \\n\", matrix2)\n", "\n", "# After the operation a new `ndarray` is returned\n", "print(\"np.add(matrix1 , matrix2) = \\n\", matrix1 + matrix2) # np.add(matrix1 , matrix2)\n", "print(\"np.subtract(matrix1 , matrix2) = \\n\", matrix1 - matrix2) # np.subtract(matrix1 , matrix2)\n", "print(\"np.multiply(matrix1 , matrix2) = \\n\", matrix1 * matrix2) # np.multiply(matrix1 , matrix2)\n", "print(\"np.divide(matrix1 , matrix2) = \\n\", matrix1 / matrix2) # np.divide(matrix1 , matrix2)\n", "print(\"np.mod(matrix1 , matrix2) = \\n\", matrix1 % matrix2) # np.remainder(matrix1 , matrix2)" ] }, { "cell_type": "code", "execution_count": null, "id": "d2a244f9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "6af771f9", "metadata": {}, "source": [ "## 3. More Mathematical Operations" ] }, { "cell_type": "markdown", "id": "a625e92a", "metadata": {}, "source": [ "### a. Mathematical Operations on 1-D Array" ] }, { "cell_type": "code", "execution_count": null, "id": "cf755e36", "metadata": {}, "outputs": [], "source": [ "# Creating 1-D array of 5 elements of random floats between 1 and 10\n", "arr = np.random.rand(5)*10\n", "print(\"Original ndarray: \", arr)\n", "\n", "\n", "# After the operation a new `ndarray` is returned, which is a copy of the original array with the operation performed\n", "print(\"np.abs(arr) = \", np.abs(arr))\n", "print(\"np.square(arr) = \", np.square(arr))\n", "print(\"np.sqrt(arr) = \", np.sqrt(arr))\n", "print(\"np.cbrt(arr) = \", np.cbrt(arr))\n", "print(\"np.log(arr) = \", np.log(arr))\n", "print(\"np.log2(arr) = \", np.log2(arr))\n", "print(\"np.log10(arr) = \", np.log10(arr))\n", "print(\"np.isnan(arr) = \", np.isnan(arr))\n", "print(\"np.ceil(arr) = \", np.ceil(arr))\n", "print(\"np.floor(arr) = \", np.floor(arr))\n", "print(\"np.cumsum(arr) = \", np.cumsum(arr))" ] }, { "cell_type": "code", "execution_count": null, "id": "5193c0ec", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "51e8cef6", "metadata": {}, "source": [ "### b. Mathematical Operations on 2-D Array" ] }, { "cell_type": "code", "execution_count": null, "id": "71a1ae23", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "# Create a 2x2 matrix with random integers between 1 to 9\n", "matrix = np.random.randint(1,10, size=(2, 2))\n", "print(\"matrix: \\n\", matrix)\n", "\n", "# After the operation a new `ndarray` is returned, which is a copy of the original array with the operation performed\n", "print(\"np.square(matrix) = \\n\", np.square(matrix))\n", "print(\"np.sqrt(matrix) = \\n\", np.sqrt(matrix))\n", "print(\"np.log2(matrix) = \\n\", np.log2(matrix))\n", "print(\"np.isnan(matrix) = \\n\", np.isnan(matrix))\n", "print(\"np.ceil(matrix) = \\n\", np.ceil(matrix))\n", "print(\"np.floor(matrix) = \\n\", np.floor(matrix))\n", "print(\"np.cumsum(matrix) = \\n\", np.cumsum(matrix))" ] }, { "cell_type": "code", "execution_count": null, "id": "afdd4b87", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c5d411d3", "metadata": {}, "source": [ "## 4. Aggregate Functions" ] }, { "cell_type": "markdown", "id": "dde44c0c", "metadata": {}, "source": [ "### a. Aggregate Functions on 1-D Arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "1d8605be", "metadata": {}, "outputs": [], "source": [ "# Create a 1-D array having 5 random integers from 1 to 9\n", "arr = np.random.randint(1, 10, size=5)\n", "print(\"arr: \", arr)\n", "\n", "# After the operation a new `ndarray` is returned, which is a copy of the original array with the operation performed\n", "print(\"np.min(arr) = \", np.min(arr))\n", "print(\"np.max(arr) = \", np.max(arr))\n", "print(\"np.sum(arr) = \", np.sum(arr))\n", "print(\"np.prod(arr) = \", np.prod(arr))" ] }, { "cell_type": "code", "execution_count": null, "id": "cebfc963", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "351b8a6a", "metadata": {}, "outputs": [], "source": [ "# Create a 1-D array having 5 random integers from 1 to 9\n", "arr = np.random.randint(1, 10, size=5)\n", "print(\"arr: \", arr)\n", "\n", "#Arithmetic mean is the sum of elements along an axis divided by the number of elements. \n", "print(\"np.mean(arr) = \", np.mean(arr))\n", "\n", "# The np.average() function computes the weighted average, by multiplying of each array element by its weight\n", "# The np.average() is same as np.mean(), when weight is not specified\n", "print(\"np.average(arr) = \", np.average(arr))\n", "\n", "# Considering an array [1,2,3,4] and corresponding weights [4,3,2,1], the weighted average is calculated by \n", "# adding the product of the corresponding elements and dividing the sum by the sum of weights.\n", "# Weighted average = (1*4+2*3+3*2+4*1)/(4+3+2+1)\n", "\n", "arr1 = np.array([1,2,3,4])\n", "wts = np.array([4,3,2,1])\n", "print(\"\\narr1: \", arr1)\n", "print(\"wts: \", wts)\n", "print(\"np.average(arr1, weights=wts) = \", np.average(arr1, weights= wts))\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8a8c075d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "65a99fd8", "metadata": {}, "outputs": [], "source": [ "# Create a 1-D array having 10 random integers from 1 to 99\n", "arr = np.random.randint(1, 100, size=10)\n", "print(\"arr: \", arr)\n", "\n", "# Median is the value separating the higher half of a data sample from the lower half.\n", "print(\"np.median(arr) = \", np.median(arr))\n", "\n", "# Percentile is the value below which a given percentage of observations in a group of observations fall.\n", "# 50th percentile is the score at or below which 50% of the scores in the distribution may be found.\n", "print(\"np.percentile(arr,50) = \", np.percentile(arr,50))\n", "print(\"np.percentile(arr,0) = \", np.percentile(arr,0))\n", "print(\"np.percentile(arr,100) = \", np.percentile(arr,100))\n", "\n", "# The variance measures the average degree to which each point differs from the mean\n", "# Variance is calculated as the average of squared deviations from mean, i.e., mean((x - x.mean())**2).\n", "print(\"np.var(arr) = \", np.var(arr))\n", "\n", "\n", "# Standard deviation checks how spread out a group of numbers is from the mean\n", "# It is the square root of variance: sqrt(mean((x - x.mean())**2))\n", "print(\"np.std(arr) = \", np.std(arr))\n" ] }, { "cell_type": "code", "execution_count": null, "id": "39648ba7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c12623e8", "metadata": {}, "source": [ "### b. Aggregate Functions on 2-D Arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "4c71de42", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "# Create a 3x3 (2-D array) initialized with random integers from 1 to 9\n", "matrix = np.random.randint(1,10, size=(3, 3))\n", "print(\"matrix: \\n\", matrix)\n", "\n", "print(\"np.sum(matrix, axis=0) = \", np.sum(matrix, axis=0))\n", "print(\"np.sum(matrix, axis=1) = \", np.sum(matrix, axis=1))\n" ] }, { "cell_type": "code", "execution_count": null, "id": "83020163", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "cf45b9d3", "metadata": {}, "outputs": [], "source": [ "# Create a 3x3 (2-D array) initialized with random integers from 1 to 9\n", "matrix = np.random.randint(1,10, size=(3, 3))\n", "print(\"matrix: \\n\", matrix)\n", "\n", "print(\"np.min(matrix, axis=0) = \", np.min(matrix, axis=0))\n", "print(\"np.min(matrix, axis=1) = \", np.min(matrix, axis=1))" ] }, { "cell_type": "code", "execution_count": null, "id": "93165a2e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "ded4bbd3", "metadata": {}, "outputs": [], "source": [ "# Create a 3x3 (2-D array) initialized with random integers from 1 to 9\n", "matrix = np.random.randint(1,10, size=(3, 3))\n", "print(\"matrix: \\n\", matrix)\n", "\n", "\n", "print(\"np.mean(matrix, axis=0) = \", np.mean(matrix, axis=0))\n", "print(\"np.mean(matrix, axis=1) = \", np.mean(matrix, axis=1))" ] }, { "cell_type": "code", "execution_count": null, "id": "94152beb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "10b61422", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "1ed39e0c", "metadata": {}, "outputs": [], "source": [ "# Create a 3x3 (2-D array) initialized with random integers from 1 to 9\n", "matrix = np.random.randint(1,10, size=(3, 3))\n", "print(\"matrix: \\n\", matrix)\n", "\n", "# Median is the value separating the higher half of a data sample from the lower half.\n", "print(\"np.median(matrix, axis=0) = \", np.median(matrix, axis=0))\n", "print(\"np.median(matrix, axis=1) = \", np.median(matrix, axis=1))" ] }, { "cell_type": "markdown", "id": "8b4429f9", "metadata": {}, "source": [ "Please do practice calculating the percentile, variance and standard deviation along different axis of a 2-D array" ] }, { "cell_type": "code", "execution_count": null, "id": "dce99a00", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "32fde252", "metadata": {}, "source": [ "## 5. Comparing NumPy Arrays" ] }, { "cell_type": "markdown", "id": "624dfa93", "metadata": {}, "source": [ "### a. Comparing NumPy 1-D Arrays\n", "- Returns a Boolean numpy array containing True/False after doing element wise comparison" ] }, { "cell_type": "code", "execution_count": 1, "id": "6fc80b92", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1: [7 5 8 7]\n", "arr2: [2 3 3 4]\n", "np.equal(arr1, arr2) = [False False False False]\n", "np.not_equal(arr1, arr2) = [ True True True True]\n", "np.greater(arr1, arr2) = [ True True True True]\n", "np.greater_equal(arr1, arr2) = [ True True True True]\n", "np.less(arr1, arr2) = [False False False False]\n", "np.less_equal(arr1, arr2) = [False False False False]\n" ] } ], "source": [ "import numpy as np\n", "# Create two 1-D arrays each having 4 random integers from 1 to 9\n", "arr1 = np.random.randint(1,10, size=4)\n", "arr2 = np.random.randint(1,10, size=4)\n", "print(\"arr1: \", arr1)\n", "print(\"arr2: \", arr2)\n", "print(\"np.equal(arr1, arr2) = \", arr1 == arr2) # np.equal(arr1, arr2)\n", "print(\"np.not_equal(arr1, arr2) = \", arr1 != arr2) # np.not_equal(arr1, arr2)\n", "\n", "print(\"np.greater(arr1, arr2) = \", arr1 > arr2) # np.greater(arr1, arr2)\n", "print(\"np.greater_equal(arr1, arr2) = \", arr1 >= arr2) # np.greater_equal(arr1, arr2)\n", "\n", "print(\"np.less(arr1, arr2) = \", arr1 < arr2) # np.less(arr1, arr2)\n", "print(\"np.less_equal(arr1, arr2) = \", arr1 <= arr2) # np.less_equal(arr1, arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "c20a9ed4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "d03fa4fb", "metadata": {}, "source": [ "### b. Comparing NumPy 2-D Arrays" ] }, { "cell_type": "code", "execution_count": 2, "id": "b65058ea", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "matrix1: \n", " [[4 6]\n", " [5 6]]\n", "matrix2: \n", " [[7 5]\n", " [9 4]]\n", "np.equal(matrix1, matrix2) = \n", " [[False False]\n", " [False False]]\n", "np.not_equal(matrix1, matrix2) = \n", " [[ True True]\n", " [ True True]]\n", "np.greater(matrix1, matrix2) = \n", " [[False True]\n", " [False True]]\n", "np.greater_equal(matrix1, matrix2) = \n", " [[False True]\n", " [False True]]\n", "np.less(matrix1, matrix2) = \n", " [[ True False]\n", " [ True False]]\n", "np.less_equal(matrix1, matrix2) = \n", " [[ True False]\n", " [ True False]]\n" ] } ], "source": [ "import numpy as np\n", "# Create a 2x2 matrix with random integers between 1 to 9\n", "matrix1 = np.random.randint(1,10, size=(2, 2))\n", "matrix2 = np.random.randint(1, 10, size=(2, 2))\n", "print(\"matrix1: \\n\", matrix1)\n", "print(\"matrix2: \\n\", matrix2)\n", "\n", "\n", "\n", "print(\"np.equal(matrix1, matrix2) = \\n\", matrix1 == matrix2) \n", "print(\"np.not_equal(matrix1, matrix2) = \\n\", matrix1 != matrix2) \n", "\n", "print(\"np.greater(matrix1, matrix2) = \\n\", matrix1 > matrix2) \n", "print(\"np.greater_equal(matrix1, matrix2) = \\n\",matrix1 >= matrix2) \n", "\n", "print(\"np.less(matrix1, matrix2) = \\n\", matrix1 < matrix2) \n", "print(\"np.less_equal(matrix1, matrix2) = \\n\", matrix1 <= matrix2) " ] }, { "cell_type": "code", "execution_count": null, "id": "1159671e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "4ed923c8", "metadata": {}, "source": [ "## 6. Searching NumPy Arrays using `numpy.where()`" ] }, { "cell_type": "markdown", "id": "7a15d751", "metadata": {}, "source": [ "### a. Searching NumPy 1-D Arrays" ] }, { "cell_type": "markdown", "id": "bfcf9f08", "metadata": {}, "source": [ "- The `numpy.where()` method is used to search NumPy arrays for the index values of any element which matches the condition passed as a parameter to the function.\n", "```\n", "numpy.where(condition, [x, y])\n", "```\n", " - condition : When True, yield x, otherwise yield y. If x and y are not given just return the value of the array\n", " - Returns: ndarray or tuple of ndarrays. If both x and y are specified, the output array contains elements of x where condition is True, and elements from y elsewhere." ] }, { "cell_type": "code", "execution_count": 4, "id": "0a209cc5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array([3, 5, 6]),)\n" ] } ], "source": [ "import numpy as np\n", "arr = np.array([1, 2, 3, 4, 5, 4, 4, 9, 2, 3, 8])\n", "t1 = np.where(arr == 4)\n", "print(t1)" ] }, { "cell_type": "code", "execution_count": null, "id": "1b4fa4ae", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "24865ae5", "metadata": {}, "source": [ "Note: The result is a tuple of 1-D arrays having a single tuple containing the index" ] }, { "cell_type": "code", "execution_count": 3, "id": "e1db54c3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array([1, 6]),)\n" ] } ], "source": [ "import numpy as np\n", "arr = np.array([1, 0, 3, 9, 5, 2, 0, 8])\n", "t1 = np.where(arr == 0)\n", "print(t1)" ] }, { "cell_type": "code", "execution_count": null, "id": "7c6fe56b", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", "t1 = np.where(arr%2 == 0)\n", "print(t1)" ] }, { "cell_type": "code", "execution_count": null, "id": "c96f2079", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "e5071b86", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 5, "id": "4f82105f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False True False True False True False True]\n" ] } ], "source": [ "import numpy as np\n", "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", "t1 = np.where(arr%2 == 0, True, False)\n", "print(t1)" ] }, { "cell_type": "markdown", "id": "f7f2c65c", "metadata": {}, "source": [ "Note: The result is a simple 1-D array, when you use the x and y arguments to `np.where()`" ] }, { "cell_type": "code", "execution_count": null, "id": "336bb2e8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "57f89640", "metadata": {}, "source": [ "### b. Searching NumPy 2-D Arrays" ] }, { "cell_type": "markdown", "id": "b63342d5", "metadata": {}, "source": [ "- The `numpy.where()` method is used to search NumPy arrays for the index values of any element which matches the condition passed as a parameter to the function.\n", "```\n", "numpy.where(condition, [x, y])\n", "```\n", " - condition : When True, yield x, otherwise yield y. If x and y are not given just return the value of the array\n", " - Returns: ndarray or tuple of ndarrays. If both x and y are specified, the output array contains elements of x where condition is True, and elements from y elsewhere." ] }, { "cell_type": "code", "execution_count": 6, "id": "fde6ed1e", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "matrix: \n", " [[1 2 3]\n", " [4 5 4]\n", " [4 9 2]\n", " [3 8 6]]\n", "(array([1, 1, 2]), array([0, 2, 0]))\n" ] } ], "source": [ "import numpy as np\n", "matrix = np.array([[1, 2, 3], [4, 5, 4], [4, 9, 2], [3, 8, 6]])\n", "print(\"matrix: \\n\", matrix)\n", "t1 = np.where(matrix == 4)\n", "print(t1)" ] }, { "cell_type": "markdown", "id": "1ae934c6", "metadata": {}, "source": [ "**Note:** The result is a tuple of 1-D arrays having two tuples. The condition is true for three elements at indices `(1,0)`, `(1,2)` and `(2,0)`" ] }, { "cell_type": "code", "execution_count": null, "id": "07e93d93", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 7, "id": "24c55c5c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "matrix: \n", " [[1 2 0]\n", " [4 5 0]\n", " [4 9 2]\n", " [3 0 6]]\n", "(array([0, 1, 3]), array([2, 2, 1]))\n" ] } ], "source": [ "import numpy as np\n", "matrix = np.array([[1, 2, 0], [4, 5, 0], [4, 9, 2], [3, 0, 6]])\n", "print(\"matrix: \\n\", matrix)\n", "t1 = np.where(matrix == 0)\n", "print(t1)" ] }, { "cell_type": "markdown", "id": "58c391db", "metadata": {}, "source": [ "**Note:** The result is a tuple of 1-D arrays having two tuples. The condition is true for three elements at indices:\n", "`(0,2)`, `(1,2)` and `(3,1)`" ] }, { "cell_type": "code", "execution_count": null, "id": "80ba08eb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d6ebe838", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 8, "id": "e901346d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "matrix: \n", " [[1 2 3]\n", " [5 7 3]\n", " [4 9 1]\n", " [3 5 3]]\n", "(array([0, 2]), array([1, 0]))\n" ] } ], "source": [ "import numpy as np\n", "matrix = np.array([[1, 2, 3], [5, 7, 3], [4, 9, 1], [3, 5, 3]])\n", "print(\"matrix: \\n\", matrix)\n", "t1 = np.where(matrix%2 == 0)\n", "print(t1)" ] }, { "cell_type": "markdown", "id": "4358b8be", "metadata": {}, "source": [ "**Note:** The result is a tuple of 1-D arrays having two tuples. The condition is true for two elements at indices:\n", "`(0,2)` and `(1,0)`" ] }, { "cell_type": "code", "execution_count": null, "id": "9134a9aa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "1449c662", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 9, "id": "86e4c330", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "matrix: \n", " [[1 2 3]\n", " [5 7 3]\n", " [4 9 1]\n", " [3 5 3]]\n", "[[False True False]\n", " [False False False]\n", " [ True False False]\n", " [False False False]]\n" ] } ], "source": [ "import numpy as np\n", "matrix = np.array([[1, 2, 3], [5, 7, 3], [4, 9, 1], [3, 5, 3]])\n", "print(\"matrix: \\n\", matrix)\n", "t1 = np.where(matrix%2 == 0, True, False)\n", "print(t1)" ] }, { "cell_type": "markdown", "id": "b9dad9b8", "metadata": {}, "source": [ "**Note:** The result is a simple 2-D array, when you use the `x` and `y` arguments to `np.where()`" ] }, { "cell_type": "code", "execution_count": null, "id": "0d58aba7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 12, "id": "3ac7c142", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array([0, 1, 1, 2, 3]), array([3, 0, 2, 1, 0]))\n" ] } ], "source": [ "import numpy as np\n", "arr = np.array([[1, 2, 3, 4], [4, 5, 4, 2], [8, 4, 9, 2], [4, 3, 8, 6]])\n", "t1 = np.where(arr == 4)\n", "print(t1)" ] }, { "cell_type": "code", "execution_count": null, "id": "ec3a306d", "metadata": {}, "outputs": [], "source": [] } ], "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }