{ "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.7 (NumPy-07)

" ] }, { "cell_type": "markdown", "id": "76f782ee", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "1582af95", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "id": "65a0019e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "b8ae36f7", "metadata": {}, "source": [ "# Learning agenda of this notebook" ] }, { "cell_type": "markdown", "id": "30058765", "metadata": {}, "source": [ "## 1. Concatenating of NumPy Arrays" ] }, { "cell_type": "markdown", "id": "fca13446", "metadata": {}, "source": [ "\n", " " ] }, { "cell_type": "markdown", "id": "2a42f40f", "metadata": {}, "source": [ "## 2. Stacking of NumPy Arrays" ] }, { "cell_type": "markdown", "id": "a5841372", "metadata": {}, "source": [ " \n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "bec00301", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5cc11c64", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "1a425185", "metadata": {}, "source": [ "## 3. Splitting NumPy Arrays" ] }, { "cell_type": "markdown", "id": "523c9549", "metadata": {}, "source": [ " " ] }, { "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": null, "id": "98474a05", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "np.__version__ , np.__path__" ] }, { "cell_type": "code", "execution_count": null, "id": "ce87c06b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "cdc7a39b", "metadata": {}, "source": [ "## 1. Concatenating NumPy Arrays\n", "\n", " \n", "\n", "









\n", "\n", "The `np.concatenate()` method is used to join arrays with respect to given axis.\n", "\n", "```\n", "np.concatenate(tup, axis=0)\n", "```\n", "\n", "- Where `tup` is comma separated ndarrays\n", "- If axis is 0, it will join the arrays by row-wise (vertically). For 2-D arrays, the number of columns must match.\n", "- If axis is 1, it will join the arrays by column-wise (horizontally). For 2-D arrays, the number of rows must match.\n", "- For 1-D arrays, the arrays can be of any size/length.\n", "- The original arrays remains as such, as it does not occur in-place." ] }, { "cell_type": "code", "execution_count": null, "id": "b74d3b49", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "8bf21ff4", "metadata": {}, "source": [ "**Example:** Concatenate two 1-D Arrays along axis = 0 (row wise). The 1-D arrays can be of any size/length." ] }, { "cell_type": "code", "execution_count": 2, "id": "328b234a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [96 47 91 69 98]\n", "arr2 = [85 96 25]\n", "\n", "np.concatenate((arr1,arr2)) = [96 47 91 69 98 85 96 25]\n" ] } ], "source": [ "import numpy as np\n", "arr1 = np.random.randint(low = 1, high = 100, size = 5)\n", "arr2 = np.random.randint(low = 1, high = 100, size = 3)\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)\n", "\n", "arr3 = np.concatenate((arr1, arr2))\n", "arr3 = np.concatenate((arr1, arr2), axis=0)\n", "print(\"\\nnp.concatenate((arr1,arr2)) = \", arr3)" ] }, { "cell_type": "markdown", "id": "c9ef53fe", "metadata": {}, "source": [ "You cannot concatenate 1-D arrays on `axis=1`, as it do not exist :)" ] }, { "cell_type": "code", "execution_count": null, "id": "c8652f5e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "1453c811", "metadata": {}, "source": [ "**Example:** Concatenate two 2-D Arrays along `axis=0` (vertically/row-wise). The number of columns of two arrays must match." ] }, { "cell_type": "code", "execution_count": 7, "id": "86f79a6f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[4 4 3]\n", " [9 3 5]]\n", "arr2 = \n", " [[4 1 6]\n", " [9 5 3]\n", " [9 3 2]]\n", "\n", "np.concatenate((arr1,arr2)) = \n", " [[4 4 3]\n", " [9 3 5]\n", " [4 1 6]\n", " [9 5 3]\n", " [9 3 2]]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (2,3))\n", "arr2 = np.random.randint(low = 1, high = 10, size = (3,3))\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)\n", "\n", "arr3 = np.concatenate((arr1, arr2), axis=0)\n", "print(\"\\nnp.concatenate((arr1,arr2)) = \\n\", arr3)" ] }, { "cell_type": "code", "execution_count": null, "id": "20867721", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "c66729fa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "280eed63", "metadata": {}, "source": [ "**Example:** Concatenate two 2-D Arrays along `axis=1` (horizontally/column-wise). The number of rows of two arrays must match." ] }, { "cell_type": "code", "execution_count": 8, "id": "b2096ab9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[7 7]\n", " [6 1]]\n", "arr2 = \n", " [[7 2 9]\n", " [4 4 6]]\n", "\n", "np.concatenate((arr1,arr2)) = \n", " [[7 7 7 2 9]\n", " [6 1 4 4 6]]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (2,2))\n", "arr2 = np.random.randint(low = 1, high = 10, size = (2,3))\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)\n", "\n", "arr3 = np.concatenate((arr1, arr2), axis=1)\n", "print(\"\\nnp.concatenate((arr1,arr2)) = \\n\", arr3)" ] }, { "cell_type": "code", "execution_count": null, "id": "d9bf0876", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "9ad0ca2a", "metadata": {}, "source": [ "## 2. Stacking NumPy Arrays\n", "- Concatenating joins a sequence of arrays along an existing axis, and stacking joins a sequence of arrays along existing as well as along a new axis.\n", "- We can perform stacking along three dimensions:\n", " - `np.vstack()` : it performs vertical stacking along the rows.\n", " - `np.hstack()` : it performs horizontal stacking along with the columns.\n", " - `np.dstack()` : it performs in-depth stacking along a new third axis (depth).\n", "\n", "**Note:** \n", "- `numpy.stack()` is the most general of the three methods, offering an axis parameter for specifying which way to put the arrays together.\n", "- `np.column_stack()` is used to stack 1-D arrays as columns into 2-D array.\n", "- `np.row_stack()` is used to stack 1-D arrays as rows into 2-D array." ] }, { "cell_type": "markdown", "id": "0965f219", "metadata": {}, "source": [ "**Example: `np.row_stack()`**" ] }, { "cell_type": "code", "execution_count": 1, "id": "1a1e8151", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [2 5 1]\n", "arr2 = [3 6 2]\n", "\n", "np.row_stack((arr1, arr2)):\n", " [[2 5 1]\n", " [3 6 2]]\n" ] } ], "source": [ "#ROW STACK\n", "import numpy as np\n", "arr1 = np.array([2,5,1])\n", "arr2 = np.array([3,6,2])\n", "\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)\n", " \n", "arr3 = np.row_stack((arr1, arr2))\n", "\n", "print (\"\\nnp.row_stack((arr1, arr2)):\\n \", arr3)" ] }, { "cell_type": "markdown", "id": "32fa1887", "metadata": {}, "source": [ "**Example: `np.column_stack()`**" ] }, { "cell_type": "code", "execution_count": 2, "id": "e8fb88fb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [2 5 1]\n", "arr2 = [3 6 2]\n", "\n", "np.column_stack((arr1, arr2)):\n", " [[2 3]\n", " [5 6]\n", " [1 2]]\n" ] } ], "source": [ "#COLUMN STACK\n", "import numpy as np\n", "arr1 = np.array([2,5,1])\n", "arr2 = np.array([3,6,2])\n", "\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)\n", " \n", "arr3 = np.column_stack((arr1, arr2))\n", "\n", "print (\"\\nnp.column_stack((arr1, arr2)):\\n \", arr3)" ] }, { "cell_type": "code", "execution_count": null, "id": "5a8a8d26", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "62eb4ce5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b3626ed3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "8b0ecaa1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "6b791ea8", "metadata": {}, "source": [ " \n", "\n", "### a. Use `np.vstack()` for Row-Wise Concatenation\n", "The `np.vstack()` method is used to stack arrays vertically or row-wise.\n", "\n", "```\n", "np.vstack(tup)\n", "```\n", "\n", "- Where `tup` is comma separated ndarrays\n", "- 1-D arrays must have the same size/length, while for 2-D arrays, the number of columns must match.\n", "- It returns an ndarray formed by stacking the given arrays, will be at least 2-D.\n", "- The original arrays remains as such, as it does not occur in-place." ] }, { "cell_type": "code", "execution_count": null, "id": "9491a3b8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3c58a9cf", "metadata": {}, "source": [ "**Example:** Perform vertical stacking of two 1-D Arrays, which must have the same size/length." ] }, { "cell_type": "code", "execution_count": 11, "id": "0a1ba250", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [1 4 5 6]\n", "arr2 = [8 7 4 9]\n", "\n", "np.vstack((arr1, arr2)):\n", " [[1 4 5 6]\n", " [8 7 4 9]]\n" ] } ], "source": [ "import numpy as np\n", "arr1 = np.random.randint(low = 1, high = 10, size = 4)\n", "arr2 = np.random.randint(low = 1, high = 10, size = 4)\n", "\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)\n", " \n", "arr3 = np.vstack((arr1, arr2))\n", "\n", "print (\"\\nnp.vstack((arr1, arr2)):\\n \", arr3)" ] }, { "cell_type": "markdown", "id": "eaca3c1e", "metadata": {}, "source": [ "Note: The output array is a 2-D array" ] }, { "cell_type": "code", "execution_count": null, "id": "a92c8ce6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "29694002", "metadata": {}, "source": [ "**Example:** Perform vertical stacking of two 2-D Arrays. The number of columns of two arrays must match" ] }, { "cell_type": "code", "execution_count": 14, "id": "dd21992c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[9 1 8]\n", " [5 8 3]]\n", "arr2 = \n", " [[9 8 6]\n", " [8 4 7]\n", " [7 1 8]]\n", "\n", " np.vstack((arr1, arr2)):\n", " [[9 1 8]\n", " [5 8 3]\n", " [9 8 6]\n", " [8 4 7]\n", " [7 1 8]]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (2,3))\n", "arr2 = np.random.randint(low = 1, high = 10, size = (3,3))\n", "\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)\n", "\n", "arr3 = np.vstack((arr1, arr2))\n", "print (\"\\n np.vstack((arr1, arr2)):\\n \", arr3)" ] }, { "cell_type": "code", "execution_count": null, "id": "780d1db4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "2e1e3875", "metadata": {}, "source": [ " \n", "\n", "### b. Using `np.hstack()` for Column-Wise Concatenation\n", "The `np.hstack()` method is used to stack arrays horizontally or column-wise.\n", "\n", "```\n", "np.hstack(tup)\n", "```\n", "\n", "- Where `tup` is comma separated ndarrays\n", "- 1-D arrays can have any size/length, while for 2-D arrays, the number of rows must match.\n", "- It returns an ndarray formed by stacking the given arrays.\n", "- The original arrays remains as such, as it does not occur in-place." ] }, { "cell_type": "code", "execution_count": null, "id": "deceefd3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "7e71cc75", "metadata": {}, "source": [ "**Example:** Perform horizontal stacking of two 1-D Arrays, which can be of different size/length" ] }, { "cell_type": "code", "execution_count": 15, "id": "bac46141", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [6 2 8 9 3]\n", "arr2 = [4 1 1 9]\n", "\n", " np.hstack((arr1, arr2)):\n", " [6 2 8 9 3 4 1 1 9]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = 5)\n", "arr2 = np.random.randint(low = 1, high = 10, size = 4)\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)\n", " \n", "arr3 = np.hstack((arr1, arr2))\n", "print (\"\\n np.hstack((arr1, arr2)):\\n \", arr3)" ] }, { "cell_type": "markdown", "id": "5d786095", "metadata": {}, "source": [ "Note: The output array is a 1-D array" ] }, { "cell_type": "code", "execution_count": null, "id": "3351f377", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "f7eb1d49", "metadata": {}, "source": [ "**Example:** Perform horizontal stacking of two 2-D Arrays. The number of rows of two arrays must match" ] }, { "cell_type": "code", "execution_count": 18, "id": "1b6fce65", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[8 3]\n", " [9 8]]\n", "arr2 = \n", " [[6 8 9]\n", " [3 2 4]]\n", "\n", " np.hstack((arr1, arr2)):\n", " [[8 3 6 8 9]\n", " [9 8 3 2 4]]\n" ] } ], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = (2,2))\n", "arr2 = np.random.randint(low = 1, high = 10, size = (2,3))\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)\n", "\n", "arr3 = np.hstack((arr1, arr2))\n", "print (\"\\n np.hstack((arr1, arr2)):\\n \", arr3)" ] }, { "cell_type": "code", "execution_count": null, "id": "0715afa4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b2cb4513", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "538597f0", "metadata": {}, "source": [ "### e. Using `np.stack()`\n", "\n", "- The `np.stack()` method is used to join a sequence of same dimension arrays along a new axis.\n", "- The axis parameter specifies the index of the new axis in the dimensions of the result. \n", "- For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.\n", "```\n", "np.stack(a1, a2, a3, ..., axis=0)\n", "```\n", "\n", "- Where `tup` is comma separated ndarrays\n", "- 1-D or 2-D arrays must have the same shape, while n-D arrays must have the same shape along all but the third axis.\n", "- It returns the array formed by stacking the given arrays, which has one more dimension than the input arrays.\n", "- This function makes most sense for arrays with up to 3 dimensions. For instance, for pixel-data with a height (first axis), width (second axis), and r/g/b channels (third axis).\n", "\n", "\n", "\n", "**Note:** Concatenating joins a sequence of tensors along an existing axis, and stacking joins a sequence of tensors along a new axis" ] }, { "cell_type": "markdown", "id": "bf7c1a4c", "metadata": {}, "source": [ "**Example:** Perform stacking of two 1-D Arrays, which must have the same size/shape." ] }, { "cell_type": "code", "execution_count": 20, "id": "d41eb42c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = [6 9 9 8]\n", "arr2 = [7 2 7 3]\n", "\n", " np.stack(arr1, arr2, axis=0):\n", " [[6 9 9 8]\n", " [7 2 7 3]]\n", "\n", " np.row_stack(arr1, arr2)):\n", " [[6 9 9 8]\n", " [7 2 7 3]]\n", "\n", " np.stack((arr1, arr2), axis=1):\n", " [[6 7]\n", " [9 2]\n", " [9 7]\n", " [8 3]]\n", "\n", " np.column_stack(arr1, arr2):\n", " [[6 7]\n", " [9 2]\n", " [9 7]\n", " [8 3]]\n" ] } ], "source": [ "import numpy as np\n", "arr1 = np.random.randint(low = 1, high = 10, size = 4)\n", "arr2 = np.random.randint(low = 1, high = 10, size = 4)\n", "print(\"arr1 = \", arr1)\n", "print(\"arr2 = \", arr2)\n", " \n", "# Stacking two 1-D arrays along axis 0 using stack()\n", "arr3 = np.stack((arr1, arr2), axis = 0)\n", "print (\"\\n np.stack(arr1, arr2, axis=0):\\n \", arr3)\n", "# Stacking the two 1-D arrays using row_stack()\n", "arr4 = np.row_stack((arr1, arr2))\n", "print (\"\\n np.row_stack(arr1, arr2)):\\n \", arr4)\n", " \n", "\n", "# Stacking two 1-D arrays along axis 1 using stack()\n", "arr5 = np.stack((arr1, arr2), axis = 1)\n", "print (\"\\n np.stack((arr1, arr2), axis=1):\\n \", arr5)\n", "# Stacking the two 1-D arrays using column_stack()\n", "arr6 = np.column_stack((arr1, arr2))\n", "print (\"\\n np.column_stack(arr1, arr2):\\n \", arr6)" ] }, { "cell_type": "code", "execution_count": null, "id": "c33b2b61", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "ec563ebb", "metadata": {}, "source": [ "**Example:** Perform stacking of two 2-D Arrays, which must have the same size/shape." ] }, { "cell_type": "code", "execution_count": 21, "id": "96c36e49", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[4 3 1]\n", " [5 6 2]]\n", "arr2 = \n", " [[5 1 8]\n", " [3 9 1]]\n", "\n", " np.stack((arr1, arr2), axis=0): \n", " [[[4 3 1]\n", " [5 6 2]]\n", "\n", " [[5 1 8]\n", " [3 9 1]]]\n", "shape of arr3: (2, 2, 3)\n" ] } ], "source": [ "import numpy as np\n", "arr1 = np.array([[4,3,1],[5,6,2]])\n", "arr2 = np.array([[5,1,8],[3,9,1]])\n", "\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)\n", "\n", "# Stacking the two arrays along axis 0\n", "arr3 = np.stack((arr1, arr2), axis = 0)\n", "print (\"\\n np.stack((arr1, arr2), axis=0): \\n\", arr3)\n", "\n", "print(\"shape of arr3:\", arr3.shape)" ] }, { "cell_type": "code", "execution_count": 22, "id": "75d2beb7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr1 = \n", " [[4 3 1]\n", " [5 6 2]]\n", "arr2 = \n", " [[5 1 8]\n", " [3 9 1]]\n", "\n", " np.stack((arr1, arr2), axis=1): \n", " [[[4 3 1]\n", " [5 1 8]]\n", "\n", " [[5 6 2]\n", " [3 9 1]]]\n", "shape of arr3: (2, 2, 3)\n" ] } ], "source": [ "import numpy as np\n", "arr1 = np.array([[4,3,1],[5,6,2]])\n", "arr2 = np.array([[5,1,8],[3,9,1]])\n", "\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)\n", "\n", "# Stacking the two arrays along axis 1\n", "arr3 = np.stack((arr1, arr2), axis = 1)\n", "print (\"\\n np.stack((arr1, arr2), axis=1): \\n\", arr3)\n", "\n", "print(\"shape of arr3:\", arr3.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "8bc1301b", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "arr1 = np.array([[4,3,1],[5,6,2]])\n", "arr2 = np.array([[5,1,8],[3,9,1]])\n", "\n", "print(\"arr1 = \\n\", arr1)\n", "print(\"arr2 = \\n\", arr2)\n", "\n", " \n", "# Stacking the two arrays along last axis\n", "arr3 = np.stack((arr1, arr2), axis = -1)\n", "print (\"\\n np.stack((arr1, arr2), axis=-1):\\n \", arr3)\n", "print(\"shape of arr3:\", arr3.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "643a3e7b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3bb7e22c", "metadata": {}, "source": [ "## 3. Splitting NumPy Arrays\n", "- Splitting is reverse operation of Joining, and is used to split one array into multiple arrays....\n", "- We can perform splitting along three dimensions:\n", " - `np.split()` : Split array into a list of multiple sub-arrays of equal size.\n", " - `np.hsplit()` : Split array into multiple sub-arrays horizontally (column wise).\n", " - `np.vsplit()` : Split array into multiple sub-arrays vertically (row wise)." ] }, { "cell_type": "code", "execution_count": null, "id": "1093d12a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "968822fb", "metadata": {}, "source": [ "### a. The `np.split()` and `np.array_split()` Methods\n", "\n", " \n", " \n", "\n", "









\n", "- The `np.split()` method splits an array into multiple sub-arrays of equal sizes.\n", "\n", "```\n", "np.split(arr, size, axis=0)\n", "```\n", "- Where,\n", " - `arr` is the array to be divided into sub-arrays.\n", " - `size` is an size of the sub-arrays, into which `arr` will be divided along the axis. \n", " - `axis` is the axis along which to split, default is 0.\n", "- If such split is not possible, an error is raised. To avoid error you can use `np.array_split()`\n", "- It returns a list of sub-arrays as views into `arr`" ] }, { "cell_type": "markdown", "id": "9b03967e", "metadata": {}, "source": [ "**Example:** Use of `split()`" ] }, { "cell_type": "code", "execution_count": null, "id": "93d40d2b", "metadata": {}, "outputs": [], "source": [ "arr1 = np.random.randint(low = 1, high = 10, size = 20)\n", "print(\"arr1:\\n\",arr1)\n", "\n", "# The split size must be a factor of array size (can be 1, 2, 4, 5, 10) \n", "print(\"\\nSub-arrays: \\n\", np.split(arr1, 4))" ] }, { "cell_type": "code", "execution_count": null, "id": "334e0358", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "0fe4f87c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "5502e895", "metadata": {}, "source": [ "**Example:** Use of `array_split()`" ] }, { "cell_type": "code", "execution_count": null, "id": "37370678", "metadata": {}, "outputs": [], "source": [ "# create an array of float type\n", "arr1 = np.random.randint(low = 1, high = 10, size = 13)\n", "print(\"arr1:\\n\",arr1)\n", "\n", "# The array_split() will not flag an error if size is not a factor of array size (will manage)\n", "print(\"\\nSub-arrays: \\n\", np.array_split(arr1, 4))" ] }, { "cell_type": "code", "execution_count": null, "id": "4144eb48", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "0a688f2a", "metadata": {}, "source": [ " \n", "\n", "### b. The `np.hsplit()` Method\n", "- The `np.hsplit()` method is used to split an array into multiple sub-arrays horizontally (column-wise). \n", "- The `np.hsplit()` is equivalent to split with axis=1, the array is always split along the second axis regardless of the array dimension.\n", "\n", "```\n", "np.hsplit(arr, size)\n", "```\n", "- Where,\n", " - `arr` is the array to be divided into sub-arrays.\n", " - `size` is the size of the sub-arrays, into which `arr` will be divided along the axis. For `hsplit()`, size argument should be a factor of number of columns, else it flags an error\n", "- It returns a list of sub-arrays as views into `arr`" ] }, { "cell_type": "markdown", "id": "6042a59e", "metadata": {}, "source": [ "**Example:**" ] }, { "cell_type": "code", "execution_count": null, "id": "d9baa401", "metadata": {}, "outputs": [], "source": [ "# create an array of float type with 4 rows and 4 columns with sequential values from 0 to 15\n", "arr1 = np.arange(16.0).reshape(4,4)\n", "# print array\n", "print(\"arr1:\\n\",arr1)\n", "print(\"shape: \", arr1.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "9d1e5fd3", "metadata": {}, "outputs": [], "source": [ "# horizontally split array into 2 subarrays\n", "print(\"\\nSub-arrays: \\n\", np.hsplit(arr1, 2))" ] }, { "cell_type": "code", "execution_count": null, "id": "b9ea9ca1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "6fe57a59", "metadata": {}, "source": [ " \n", "\n", "### c. The `np.vsplit()` Method\n", "- The `np.vsplit()` method is used to split an array into multiple sub-arrays vertically (row-wise). Not applicable for 1-D array.\n", "- The `np.vsplit()` is equivalent to split with axis=0, the array is always split along the first axis regardless of the array dimension.\n", "\n", "```\n", "np.vsplit(arr, size)\n", "```\n", "-Where,\n", " - `arr` is the array to be divided into sub-arrays.\n", " - `size` is the size of the sub-arrays, into which `arr` will be divided along the axis. For `vsplit()`, size argument should be a factor of number of rows, else it flags an error\n", "- It returns a list of sub-arrays as views into `arr`" ] }, { "cell_type": "markdown", "id": "134c339a", "metadata": {}, "source": [ "**Example:**" ] }, { "cell_type": "code", "execution_count": null, "id": "b4f3ba75", "metadata": {}, "outputs": [], "source": [ "# create an array of float type with 4 rows and 5 columns with sequential values from 0 to 19\n", "arr1 = np.arange(16.0).reshape(4,4)\n", "print(\"arr1:\\n\",arr1)\n", "print(\"shape: \", arr1.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "ffb810cb", "metadata": {}, "outputs": [], "source": [ "# vertically split array into 2 subarrays (remember size argument must be a factor of number of rows )\n", "print(\"\\nSub-arrays: \\n\", np.vsplit(arr1, 2))" ] }, { "cell_type": "code", "execution_count": null, "id": "61acb5b9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "609b8419", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "fb676934", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "db14fa84", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "91e9898b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "ac133d80", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 10, "id": "49c29303", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr3: [[2 3]\n", " [5 6]\n", " [1 2]]\n", "arr4: [[2 5 1]\n", " [3 6 2]]\n", "arr5: [2 5 1 3 6 2]\n", "arr6: [[2 3]\n", " [5 6]\n", " [1 2]]\n" ] } ], "source": [ "import numpy as np\n", "arr1 = np.array([2,5,1])\n", "arr2 = np.array([3,6,2])\n", "arr3 = np.column_stack((arr1, arr2))\n", "arr4 = np.vstack((arr1, arr2))\n", "arr5 = np.hstack((arr1, arr2))\n", "arr6 = np.stack((arr1, arr2), axis = 1)\n", "print (\"arr3: \", arr3)\n", "print (\"arr4: \", arr4)\n", "print (\"arr5: \", arr5)\n", "print (\"arr6: \", arr6)" ] }, { "cell_type": "code", "execution_count": null, "id": "5beeaacc", "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 }