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

Lecture 3.6 (NumPy-06)

" ] }, { "cell_type": "markdown", "id": "64ba925a", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "fa6dcb19", "metadata": {}, "source": [ "# _Manipulating Array Elements.ipynb_" ] }, { "cell_type": "markdown", "id": "c97f0f67", "metadata": {}, "source": [ " \n", "\n", "# Learning agenda of this notebook\n", "\n", "1. Updating existing values of NumPy array elements\n", "2. Append new elements to a NumPy array using np.append()\n", "3. Insert new elements in a NumPy array using np.insert()\n", "4. Delete elements of a NumPy array using np.delete()\n", "5. Alias vs Shallow Copy vs Deep Copy" ] }, { "cell_type": "code", "execution_count": null, "id": "eaf623d0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5f9a8b93", "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": "6fa14827", "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": "a55cc9b4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9800e96a", "metadata": {}, "source": [ "## 1. Updating Existing Values of Numpy Array Elements" ] }, { "cell_type": "markdown", "id": "2c75b1aa", "metadata": {}, "source": [ "### a. 1-D Arrays" ] }, { "cell_type": "code", "execution_count": 2, "id": "c22d47e9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original Array \n", " [21 64 42 91 22 79 40 84 91 92]\n", "Updated Array \n", " [ 21 64 333 91 22 79 40 84 91 777]\n" ] } ], "source": [ "arr = np.random.randint(low = 1, high = 100, size = 10)\n", "print(\"Original Array \\n\", arr)\n", "arr[2] = 333\n", "arr[-1] = 777\n", "print(\"Updated Array \\n\", arr)" ] }, { "cell_type": "markdown", "id": "2bd1f603", "metadata": {}, "source": [ "### b. 2-D Arrays" ] }, { "cell_type": "code", "execution_count": 3, "id": "3e3b0f21", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original Array \n", " [[7 4 7 1]\n", " [3 9 2 3]\n", " [7 4 8 5]\n", " [4 5 5 9]]\n", "Updated Array \n", " [[ 7 77 7 1]\n", " [ 3 9 66 3]\n", " [ 7 4 8 22]\n", " [ 4 5 5 9]]\n" ] } ], "source": [ "# Creating 2-D array of size 4x4 of int type b/w interval 1 to 9\n", "arr = np.random.randint(low = 1, high = 10, size = (4, 4))\n", "print(\"Original Array \\n\", arr)\n", "arr[0][1] = 77\n", "arr[1][2] = 66\n", "arr[2][-1] = 22\n", "print(\"Updated Array \\n\", arr)" ] }, { "cell_type": "code", "execution_count": null, "id": "ec4398ea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "73186f77", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a22f0d64", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "edf452b4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c7b21ead", "metadata": {}, "source": [ "## 2. Append New Elements to Numpy Arrays\n", "- The `np.append()` method allows us to insert new values at the end of a NumPy array.\n", "- The method always returns a copy of the existing numpy array with the values appended to the given axis.\n", "```\n", "np.append(arr, values, axis=None)\n", "```\n", "- Where,\n", " - `arr` is the array in which we want to append\n", " - `values` must be of the correct shape (the same shape as `arr` excluding `axis`)\n", " - If `axis` is not specified, both `arr` and `values` are flattened before use.\n", " - If `axis` is specified, then `values` must be of the correct shape (the same shape as `arr` excluding `axis`)\n", "- The original array remains as such, as it does not occur in-place." ] }, { "cell_type": "markdown", "id": "c9b7fa54", "metadata": {}, "source": [ "### a. Appending Elements in 1-D Arrays" ] }, { "cell_type": "code", "execution_count": 4, "id": "22b89811", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [27 43 66 95 39 38 42 7 25 37]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 100, size = 10)\n", "print(\"arr1 = \", arr1)" ] }, { "cell_type": "code", "execution_count": 5, "id": "b966077f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "After append:\n", "arr1 = [27 43 66 95 39 38 42 7 25 37]\n", "arr2 = [ 27 43 66 95 39 38 42 7 25 37 101 202 303]\n" ] } ], "source": [ "# You can add a scalar value or a list of values at the end of a 1-D array\n", "arr2 = np.append(arr1, [101, 202,303])\n", "print(\"After append:\")\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "f809838c", "metadata": {}, "outputs": [], "source": [ "print(id(arr1))\n", "print(id(arr2))" ] }, { "cell_type": "code", "execution_count": null, "id": "f29ee507", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "c42b712a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "196785fd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "b71d3c60", "metadata": {}, "source": [ "### b. Appending Elements in 2-D Arrays" ] }, { "cell_type": "markdown", "id": "94d9cf4a", "metadata": {}, "source": [ "**Example:** In case of 2-D Arrays if `axis` is not mentioned both `arr` and `values` are flattened before use" ] }, { "cell_type": "code", "execution_count": 6, "id": "9fdcd017", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[3 3 1]\n", " [7 7 3]\n", " [6 8 7]]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (3,3))\n", "print(\"arr1 = \\n\", arr1)" ] }, { "cell_type": "code", "execution_count": 7, "id": "dfdb6a95", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "After append:\n", "arr1 = \n", " [[3 3 1]\n", " [7 7 3]\n", " [6 8 7]]\n", "arr2 = [ 3 3 1 7 7 3 6 8 7 101 202 303 404 505]\n" ] } ], "source": [ "# If the axis is not mentioned, values can be of any shape and both `arr` and `values` are flattened before use.\n", "arr2 = np.append(arr1, [101, 202,303, 404, 505])\n", "print(\"After append:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "302c7f71", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "137dc887", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "c5de2401", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "8c44d2ef", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "130e285f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "28824ef2", "metadata": {}, "source": [ "**Example:** Appending a Row to a 2-D array (`axis=0`)" ] }, { "cell_type": "code", "execution_count": 8, "id": "469af8c2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[2 7 2]\n", " [8 7 3]\n", " [4 1 9]\n", " [9 8 1]]\n", "shape: (4, 3)\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (4,3))\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"shape: \", arr1.shape)" ] }, { "cell_type": "code", "execution_count": 9, "id": "43aea3c8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "After append:\n", "arr1 = \n", " [[2 7 2]\n", " [8 7 3]\n", " [4 1 9]\n", " [9 8 1]]\n", "arr2 = \n", " [[ 2 7 2]\n", " [ 8 7 3]\n", " [ 4 1 9]\n", " [ 9 8 1]\n", " [101 202 303]]\n" ] } ], "source": [ "# For appending at axis 0, the values argument must the same shape as `arr` excluding `axis`\n", "# so the values should be a row vector, and in this case of shape (1,3), having 1 row and 3 columns\n", "arr2 = np.append(arr1, [[101, 202,303]], axis=0)\n", "print(\"After append:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "d8b37d3f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b385e069", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "8d04630e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d820ca37", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "154a8a0a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "1e3ef38e", "metadata": {}, "source": [ "**Example:** Appending a Column to a 2-D array (`axis=1`)" ] }, { "cell_type": "code", "execution_count": 10, "id": "70c4b2dc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[4 5 6]\n", " [8 3 5]\n", " [4 6 9]\n", " [4 2 7]]\n", "shape: (4, 3)\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (4,3))\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"shape: \", arr1.shape)" ] }, { "cell_type": "code", "execution_count": 11, "id": "4d32417b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "After append:\n", "arr1 = \n", " [[4 5 6]\n", " [8 3 5]\n", " [4 6 9]\n", " [4 2 7]]\n", "arr2 = \n", " [[ 4 5 6 101]\n", " [ 8 3 5 202]\n", " [ 4 6 9 303]\n", " [ 4 2 7 404]]\n" ] } ], "source": [ "# For appending at axis 1, the values argument must the same shape as `arr` excluding `axis`\n", "# so the values should be a column vector of shape (4,1), having 4 rows and 1 column\n", "arr2 = np.append(arr1, [[101], [202], [303], [404]], axis=1)\n", "print(\"After append:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "277823a5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a969f309", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "eaadbd59", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "66964303", "metadata": {}, "source": [ "## 3. Inserting New Elements in Numpy Arrays\n", "- The `np.insert()` method allows us to insert new values along the given axis before the given index.\n", "- The method always returns a copy of the existing numpy array with the values inserted to the given axis.\n", "```\n", "np.insert(arr, index, values, axis=None)\n", "```\n", "- Where,\n", " - `arr` is the array in which we want to insert\n", " - `index` is the index before which we want to insert\n", " - `values` [array_like] values to be added in the `arr`\n", " - If `axis` is not specified, both `arr` and `values` are flattened before use.\n", " - If `axis` is zero, a row is inserted (For 2-D arrays)\n", " - If `axis` is one, a column is inserted (For 2-D arrays)\n", "- The original array remains as such, as it does not occur in-place." ] }, { "cell_type": "markdown", "id": "962d7c69", "metadata": {}, "source": [ "### a. Inserting Elements in 1-D Arrays" ] }, { "cell_type": "code", "execution_count": 12, "id": "668f2e4a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [15 58 34 72 77]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 100, size = 5)\n", "print(\"arr1 = \", arr1)" ] }, { "cell_type": "code", "execution_count": 13, "id": "a6c59fdd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "After insert:\n", "arr1 = [15 58 34 72 77]\n", "arr2 = [15 58 34 55 66 77 72 77]\n" ] } ], "source": [ "# You can insert a scalar value or a list of values in between array elements before the mentioned index\n", "arr2 = np.insert(arr1, 3, [55, 66,77])\n", "print(\"After insert:\")\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "a513d985", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5edc791f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3bf0a3ab", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "0e468e72", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "06a5cb18", "metadata": {}, "source": [ "### b. Inserting Elements in 2-D Arrays" ] }, { "cell_type": "markdown", "id": "de9ed861", "metadata": {}, "source": [ "**Example:** In case of 2-D array, if `axis` is not mentioned the array is flattened first" ] }, { "cell_type": "code", "execution_count": 14, "id": "3284a269", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[5 5 1 9]\n", " [8 3 1 1]\n", " [2 7 9 2]]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (3,4))\n", "print(\"arr1 = \\n\", arr1)" ] }, { "cell_type": "code", "execution_count": 15, "id": "cfad3dcf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "After insert:\n", "arr1 = \n", " [[5 5 1 9]\n", " [8 3 1 1]\n", " [2 7 9 2]]\n", "arr2 = [ 5 5 1 9 55 8 3 1 1 2 7 9 2]\n" ] } ], "source": [ "# Inserting a single value\n", "arr2 = np.insert(arr1, 4, 55)\n", "print(\"After insert:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "40317d8a", "metadata": {}, "outputs": [], "source": [ "# Inserting a multiple values\n", "arr2 = np.insert(arr1, 4, [55, 66])\n", "print(\"After insert:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "ade890b3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "8ef7679e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "556b2299", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "816fd274", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "f2a9adba", "metadata": {}, "source": [ "**Example:** If axis=0, value(s) are added as a row before mentioned index" ] }, { "cell_type": "code", "execution_count": 16, "id": "fc4f9a6b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[8 9 3 7]\n", " [9 8 7 7]\n", " [8 6 1 9]]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (3,4))\n", "print(\"arr1 = \\n\", arr1)" ] }, { "cell_type": "code", "execution_count": 17, "id": "a11a09b7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "After insert:\n", "arr1 = \n", " [[8 9 3 7]\n", " [9 8 7 7]\n", " [8 6 1 9]]\n", "arr2 = \n", " [[ 8 9 3 7]\n", " [ 9 8 7 7]\n", " [55 55 55 55]\n", " [ 8 6 1 9]]\n" ] } ], "source": [ "# For axis = 0, note how the scalar value is replicated before insertion\n", "arr2 = np.insert(arr1, 2, 55, axis=0)\n", "print(\"After insert:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "49fbdaad", "metadata": {}, "outputs": [], "source": [ "# For axis=0, note the size of values has to be 4 in this case (equal to number of columns)\n", "arr2 = np.insert(arr1, 2, [55, 66, 77, 88], axis=0)\n", "print(\"After insert:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "bfca2c79", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "8f584165", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "0ea4dcf0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "87670040", "metadata": {}, "source": [ "**Example:** If axis=1, value(s) are added as a column at mentioned index" ] }, { "cell_type": "code", "execution_count": null, "id": "a92e0c42", "metadata": {}, "outputs": [], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (3,4))\n", "print(\"arr1 = \\n\", arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "e6d02c09", "metadata": {}, "outputs": [], "source": [ "# For axis = 1, note how the scalar value is replicated before insertion\n", "arr2 = np.insert(arr1, 2, 55, axis=1)\n", "print(\"After insert:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "08c536be", "metadata": {}, "outputs": [], "source": [ "# For axis=1, note the size of values has to be 3 in this case (equal to number of rows)\n", "arr2 = np.insert(arr1, 2, [55, 66, 77], axis=1)\n", "print(\"After insert:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "a9c796a3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "835da82c", "metadata": {}, "source": [ "## 4. Deleting Elements of Numpy Arrays\n", "- The `np.delete()` method allows us to delete value(s) from an array at the given index\n", "- This function always returns a copy of the existing numpy array with the values deleted from the given axis.\n", "- If axis is not specified, values can be of any shape and will be flattened before use\n", "```\n", "np.delete(arr, index, axis=None)\n", "```\n", "- The original array remains as such, as it does not occur in-place." ] }, { "cell_type": "markdown", "id": "8179e2e5", "metadata": {}, "source": [ "### a. Deleting Elements from a 1-D Arrays" ] }, { "cell_type": "markdown", "id": "0e6c6747", "metadata": {}, "source": [ "**Example:**" ] }, { "cell_type": "code", "execution_count": null, "id": "fe38b972", "metadata": {}, "outputs": [], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = 5)\n", "print(\"arr1 = \", arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "e988364b", "metadata": {}, "outputs": [], "source": [ "# You can delete a scalar value from a specific index\n", "arr2 = np.delete(arr1, 3)\n", "print(\"After delete:\")\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "d8b82c88", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "c3ce1cc1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "123f4850", "metadata": {}, "source": [ "**Example:**" ] }, { "cell_type": "code", "execution_count": null, "id": "e1911555", "metadata": {}, "outputs": [], "source": [ "arr1 = np.random.randint(low = 1, high = 100, size = 10)\n", "print(\"arr1 = \", arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "73ce969a", "metadata": {}, "outputs": [], "source": [ "# You can delete a list of values in between array elements from specific indices\n", "arr2 = np.delete(arr1, [2,5])\n", "print(\"After delete:\")\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "10436aac", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "20dd4fec", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d8cf1d87", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "569a9b74", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2f5fefe0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9fd1c3ac", "metadata": {}, "source": [ "### b. Deleting Elements from a 2-D Arrays" ] }, { "cell_type": "markdown", "id": "6bc492dc", "metadata": {}, "source": [ "**Example:** Delete a specific element from a 2-D array, don't mention the axis. The resulting array is flattened before use" ] }, { "cell_type": "code", "execution_count": null, "id": "3b859729", "metadata": {}, "outputs": [], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (3,3))\n", "print(\"arr1 = \\n\", arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "fe24666f", "metadata": {}, "outputs": [], "source": [ "arr2 = np.delete(arr1, 5)\n", "print(\"After delete:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "2dcac4ef", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a179b1ec", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2dad6e7d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "fa78d4b7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "25e85c4f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "572970be", "metadata": {}, "source": [ "**Example:** Delete a specific row from an existing 2-D array" ] }, { "cell_type": "code", "execution_count": null, "id": "9c787db6", "metadata": {}, "outputs": [], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (4,4))\n", "print(\"arr1 = \\n\", arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "9aa44191", "metadata": {}, "outputs": [], "source": [ "arr2 = np.delete(arr1, 2, axis=0)\n", "print(\"After delete:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "46b11add", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "467e9c34", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "c59461c9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "4c2a31aa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "12e50b96", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "a67df0fe", "metadata": {}, "source": [ "**Example:** Delete a specific column from an existing 2-D array" ] }, { "cell_type": "code", "execution_count": null, "id": "3e08589e", "metadata": {}, "outputs": [], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (4,4))\n", "print(\"arr1 = \\n\", arr1)" ] }, { "cell_type": "code", "execution_count": null, "id": "e6da91c2", "metadata": {}, "outputs": [], "source": [ "arr2 = np.delete(arr1, 2, axis=1)\n", "print(\"After delete:\")\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "977612c1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "4d25f455", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "770bac4c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "7f00bded", "metadata": {}, "source": [ "## 5. Assigning vs Coping NumPy Arrays" ] }, { "cell_type": "markdown", "id": "8e0e6a49", "metadata": {}, "source": [ "### a. Assigning two NumPy Arrays (Create an alias)" ] }, { "cell_type": "code", "execution_count": 18, "id": "382c3f2c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [8 6 3 4 9 3 4 3 2 9]\n", "arr2 = [8 6 3 4 9 3 4 3 2 9]\n", "140657482813808\n", "140657482813808\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = 10)\n", "\n", "# Creating a copy using assignment operator, both variables point at the same array\n", "arr2 = arr1\n", "\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)\n", "print(id(arr1))\n", "print(id(arr2))" ] }, { "cell_type": "code", "execution_count": null, "id": "2ec4f294", "metadata": {}, "outputs": [], "source": [ "# Change value in arr1 will also occur in arr2\n", "arr2[2] = 55\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b8fd525a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d5dffdcd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "aa2d253d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "d6aa14c7", "metadata": {}, "source": [ "### b. View/Shallow Copy\n", "Arrays that share some data. The view method creates an object looking at the same data. Slicing an array returns a view of that array." ] }, { "cell_type": "code", "execution_count": 19, "id": "670adc44", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [6 9 5 9 6 5 4 8 3 3]\n", "arr2 = [6 9 5 9 6 5 4 8 3 3]\n", "140657482814368\n", "140657482813312\n" ] } ], "source": [ "import numpy as np\n", "arr1 = np.random.randint(low = 1, high = 10, size = 10)\n", "\n", "# Creating a shallow copy (view) using slice operator\n", "arr2 = arr1[:]\n", "\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)\n", "print(id(arr1))\n", "print(id(arr2))" ] }, { "cell_type": "code", "execution_count": 20, "id": "ede79da2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [ 6 9 55 9 6 5 4 8 3 3]\n", "arr2 = [ 6 9 55 9 6 5 4 8 3 3]\n" ] } ], "source": [ "# Change value in arr1 will occur in arr2\n", "arr2[2] = 55\n", "\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "2c5bf0ed", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3371fba8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "341a0304", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3f007f4f", "metadata": {}, "source": [ "### c. Deep Copy" ] }, { "cell_type": "code", "execution_count": 21, "id": "1abe5e75", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [1 5 9 4 1 5 9 8 6 6]\n", "arr2 = [1 5 9 4 1 5 9 8 6 6]\n", "140657482815488\n", "140657482769472\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = 10)\n", "\n", "# Create a Deep copy using copy() method, which will create a new copy of the array\n", "arr2 = arr1.copy()\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)\n", "print(id(arr1))\n", "print(id(arr2))" ] }, { "cell_type": "code", "execution_count": 22, "id": "5b11e005", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [1 5 9 4 1 5 9 8 6 6]\n", "arr2 = [ 1 5 55 4 1 5 9 8 6 6]\n" ] } ], "source": [ "# Change value in array 1 will NOT occur in array 2\n", "arr2[2] = 55\n", "\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)" ] }, { "cell_type": "code", "execution_count": null, "id": "5ec509b4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "06a4ad16", "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 }