{ "cells": [ { "cell_type": "markdown", "id": "b5b40627", "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": "7e05a8a1", "metadata": {}, "source": [ "

Lecture 3.5 (NumPy-05)

" ] }, { "cell_type": "markdown", "id": "bc9dbf68", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "2f6acdc3", "metadata": {}, "source": [ "# _Broadcasting, Reshaping, Sorting and Iterating NumPy Arrays.ipynb_" ] }, { "cell_type": "markdown", "id": "29a16b1f", "metadata": {}, "source": [ " " ] }, { "cell_type": "code", "execution_count": null, "id": "330b046a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c10ce742", "metadata": {}, "source": [ "# Learning agenda of this notebook\n", "1. Broadcasting NumPy Arrays\n", "2. Reshaping NumPy Arrays\n", " - Use `shape` attribute (in-place operation)\n", " - Use `np.reshape()` method (shallow copy)\n", " - Use `np.resize()` method (deep copy)\n", " - Use `ndarray.transpose()` method (shallow copy)\n", " - Use `np.swapaxes()`method (shallow copy)\n", " - Use `np.flatten()` method (deep copy)\n", "3. Sorting Arrays using `np.sort()` Method\n", "4. Iterating NumPy Arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "932c9898", "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": 1, "id": "98474a05", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('1.19.5',\n", " ['/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy'])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "np.__version__ , np.__path__" ] }, { "cell_type": "code", "execution_count": null, "id": "20c8c749", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c3ea9ffe", "metadata": {}, "source": [ "## 1. Broadcasting numPy Arrays\n", "- Numpy arrays also support **broadcasting**, allowing arithmetic operations between two arrays with different numbers of dimensions but compatible shapes. \n", "- Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes.\n", "- Two dimensions are compatible when\n", " - they are equal, or\n", " - one of them is 1" ] }, { "cell_type": "markdown", "id": "605189d4", "metadata": {}, "source": [ ">**Review of Arithmetic operations with numPy arrays of same shape:**" ] }, { "cell_type": "code", "execution_count": 2, "id": "25af4615", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1: [11 11 32 29 25]\n", "arr2: [7 9 1 7 4]\n", "arr1 + arr2 = [18 20 33 36 29]\n", "arr1 / arr2 = [ 1.57142857 1.22222222 32. 4.14285714 6.25 ]\n", "arr1 // arr2 = [ 1 1 32 4 6]\n", "arr1 ** arr2 = [ 19487171 2357947691 32 17249876309 390625]\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,50, size=5)\n", "arr2 = np.random.randint(1,10, size=5)\n", "print(\"arr1: \", arr1)\n", "print(\"arr2: \", arr2)\n", "\n", "# After the operation a new `ndarray` is returned\n", "print(\"arr1 + arr2 = \", arr1 + arr2) \n", "print(\"arr1 / arr2 = \", arr1 / arr2) \n", "print(\"arr1 // arr2 = \", arr1 // arr2) \n", "print(\"arr1 ** arr2 = \", arr1 ** arr2) " ] }, { "cell_type": "code", "execution_count": null, "id": "bd188d7c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9f70a279", "metadata": {}, "source": [ "### a. Arithmetic of 1-Dimensional Array with a Scalar Value" ] }, { "cell_type": "code", "execution_count": 3, "id": "2dc0cc6f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1: [1 2 3 4]\n", "arr1.shape: (4,)\n", "a: 2\n" ] } ], "source": [ "# Consider adding a scalar value 'a' to a 1-D numPy array\n", "arr1 = np.array([1, 2, 3, 4])\n", "print(\"arr1: \", arr1)\n", "print(\"arr1.shape: \", arr1.shape)\n", "a = 2\n", "print(\"a: \", a)" ] }, { "cell_type": "code", "execution_count": 4, "id": "fde47c32", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr2: \n", " [3 4 5 6]\n" ] } ], "source": [ "# The scalar value is replicated to match the shape of arr1 before the operation\n", "# [2 2 2 2] \n", "\n", "arr2 = arr1 + a \n", "print(\"arr2: \\n\", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "4dacde3b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "8dc9db14", "metadata": {}, "source": [ "### b. Arithmetic of 2-Dimensional Array with a Scalar Value" ] }, { "cell_type": "code", "execution_count": 5, "id": "254d9dff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1:\n", " [[1 2 3]\n", " [1 2 3]]\n", "arr1.shape: (2, 3)\n", "a: 2\n" ] } ], "source": [ "# Consider adding a scalar value 'a' to a 2-D numPy array\n", "arr1 = np.array([[1, 2, 3], [1, 2, 3]])\n", "print(\"arr1:\\n\", arr1)\n", "print(\"arr1.shape: \", arr1.shape)\n", "a = 2\n", "print(\"a: \", a)" ] }, { "cell_type": "code", "execution_count": 6, "id": "4c024488", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr2: \n", " [[3 4 5]\n", " [3 4 5]]\n" ] } ], "source": [ "# The scalar value is replicated to match the shape of arr1 before the operation\n", "# 2 2 2 \n", "# 2 2 2 \n", "\n", "arr2 = arr1 + a \n", "print(\"arr2: \\n\", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "50ec8fc2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "a278f789", "metadata": {}, "source": [ "### c. Arithmetic of 1-Dimensional Array with a 2-Dimensional Array" ] }, { "cell_type": "markdown", "id": "12b38b4c", "metadata": {}, "source": [ "**Example 1:** Consider adding a 2-D array (3x4) to a 1-D array with 4 values" ] }, { "cell_type": "code", "execution_count": 9, "id": "c70b2618", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1:\n", " [[1 2 3 4]\n", " [3 4 5 6]\n", " [2 7 8 9]]\n", "arr1.shape: (3, 4)\n", "arr2:\n", " [4 2 3]\n", "arr2.shape: (3,)\n" ] } ], "source": [ "arr1 = np.array([[1, 2, 3, 4], [3, 4, 5, 6], [2, 7, 8, 9]])\n", "arr2 = np.array([4, 2, 3, 2])\n", "print(\"arr1:\\n\", arr1)\n", "print(\"arr1.shape: \", arr1.shape)\n", "print(\"arr2:\\n\", arr2)\n", "print(\"arr2.shape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": 8, "id": "5a995ec2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr3: \n", " [[ 5 4 6 9]\n", " [ 7 6 8 11]\n", " [ 6 9 11 14]]\n" ] } ], "source": [ "# The only row of arr2 is replicated twice before the operation\n", "# 4 2 3 5\n", "# 4 2 3 5\n", "# 4 2 3 5\n", "\n", "arr3 = arr1 + arr2 \n", "\n", "print(\"arr3: \\n\", arr3)" ] }, { "cell_type": "markdown", "id": "b9e71d83", "metadata": {}, "source": [ "**Example 2:** Consider adding a 2-D array (4x2) to a 1-D array with 2 values" ] }, { "cell_type": "code", "execution_count": null, "id": "8785ba17", "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])\n", "print(\"arr1:\\n\", arr1)\n", "print(\"arr1.shape: \", arr1.shape)\n", "\n", "arr2 = np.array([4, 5])\n", "print(\"arr2:\\n\", arr2)\n", "print(\"arr2.shape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "a9e6b566", "metadata": {}, "outputs": [], "source": [ "# The only row of arr2 is replicated three times before the operation\n", "# 4 5\n", "# 4 5\n", "# 4 5\n", "# 4 5\n", "arr3 = arr2 + arr1 \n", "\n", "print(\"arr3: \\n\", arr3)" ] }, { "cell_type": "markdown", "id": "c9c5a54f", "metadata": {}, "source": [ "### d. Arithmetic of two 2-Dimensional Arrays" ] }, { "cell_type": "markdown", "id": "df0990af", "metadata": {}, "source": [ "**Example 1:** Consider adding elements of a 2-D array (2x3) with another 2-D array (3x1)" ] }, { "cell_type": "code", "execution_count": null, "id": "10440087", "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([[5, 3, 2],[3, 4, 5], [7, 1, 4]])\n", "print(\"arr1:\\n\", arr1)\n", "print(\"arr1.shape: \", arr1.shape)\n", "\n", "arr2 = np.array([[100], [200], [300]])\n", "print(\"arr2:\\n\", arr2)\n", "print(\"arr2.shape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "182ab99a", "metadata": {}, "outputs": [], "source": [ "# The only column of arr2 is replicated twice before the operation\n", "# 100 100 100\n", "# 200 200 300\n", "# 300 300 300\n", "\n", "arr3 = arr1 + arr2 \n", "\n", "print(\"arr3: \\n\", arr3)" ] }, { "cell_type": "markdown", "id": "c651f0ef", "metadata": {}, "source": [ "**Points to Ponder in Arithmetic and Broadcasting:**\n", ">- Arithmetic between elements of two numPy arrays works fine if both the arrays are of same shape.\n", ">- Arithmetic between a numPy array and a scalar value works fine due to broadcasting.\n", ">- Arithmetic between elements of two numPy arrays with different dimensions will work, if and only if the array with smaller dimension can be replicated to match the shape of other array (as in above examples)" ] }, { "cell_type": "markdown", "id": "1188f96d", "metadata": {}, "source": [ "**Example 1: Broadcast Error**" ] }, { "cell_type": "code", "execution_count": 10, "id": "69240490", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1:\n", " [[1 2 3]\n", " [1 2 3]]\n", "arr1.shape: (2, 3)\n", "arr2:\n", " [1 2]\n", "arr2.shape: (2,)\n" ] } ], "source": [ "# Consider adding a 2-D array (2x3) to a 1-D array with 2 values\n", "arr1 = np.array([[1, 2, 3], [1, 2, 3]])\n", "arr2 = np.array([1,2])\n", "print(\"arr1:\\n\", arr1)\n", "print(\"arr1.shape: \", arr1.shape)\n", "print(\"arr2:\\n\", arr2)\n", "print(\"arr2.shape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": 11, "id": "e35edb35", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "operands could not be broadcast together with shapes (2,3) (2,) ", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/var/folders/1t/g3ylw8h50cjdqmk5d6jh1qmm0000gn/T/ipykernel_7691/3649234492.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# (last dimension of `arr1` i.e., 3 does not match with the first dimension of `arr2` i.e., 2)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# therefore, broadcasting is unsuccessful and will flag an error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0marr3\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marr1\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0marr2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"arr3: \\n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marr3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3) (2,) " ] } ], "source": [ "# Since arr2 cannot be replicated to match the shape of arr1,\n", "# (last dimension of `arr1` i.e., 3 does not match with the first dimension of `arr2` i.e., 2)\n", "# therefore, broadcasting is unsuccessful and will flag an error\n", "arr3 = arr1 + arr2 \n", "\n", "print(\"arr3: \\n\", arr3)" ] }, { "cell_type": "code", "execution_count": null, "id": "e6f69aba", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "d824b817", "metadata": {}, "source": [ "**Example 2: Broadcast Error**" ] }, { "cell_type": "code", "execution_count": 15, "id": "1fc34edb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1:\n", " [[1 2]\n", " [3 4]\n", " [5 6]\n", " [7 8]]\n", "arr1.shape: (4, 2)\n", "arr2:\n", " [[4]\n", " [5]\n", " [6]\n", " [5]]\n", "arr2.shape: (4, 1)\n" ] } ], "source": [ "# Consider adding a 2-D array (2x3) to a 1-D array with 2 values\n", "arr1 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])\n", "print(\"arr1:\\n\", arr1)\n", "print(\"arr1.shape: \", arr1.shape)\n", "\n", "\n", "arr2 = np.array([[4], [5], [6], [5]])\n", "print(\"arr2:\\n\", arr2)\n", "print(\"arr2.shape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": 16, "id": "67d7e845", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr3: \n", " [[ 5 6]\n", " [ 8 9]\n", " [11 12]\n", " [12 13]]\n" ] } ], "source": [ "# Since arr2 cannot be replicated to match the shape of arr1,\n", "# (last dimension of `arr1` i.e., 2 does not match with the first dimension of `arr2` i.e., 3)\n", "# therefore, broadcasting is unsuccessful and will flag an error\n", "try:\n", " arr3 = arr1 + arr2 \n", " print(\"arr3: \\n\", arr3)\n", "except ValueError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "id": "5e1e61f0", "metadata": {}, "source": [ "**Example 3: Broadcast Error**" ] }, { "cell_type": "code", "execution_count": null, "id": "1e24eb32", "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([[5, 3, 2],[3, 4, 5], [7, 1, 4]])\n", "print(\"arr1:\\n\", arr1)\n", "print(\"arr1.shape: \", arr1.shape)\n", "\n", "arr2 = np.array([[100], [200]])\n", "print(\"arr2:\\n\", arr2)\n", "print(\"arr2.shape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "cd4f687f", "metadata": {}, "outputs": [], "source": [ "# Since arr2 cannot be replicated to match the shape of arr1,\n", "# (last dimension of `arr1` i.e., 3 does not match with the first dimension of `arr2` i.e., 2)\n", "# therefore, broadcasting is unsuccessful and will flag an error\n", "try:\n", " arr3 = arr1 + arr2 \n", " print(\"arr3: \\n\", arr3)\n", "except ValueError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "id": "7494392a", "metadata": {}, "source": [ "## 2. Reshaping Arrays\n", "- Reshaping numpy array simply means changing the shape of the given array, shape basically tells the number of elements and dimension of array.\n", "- By reshaping an array we can add or remove dimensions or change number of elements in each dimension.\n", "- There are different ways that reshape numPy arrays:\n", " - Changing the `shape` attribute (in-place operation)\n", " - Use `np.reshape()` method (shallow copy)\n", " - Use `np.resize()` method (deep copy)\n", " - Use `ndarray.transpose()` method (shallow copy)\n", " - Use `np.swapaxes()`method (shallow copy)\n", " - Use `ndarray.flatten()` method (deep copy)" ] }, { "cell_type": "code", "execution_count": null, "id": "aa4460ed", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a3979bd7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "32ab0cc0", "metadata": {}, "source": [ "### a. Change the `np.shape` Attribute\n", "- Changing the shape of an `ndarray` is as simple as setting its `shape` attribute. However, the array's size must remain the same.\n", "- No new array is created, rather the change of shape occurs in-place." ] }, { "cell_type": "code", "execution_count": 17, "id": "9ddec588", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n", "Dimensions: 1\n", "Shape: (24,)\n", "Strides: (8,)\n" ] } ], "source": [ "import numpy as np\n", "arr1 = np.arange(24)\n", "print(\"arr1:\", arr1)\n", "print(\"Dimensions:\", arr1.ndim)\n", "print(\"Shape:\", arr1.shape)\n", "print(\"Strides:\", arr1.strides)" ] }, { "cell_type": "code", "execution_count": 18, "id": "692e83f4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1: \n", " [[ 0 1 2 3]\n", " [ 4 5 6 7]\n", " [ 8 9 10 11]\n", " [12 13 14 15]\n", " [16 17 18 19]\n", " [20 21 22 23]]\n", "Dimensions: 2\n", "Shape: (6, 4)\n", "Strides: (32, 8)\n" ] } ], "source": [ "#Changing the shape attribute (array size must remain same)\n", "arr1.shape = (6, 4)\n", "print(\"arr1: \\n\", arr1)\n", "print(\"Dimensions:\", arr1.ndim)\n", "print(\"Shape:\", arr1.shape)\n", "print(\"Strides:\", arr1.strides)" ] }, { "cell_type": "code", "execution_count": null, "id": "76c0369f", "metadata": {}, "outputs": [], "source": [ "#Changing the shape attribute (array size must remain same)\n", "arr1.shape = (2, 4, 3)\n", "print(\"arr1: \\n\", arr1)\n", "print(\"Dimensions:\", arr1.ndim)\n", "print(\"Shape:\", arr1.shape)\n", "print(\"Strides:\", arr1.strides)" ] }, { "cell_type": "markdown", "id": "49b38500", "metadata": {}, "source": [ "### b. Use the `np.reshape()` Method\n", " \n", "\n", "```\n", "np.reshape(arr, newshape)\n", "```\n", "\n", "- The `np.reshape()` method takes the input array, then a tuple that defines the shape of the new array and returns a new array, which shares the same memory as the original array. You can think it as shallow copy in Python, where if you change the data in one array, the corresponding data in the other array is also modified." ] }, { "cell_type": "markdown", "id": "07da1408", "metadata": {}, "source": [ "**Example 1:** Reshaping from 1-D numPy Arrays to 2-D numPy Arrays" ] }, { "cell_type": "code", "execution_count": 19, "id": "0e3c6adc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original Array: [1 2 3 4 5 6] \n", "Shape: (6,)\n" ] } ], "source": [ "arr1 = np.array([1, 2, 3, 4, 5, 6])\n", "print(\"Original Array: \", arr1, \"\\nShape: \", arr1.shape)" ] }, { "cell_type": "code", "execution_count": 22, "id": "046869e6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Reshaped Array: \n", " [[1 2 3]\n", " [4 5 6]] \n", "Shape: (2, 3)\n" ] } ], "source": [ "# Changing the dimension of array using reshape()\n", "arr2 = np.reshape(arr1, (2, 3))\n", "print(\"Reshaped Array: \\n\", arr2, \"\\nShape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "84df047d", "metadata": {}, "outputs": [], "source": [ "# make change in one of the arrays, the change is reflected in both\n", "arr2[0][0] = 99\n", "print(\"Original Array: \", arr1)\n", "print(\"Reshaped Array: \\n\", arr2)\n", "print(\"id(arr1): \", id(arr1))\n", "print(\"id(arr2): \",id(arr2))" ] }, { "cell_type": "markdown", "id": "dffe7c85", "metadata": {}, "source": [ "**Example 2:** Reshaping from 1-D numPy Arrays to 3-D numPy Arrays" ] }, { "cell_type": "code", "execution_count": null, "id": "b6ae4c1f", "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])\n", "print(\"Original Array: \", arr1, \"\\nShape: \", arr1.shape)\n", "\n", "arr2 = np.reshape(arr1, (2, 2, 3))\n", "print(\"\\nReshaped Array: \\n\", arr2, \"\\nShape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "0ae55f3e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "45b75ac2", "metadata": {}, "source": [ "**Example 3:** Flattening Arrays. You can convert an array of an unknown dimension to a 1D array using `np.reshape(-1) `" ] }, { "cell_type": "code", "execution_count": null, "id": "ae8a7890", "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([[1, 2, 3], [6, 7, 8], [4, 5, 6], [11, 14, 10]])\n", "print(\"Original Array: \\n\", arr1, \"\\nShape: \", arr1.shape)\n", "\n", "arr2 = np.reshape(arr1, (-1))\n", "print(\"\\nReshaped Array: \\n\", arr2, \"\\nShape: \", arr2.shape)" ] }, { "cell_type": "markdown", "id": "2bbb0874", "metadata": {}, "source": [ "**Example 4:** Reshaping an array back to its original dimensions. If you applied the `np.reshape()` method to an array and you want to get the original shape of the array back, you can call the reshape method on that array again." ] }, { "cell_type": "code", "execution_count": null, "id": "9c9b67fb", "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([[1, 2, 3], [6, 7, 8], [4, 5, 6], [11, 14, 10]])\n", "print(\"Original Array: \\n\", arr1, \"\\nShape: \", arr1.shape)\n", "\n", "arr2 = np.reshape(arr1, (2, 6))\n", "print(\"\\nReshaped Array: \\n\", arr2, \"\\nShape: \", arr2.shape)\n", "\n", "# covert the array into original shape again\n", "arr3 = np.reshape(arr2, (4,3))\n", "print(\"\\nReshaped to Original Shape: \\n\", arr3, \"\\nShape: \", arr3.shape)" ] }, { "cell_type": "markdown", "id": "94ec61b2", "metadata": {}, "source": [ "**Example 5:** You can reshape to any shape, the only requirement is that the total elements in both the arrays should be same" ] }, { "cell_type": "code", "execution_count": null, "id": "87a7f813", "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n", "arr2 = np.reshape(arr1, (3, 2))\n", "arr2" ] }, { "cell_type": "markdown", "id": "8b2e1617", "metadata": {}, "source": [ "### c. Use `np.resize()` Method\n", "```\n", "np.resize(arr, newshape)\n", "```\n", "- Like `np.reshape()` method, the `np.resize()` method also takes the input array and a tuple that defines the shape of the array, with one main difference and that is:\n", " - If the `newshape` argument mismatch with the size of `arr`, it do not raise error. The new array is formed from the data in the old array, repeated if necessary to fill out the required number of elements. " ] }, { "cell_type": "markdown", "id": "a4d0d83c", "metadata": {}, "source": [ "**Example 1:** Resizing from 1-D numPy Arrays to 2-D numPy Arrays. The new array doesn’t share the same memory with the original array. The data change in one array is not mapped to the other." ] }, { "cell_type": "code", "execution_count": 23, "id": "0ab3c202", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original Array: [1 2 3 4 5 6] \n", "Shape: (6,)\n" ] } ], "source": [ "arr1 = np.array([1, 2, 3, 4, 5, 6])\n", "print(\"Original Array: \", arr1, \"\\nShape: \", arr1.shape)" ] }, { "cell_type": "code", "execution_count": 25, "id": "b99e6eb1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Resized Array: \n", " [[1 2 3]\n", " [4 5 6]] \n", "Shape: (2, 3)\n" ] } ], "source": [ "# Changing the dimension of array using reshape()\n", "arr2 = np.resize(arr1, (2, 3))\n", "print(\"Resized Array: \\n\", arr2, \"\\nShape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": 26, "id": "7fad9f62", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original Array: [1 2 3 4 5 6]\n", "Resized Array: \n", " [[99 2 3]\n", " [ 4 5 6]]\n", "id(arr1): 140539858671936\n", "id(arr2): 140539859132704\n" ] } ], "source": [ "# make change in one of the arrays, the change is NOT reflected in both\n", "arr2[0][0] = 99\n", "print(\"Original Array: \", arr1)\n", "print(\"Resized Array: \\n\", arr2)\n", "print(\"id(arr1): \", id(arr1))\n", "print(\"id(arr2): \",id(arr2))" ] }, { "cell_type": "markdown", "id": "e89ceefa", "metadata": {}, "source": [ "**Example 2:** The `np.resize()` method allows you to resize an array to a new array having larger size than the original array. In this scenario, it fills the remaining array with repeated copies of original array elements" ] }, { "cell_type": "code", "execution_count": null, "id": "03c6399a", "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([1, 2, 3, 4, 5, 6])\n", "print(\"Original Array: \", arr1, \"\\nShape: \", arr1.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "c6269d25", "metadata": {}, "outputs": [], "source": [ "arr2 = np.resize(arr1, (4,4))\n", "print(\"\\nResized Array: \\n\", arr2, \"\\nShape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "124f0657", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "548fd15e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "4b3e3b28", "metadata": {}, "source": [ "**Example 3:** The `np.resize()` method allows you to resize an array to a new array having smaller size than the original array." ] }, { "cell_type": "code", "execution_count": null, "id": "63e4c2c7", "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", "print(\"Original Array: \", arr1, \"\\nShape: \", arr1.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "45c79d08", "metadata": {}, "outputs": [], "source": [ "arr2 = np.resize(arr1, (3, 2))\n", "print(\"\\nResized Array: \\n\", arr2, \"\\nShape: \", arr2.shape)" ] }, { "cell_type": "markdown", "id": "5f8e1f7c", "metadata": {}, "source": [ "### d. The `ndarray.transpose()` Method\n", "```\n", "ndarray.transpose()\n", "```\n", "- It has no impact on 1-D array\n", "- For a 2-D array, this is a standard matrix transpose.\n", "- For an n-D array, if axes are given, their order indicates how the axes are permuted\n", "- Returns a view of the array with axes transposed, so this is an in-place operation." ] }, { "cell_type": "code", "execution_count": null, "id": "5e407a2d", "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", "print(\"Original Array: \\n\", arr1, \"\\nShape: \", arr1.shape)\n", "\n", "#Reshape array using transpose method\n", "arr2 = arr1.transpose()\n", "print(\"\\nTransposed array: \\n\", arr2, \"\\nShape: \", arr2.shape)\n", "\n", "## make change in one of the arrays, the change is reflected in both\n", "arr2[1][1] = 99\n", "print(\"\\nOriginal Array: \\n\", arr1)\n", "print(\"Transposed Array: \\n\", arr2)" ] }, { "cell_type": "markdown", "id": "093dc697", "metadata": {}, "source": [ "### e. The `np.swapaxes()` Method\n", "- The `np.swapaxes()` method is used to interchange two axes of an array.\n", "```\n", "np.swapaxes(arr, axis1, axis2)\n", "```\n", " - `arr`: Input array whose axes are to be swapped\n", " - `axis1`: First axis\n", " - `axis2`: Second axis\n", "\n", "- For NumPy >= 1.10.0, if `arr` is an ndarray, then a view of `arr` is returned." ] }, { "cell_type": "markdown", "id": "eb183fcd", "metadata": {}, "source": [ "**Example 1:** Swapping axes of a 2-D array" ] }, { "cell_type": "code", "execution_count": null, "id": "76e10f42", "metadata": {}, "outputs": [], "source": [ "arr1 = np.arange(8).reshape(2,4) \n", "print(\"Original Array: \\n\", arr1, \"\\nShape: \", arr1.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "2d196d85", "metadata": {}, "outputs": [], "source": [ "#Reshape array using swapaxes() method\n", "arr2 = np.swapaxes(arr1,0, 1)\n", "print(\"\\nNew array: \\n\", arr2, \"\\nShape: \", arr2.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "c1980410", "metadata": {}, "outputs": [], "source": [ "## make change in one of the arrays, the change is reflected in both\n", "arr2[1][1] = 99\n", "print(\"\\nOriginal Array: \\n\", arr1)\n", "print(\"\\nNew Array: \\n\", arr2)" ] }, { "cell_type": "markdown", "id": "01515efd", "metadata": {}, "source": [ "**Example 2:** Swapping axes of a 3-D array" ] }, { "cell_type": "code", "execution_count": null, "id": "32c3c7a8", "metadata": {}, "outputs": [], "source": [ "arr1 = np.arange(8).reshape(2,2,2) \n", "print(\"Original Array: \\n\", arr1, \"\\nShape: \", arr1.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "f156b03b", "metadata": {}, "outputs": [], "source": [ "#Reshape array using swapaxes() method\n", "arr2 = np.swapaxes(arr1,1, 2)\n", "print(\"\\nNew array: \\n\", arr2, \"\\nShape: \", arr2.shape)" ] }, { "cell_type": "markdown", "id": "d514bdfc", "metadata": {}, "source": [ "### f. The `ndarray.flatten()` Method\n", "- The `ndarry.flatten()` method is used to flatten a Multi-Dimensional array/matrix to one dimension. \n", "```\n", "ndarray.flatten(order= 'C')\n", "```\n", " - Default order is 'C’, means to flatten in row-major order. \n", " - You can pass ‘F’ (FORTRAN) means to flatten in column-major.\n", "- Returns a copy of the array, flattened to one dimension." ] }, { "cell_type": "code", "execution_count": 27, "id": "feaddafa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original Array: \n", " [[ 0 1 2]\n", " [ 3 4 5]\n", " [ 6 7 8]\n", " [ 9 10 11]] \n", "Dimensions: 2\n" ] } ], "source": [ "arr1 = np.arange(12).reshape(4,3) \n", "print(\"Original Array: \\n\", arr1, \"\\nDimensions: \", arr1.ndim)" ] }, { "cell_type": "code", "execution_count": 28, "id": "ccfaf430", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Flattened array in row major order \n", " [ 0 1 2 3 4 5 6 7 8 9 10 11] \n", "Dimensions: 1\n", "\n", "Flattened array in cloumn major order \n", " [ 0 3 6 9 1 4 7 10 2 5 8 11] \n", "Dimensions: 1\n" ] } ], "source": [ "# flatten the array in row-major order\n", "arr2 = arr1.flatten(order = 'C')\n", "print(\"\\nFlattened array in row major order \\n\", arr2, \"\\nDimensions: \", arr2.ndim)\n", "\n", "# flatten the array in column-major order\n", "arr3 = arr1.flatten(order = 'F')\n", "print(\"\\nFlattened array in cloumn major order \\n\", arr3, \"\\nDimensions: \", arr3.ndim)" ] }, { "cell_type": "code", "execution_count": null, "id": "3dfcc37a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3eaaeab6", "metadata": {}, "source": [ "## 3. Sorting Arrays using `np.sort()` Method\n", "- The `np.sort()` method returns a sorted copy of an array.\n", "- If axis is not specified, values can be of any shape and will be flattened before use\n", "```\n", "np.sort(arr1, axis=-1, kind=None)\n", "```\n", " - `arr` : Array to be sorted.\n", " - `axis` : Axis along which we need array to be started. The default is -1, which sorts along the last axis. 0 stands for sorting along first axis. If metioned None, the array is flattened before sorting.\n", " - `kind` : default is 'quicksort', others can be 'mergesort', 'heapsort'" ] }, { "cell_type": "markdown", "id": "81262fcf", "metadata": {}, "source": [ "### a. Sorting a 1-D Array" ] }, { "cell_type": "code", "execution_count": 35, "id": "d9a041db", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [60 89 67 98 72]\n" ] } ], "source": [ "import numpy as np\n", "arr1 = np.random.randint(low = 1, high = 100, size = 5)\n", "print(\"arr1 = \", arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "deef6f22", "metadata": {}, "outputs": [], "source": [ "arr2 = np.sort(arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "markdown", "id": "791d8537", "metadata": {}, "source": [ "### b. Sorting a 2-D Array" ] }, { "cell_type": "markdown", "id": "14e9a50f", "metadata": {}, "source": [ "**Example 1:** Sorting a 2-D array with `axis=None`, array is flattened before sorting" ] }, { "cell_type": "code", "execution_count": 29, "id": "ff2f4f38", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1: \n", " [[61 36 91]\n", " [73 97 9]\n", " [44 70 59]]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 100, size = (3,3))\n", "print(\"arr1: \\n\", arr1)" ] }, { "cell_type": "code", "execution_count": 30, "id": "4e887b13", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Sorting along axis=None: \n", " [ 9 36 44 59 61 70 73 91 97]\n" ] } ], "source": [ "arr2 = np.sort(arr1, axis = None) \n", "print (\"\\nSorting along axis=None: \\n\", arr2)" ] }, { "cell_type": "markdown", "id": "666addfa", "metadata": {}, "source": [ "**Example 2:** Sorting a 2-D array with `axis=0`, vertical axis, top to bottom" ] }, { "cell_type": "code", "execution_count": 31, "id": "d271af63", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[7 5 6]\n", " [8 3 6]\n", " [3 2 7]]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (3,3))\n", "print(\"arr1 = \\n\", arr1)" ] }, { "cell_type": "code", "execution_count": 32, "id": "de11180e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Sorting along axis=0: \n", " [[3 2 6]\n", " [7 3 6]\n", " [8 5 7]]\n" ] } ], "source": [ "arr2 = np.sort(arr1, axis = 0) \n", "print (\"\\nSorting along axis=0: \\n\", arr2) " ] }, { "cell_type": "markdown", "id": "1315698b", "metadata": {}, "source": [ "**Example 3:** Sorting a 2-D array with `axis=1`, horizontal axis, left to right" ] }, { "cell_type": "code", "execution_count": 33, "id": "4a1c96be", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[6 3 9]\n", " [3 5 4]\n", " [7 4 8]]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (3,3))\n", "print(\"arr1 = \\n\", arr1)" ] }, { "cell_type": "code", "execution_count": 34, "id": "e469a9ba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Sorting along axis=1: \n", " [[3 6 9]\n", " [3 4 5]\n", " [4 7 8]]\n" ] } ], "source": [ "arr2 = np.sort(arr1, axis = 1) \n", "print (\"\\nSorting along axis=1: \\n\", arr2) " ] }, { "cell_type": "markdown", "id": "337a26a6", "metadata": {}, "source": [ "## 4. Iterating numPy Arrays\n", "- Iterating over `ndarrays` is very similar to iterating over regular python arrays. \n", "- Remember, iterating over multidimensional arrays is done with respect to the first axis." ] }, { "cell_type": "markdown", "id": "cc2536b3", "metadata": {}, "source": [ "**Example 1:** Iterating over 1-D numPy array" ] }, { "cell_type": "code", "execution_count": null, "id": "ac0b0562", "metadata": {}, "outputs": [], "source": [ "arr1 = np.arange(12)\n", "arr1" ] }, { "cell_type": "code", "execution_count": null, "id": "ef4e70d1", "metadata": {}, "outputs": [], "source": [ "for value in arr1:\n", " print(value, end=' ')" ] }, { "cell_type": "code", "execution_count": null, "id": "8066021b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "7d5ccb09", "metadata": {}, "source": [ "**Example 2:** Iterating over 2-D numPy array is done w.r.t first axis, i.e., zero axis (column wise). In simple words in the first iteration you will get the first row, in second iteration you will get the 2nd row and so on..." ] }, { "cell_type": "code", "execution_count": null, "id": "56eb5662", "metadata": {}, "outputs": [], "source": [ "arr1 = np.arange(12).reshape(4, 3)\n", "print(\"arr1: \\n\",arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "d3e7a573", "metadata": {}, "outputs": [], "source": [ "for zero_axis in arr1:\n", " print(\"Iteration:\")\n", " print(zero_axis)" ] }, { "cell_type": "markdown", "id": "bd19a3b8", "metadata": {}, "source": [ "**Example 3:** Iterating over 2-D numPy array" ] }, { "cell_type": "code", "execution_count": null, "id": "e44d5358", "metadata": {}, "outputs": [], "source": [ "arr1 = np.arange(8).reshape(2, 4)\n", "print(\"arr1: \\n\",arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "1ea61da8", "metadata": {}, "outputs": [], "source": [ "for zero_axis in arr1:\n", " print(\"Iteration:\")\n", " print(zero_axis)" ] }, { "cell_type": "code", "execution_count": null, "id": "eaa270d8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "52c61e01", "metadata": {}, "source": [ "**Example 4:** Iterating over 3-D numPy array is done w.r.t first axis, i.e., zero axis (level wise). In simple words in the first iteration you will get the first row, in second iteration you will get the 2nd row and so on..." ] }, { "cell_type": "code", "execution_count": null, "id": "74c55b11", "metadata": {}, "outputs": [], "source": [ "arr1 = np.arange(24).reshape(2, 3, 4)\n", "print(\"arr1: \\n\",arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "b758ed71", "metadata": {}, "outputs": [], "source": [ "for zero_axis in arr1:\n", " print(\"Iteration:\")\n", " print(zero_axis)" ] }, { "cell_type": "markdown", "id": "e20436b2", "metadata": {}, "source": [ "**Example 5:** If you want to iterate on *all* elements in the `ndarray`, simply iterate over the `flat` attribute:" ] }, { "cell_type": "code", "execution_count": null, "id": "4a59aefd", "metadata": {}, "outputs": [], "source": [ "arr1 = np.arange(12).reshape(3, 4)\n", "print(\"arr1: \\n\",arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "69c8b5b7", "metadata": {}, "outputs": [], "source": [ "for i in arr1.flat:\n", " print(i, end=' ')" ] }, { "cell_type": "code", "execution_count": null, "id": "2ce2845f", "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 }