{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Untitled45.ipynb", "provenance": [], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "-qo9S0pSPBxD", "colab_type": "text" }, "source": [ "# The Basics of NumPy Arrays" ] }, { "cell_type": "markdown", "metadata": { "id": "Sn2K_0wwPSG9", "colab_type": "text" }, "source": [ "![alt text](https://moriohcdn.b-cdn.net/132df0ee5d.png)" ] }, { "cell_type": "markdown", "metadata": { "id": "m-zULkOzPDVS", "colab_type": "text" }, "source": [ "## NumPy Array Attributes" ] }, { "cell_type": "code", "metadata": { "id": "Q7aR0N26PHzG", "colab_type": "code", "colab": {} }, "source": [ "import numpy as np\n", "np.random.seed(0)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "tDjh1cIcPaYZ", "colab_type": "text" }, "source": [ "## Creating a one dimensional array." ] }, { "cell_type": "code", "metadata": { "id": "6RpS67urPXvO", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "d962a3ff-89c9-408f-97a2-7c00c9ec848d" }, "source": [ "# Creating a one dimensional array.\n", "one = np.random.randint(10, size = 6)\n", "print(one)" ], "execution_count": 33, "outputs": [ { "output_type": "stream", "text": [ "[5 0 3 3 7 9]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "cYuSWJ8PPq4v", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "k0hichAkPnNL", "colab_type": "text" }, "source": [ "## Creating a two dimensional array." ] }, { "cell_type": "code", "metadata": { "id": "e6Aorkx0PlS6", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 52 }, "outputId": "b4811ec5-3d88-4806-c666-a1af16edf028" }, "source": [ "# Creating a two dimensional array.\n", "two = np.random.randint(10, size = (2, 5))\n", "print(two)" ], "execution_count": 34, "outputs": [ { "output_type": "stream", "text": [ "[[3 5 2 4 7]\n", " [6 8 8 1 6]]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "ogITIMRyPrkM", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "QlfY3Rx5PsE_", "colab_type": "text" }, "source": [ "## Creating a three dimensional array." ] }, { "cell_type": "code", "metadata": { "id": "MaywX9E7Pu3j", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 260 }, "outputId": "54563da2-20b4-4cda-b66e-26945bcef7e2" }, "source": [ "# Creating a three dimensional array.\n", "three = np.random.randint(10, size = (3, 4, 5))\n", "print(three)" ], "execution_count": 35, "outputs": [ { "output_type": "stream", "text": [ "[[[7 7 8 1 5]\n", " [9 8 9 4 3]\n", " [0 3 5 0 2]\n", " [3 8 1 3 3]]\n", "\n", " [[3 7 0 1 9]\n", " [9 0 4 7 3]\n", " [2 7 2 0 0]\n", " [4 5 5 6 8]]\n", "\n", " [[4 1 4 9 8]\n", " [1 1 7 9 9]\n", " [3 6 7 2 0]\n", " [3 5 9 4 4]]]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "RbE8EOLnRvyu", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Km79fDn0Pz_p", "colab_type": "text" }, "source": [ "## Using the attributes to determine the dimension of the arrays." ] }, { "cell_type": "code", "metadata": { "id": "KVBSmXHNP03f", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "ddf530de-9fe4-4598-dc46-48275f69c9f9" }, "source": [ "# Using the attributes to determine the dimension of the arrays.\n", "print(\"one ==>\", one.ndim )\n", "print(\"two ==>\", two.ndim )\n", "print(\"three ==>\", three.ndim )" ], "execution_count": 36, "outputs": [ { "output_type": "stream", "text": [ "one ==> 1\n", "two ==> 2\n", "three ==> 3\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "gG6RwZKIRvEm", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "SBMu3kkkP6vP", "colab_type": "text" }, "source": [ "## Using the attribute to determine the shape of the arrays." ] }, { "cell_type": "code", "metadata": { "id": "hBDFFJjUP7oO", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "52c0b5d1-0b35-4b1b-f4f4-249fd3d4edf6" }, "source": [ "print(\"one ==>\", one.shape)\n", "print(\"two ==>\", two.shape)\n", "print(\"three ==>\", three.shape)" ], "execution_count": 37, "outputs": [ { "output_type": "stream", "text": [ "one ==> (6,)\n", "two ==> (2, 5)\n", "three ==> (3, 4, 5)\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "TwKHM1KdP-ZO", "colab_type": "text" }, "source": [ "## Using the attribute to determine the size of the arrays." ] }, { "cell_type": "code", "metadata": { "id": "jVqNzLbyQBnb", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "28a31b51-6cc9-4103-d25c-fe3a5aad757b" }, "source": [ "# Using the attribute to determine the size of the arrays.\n", "print(\"one ==>\", one.size)\n", "print(\"two ==>\", two.size)\n", "print(\"three ==>\", three.size)" ], "execution_count": 38, "outputs": [ { "output_type": "stream", "text": [ "one ==> 6\n", "two ==> 10\n", "three ==> 60\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "ssTtTOHvRtlx", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "HDoD4rroQE0W", "colab_type": "text" }, "source": [ "## Using the attribute to determine the datatype of the arrays." ] }, { "cell_type": "code", "metadata": { "id": "GBjr9s0yQG2r", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "9947d549-0073-4da8-e043-9ab241ad7a9f" }, "source": [ "# Using the attribute to determine the datatype of the arrays.\n", "print(\"one ==>\", one.dtype)\n", "print(\"two ==>\", two.dtype)\n", "print(\"three ==>\", three.dtype)" ], "execution_count": 39, "outputs": [ { "output_type": "stream", "text": [ "one ==> int64\n", "two ==> int64\n", "three ==> int64\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "Tvw9gkh0Rs1q", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "ihoiHfdDQLYG", "colab_type": "text" }, "source": [ "## Using the attribute to determine the item size of the arrays." ] }, { "cell_type": "code", "metadata": { "id": "vPeYsDhBQJ-P", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "05580e31-c946-47e8-f316-e682fb823987" }, "source": [ "# Using the attribute to determine the item size of the arrays.\n", "print(\"one ==>\", one.itemsize)\n", "print(\"two ==>\", two.itemsize)\n", "print(\"three ==>\", three.itemsize)" ], "execution_count": 40, "outputs": [ { "output_type": "stream", "text": [ "one ==> 8\n", "two ==> 8\n", "three ==> 8\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "tCITvwkiRsBM", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "p5iASnKaQPL2", "colab_type": "text" }, "source": [ "## Using the attribute to determine the total number of bytes of the array." ] }, { "cell_type": "code", "metadata": { "id": "dgGVcORiQQCb", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "bbc50725-857e-4607-a630-92ff88e81ded" }, "source": [ "print(\"one ==>\", one.nbytes)\n", "print(\"two ==>\", two.nbytes)\n", "print(\"three ==>\", three.nbytes)" ], "execution_count": 41, "outputs": [ { "output_type": "stream", "text": [ "one ==> 48\n", "two ==> 80\n", "three ==> 480\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "mTS5yuJiRrC5", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "aznaNp1hQUCj", "colab_type": "text" }, "source": [ "## Array Indexing: Accessing Single Elements" ] }, { "cell_type": "code", "metadata": { "id": "xa3ZMpyLQUsD", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "0a33d763-2895-433b-ebe7-f82fdfa230b8" }, "source": [ "# Array Indexing: Accessing Single Elements\n", "oneValue = one[2]\n", "print(oneValue)" ], "execution_count": 42, "outputs": [ { "output_type": "stream", "text": [ "3\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "4_w6tJ00RqFW", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "6PNAeYc3QZEk", "colab_type": "text" }, "source": [ "## Accessing the elements in a multi dimensional array" ] }, { "cell_type": "code", "metadata": { "id": "fYkRVgzqQZ8Y", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "defa925e-6a04-488d-ca12-0fbee6750c62" }, "source": [ "# Accessing the elements in a multi dimensional array\n", "print(two)\n", "twoValue = two[0, 4]\n", "print(twoValue)" ], "execution_count": 43, "outputs": [ { "output_type": "stream", "text": [ "[[3 5 2 4 7]\n", " [6 8 8 1 6]]\n", "7\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "Y60uCsf8Rpfu", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "SlZINYGxQdk1", "colab_type": "text" }, "source": [ "## Changing the values of the array" ] }, { "cell_type": "code", "metadata": { "id": "4BqL6vG_Qe1n", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "19bc5a4b-55a8-4b46-cfcd-45bdeb2127f1" }, "source": [ "# Changing the values of the array\n", "print(one)\n", "one[3] = 6\n", "print(one)\n", "one[4] = 4.56 # It will be silently truncated\n", "print(one)" ], "execution_count": 44, "outputs": [ { "output_type": "stream", "text": [ "[5 0 3 3 7 9]\n", "[5 0 3 6 7 9]\n", "[5 0 3 6 4 9]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "90wOdhnrRoq3", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Jp6DxgT1QkJK", "colab_type": "text" }, "source": [ "## Array Slicing: Accessing Subarrays" ] }, { "cell_type": "code", "metadata": { "id": "gqMzY-k2QlFf", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 277 }, "outputId": "85b96a55-a08d-4f55-875f-f730e1fc4540" }, "source": [ "# Array Slicing: Accessing Subarrays\n", "# The syntax of the slicing is ==> x[start:stop:step] ==> default value is:\n", "# [0: size of the dimension: 1]\n", "\n", "a = np.random.randint(10, size = (10))\n", "print(a)\n", "print(\"------------------------\")\n", "\n", "print(a[4: 9: 2])\n", "print(\"------------------------\")\n", "\n", "print(a[1::2])\n", "print(\"------------------------\")\n", "\n", "print(a[::2])\n", "print(\"------------------------\")\n", "\n", "print(a[:9:])\n", "print(\"------------------------\")\n", "\n", "# This is the best example for reversing an array\n", "print(a[::-1])\n", "print(\"------------------------\")\n", "\n", "# I tries on strings and it worked !!!\n", "a = \"University of Regina is awesome !!!\"\n", "print(a)\n", "print(\"------------------------\")\n", "\n", "print(a[::-1])" ], "execution_count": 45, "outputs": [ { "output_type": "stream", "text": [ "[6 4 4 3 4 4 8 4 3 7]\n", "------------------------\n", "[4 8 3]\n", "------------------------\n", "[4 3 4 4 7]\n", "------------------------\n", "[6 4 4 8 3]\n", "------------------------\n", "[6 4 4 3 4 4 8 4 3]\n", "------------------------\n", "[7 3 4 8 4 4 3 4 4 6]\n", "------------------------\n", "University of Regina is awesome !!!\n", "------------------------\n", "!!! emosewa si anigeR fo ytisrevinU\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "c8vQ8xOrQvbT", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 208 }, "outputId": "ce8695bf-e55b-47cb-99db-b124339486cf" }, "source": [ "print(two)\n", "print(\"------------------------\")\n", "\n", "twoValue = two[:2, :3]\n", "print(twoValue)\n", "print(\"------------------------\")\n", "\n", "twoValue = two[:2, ::3]\n", "print(twoValue)\n", "print(\"------------------------\")\n", "\n", "twoValue = two[::-1, ::-1]\n", "print(twoValue)" ], "execution_count": 46, "outputs": [ { "output_type": "stream", "text": [ "[[3 5 2 4 7]\n", " [6 8 8 1 6]]\n", "------------------------\n", "[[3 5 2]\n", " [6 8 8]]\n", "------------------------\n", "[[3 4]\n", " [6 1]]\n", "------------------------\n", "[[6 1 8 8 6]\n", " [7 4 2 5 3]]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "z2pe6mx3RnVm", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "-ybLTKiGQxY4", "colab_type": "text" }, "source": [ "## Accessing array rows and columns." ] }, { "cell_type": "code", "metadata": { "id": "H8bGZEkkQyUg", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 156 }, "outputId": "d05a5b6d-4b75-442f-eac5-b4c0f6ca79f8" }, "source": [ "# Accessing array rows and columns.\n", "print(two)\n", "print(\"------------------------\")\n", "\n", "print(two[:, 0]) # first coloumn \n", "print(\"------------------------\")\n", "\n", "print(two[0, :]) # first row\n", "print(\"------------------------\")\n", "\n", "print(two[0]) # Entire row" ], "execution_count": 47, "outputs": [ { "output_type": "stream", "text": [ "[[3 5 2 4 7]\n", " [6 8 8 1 6]]\n", "------------------------\n", "[3 6]\n", "------------------------\n", "[3 5 2 4 7]\n", "------------------------\n", "[3 5 2 4 7]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "Xo2t3EFSRmP-", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "LDutoT8bQ1CS", "colab_type": "text" }, "source": [ "## Modifing the array without using copy() function." ] }, { "cell_type": "code", "metadata": { "id": "hYvilF5-Q1pU", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 208 }, "outputId": "e6d78c11-0b58-4bbb-d84a-98160e09cd2c" }, "source": [ "# Modifing the array without using copy() function.\n", "\n", "print(two)\n", "print(\"------------------------\")\n", "\n", "twoValue = two[:2, :2]\n", "print(twoValue)\n", "print(\"------------------------\")\n", "\n", "twoValue[0, 0] = 25\n", "print(twoValue)\n", "print(\"------------------------\")\n", "\n", "print(two)" ], "execution_count": 48, "outputs": [ { "output_type": "stream", "text": [ "[[3 5 2 4 7]\n", " [6 8 8 1 6]]\n", "------------------------\n", "[[3 5]\n", " [6 8]]\n", "------------------------\n", "[[25 5]\n", " [ 6 8]]\n", "------------------------\n", "[[25 5 2 4 7]\n", " [ 6 8 8 1 6]]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "KB5GkHcIRlEz", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "miesr6LFQ4qn", "colab_type": "text" }, "source": [ "## Modifing the array with using copy() function." ] }, { "cell_type": "code", "metadata": { "id": "jGcuAGNEQ5eK", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 208 }, "outputId": "9288d80a-6d96-40e0-aabd-d2f722fd8235" }, "source": [ "\n", "# Modifing the array with using copy() function.\n", "\n", "print(two)\n", "print(\"------------------------\")\n", "\n", "twoValue = two[:2, :2].copy()\n", "print(twoValue)\n", "print(\"------------------------\")\n", "\n", "twoValue[0, 0] = 45\n", "print(twoValue)\n", "print(\"------------------------\")\n", "\n", "print(two)" ], "execution_count": 49, "outputs": [ { "output_type": "stream", "text": [ "[[25 5 2 4 7]\n", " [ 6 8 8 1 6]]\n", "------------------------\n", "[[25 5]\n", " [ 6 8]]\n", "------------------------\n", "[[45 5]\n", " [ 6 8]]\n", "------------------------\n", "[[25 5 2 4 7]\n", " [ 6 8 8 1 6]]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "rKS7aQPURjUY", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "jrcZ0-KBQ8jJ", "colab_type": "text" }, "source": [ "## Reshaping of Arrays using reshape() in a more advanced way" ] }, { "cell_type": "code", "metadata": { "id": "dG6yE4VYQ9Va", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "d382e087-feaa-4f56-dc3d-e057fe78f076" }, "source": [ "# Reshaping of Arrays using reshape() in a more advanced way\n", "\n", "grid = np.arange(1, 10).reshape(3, 3)\n", "print(grid)" ], "execution_count": 50, "outputs": [ { "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]\n", " [7 8 9]]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "D3uIf7NkRifk", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Rs3dhY_GRA4N", "colab_type": "text" }, "source": [ "## Reshaping the arrays using newaxis\n" ] }, { "cell_type": "code", "metadata": { "id": "Ld4u0saaRBrN", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 260 }, "outputId": "c6474919-4027-4cc8-f9cd-3714d4548615" }, "source": [ "# Reshaping the arrays using newaxis\n", "\n", "print(one)\n", "print(\"------------------------\")\n", "print(one.reshape(3, 2))\n", "print(\"------------------------\")\n", "\n", "print(one[ np.newaxis, :])\n", "print(\"------------------------\")\n", "\n", "print(one[:, np.newaxis])" ], "execution_count": 51, "outputs": [ { "output_type": "stream", "text": [ "[5 0 3 6 4 9]\n", "------------------------\n", "[[5 0]\n", " [3 6]\n", " [4 9]]\n", "------------------------\n", "[[5 0 3 6 4 9]]\n", "------------------------\n", "[[5]\n", " [0]\n", " [3]\n", " [6]\n", " [4]\n", " [9]]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "cadSKl9QRhfD", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "ECcx7T23RGhb", "colab_type": "text" }, "source": [ "## Array Concatenation and Splitting" ] }, { "cell_type": "code", "metadata": { "id": "GiiHfsf4RHVG", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 277 }, "outputId": "12e81444-91ac-4166-a0da-30a9781a1718" }, "source": [ "a = np.array([1, 2, 3])\n", "b = np.array([4, 5, 6])\n", "c = np.concatenate([a, b])\n", "print(c)\n", "print(\"------------------------\")\n", "\n", "d = np.array([7, 8, 9])\n", "e = np.concatenate([c, d])\n", "print(e)\n", "print(\"------------------------\")\n", "\n", "print(two)\n", "print(\"------------------------\")\n", "\n", "f = np.concatenate([two, two])\n", "print(f)\n", "print(\"------------------------\")\n", "\n", "# concatenate along the second axis (zero-indexed)\n", "g = np.concatenate([two, two], axis = 1)\n", "print(g)\n", "print(\"------------------------\")" ], "execution_count": 52, "outputs": [ { "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n", "------------------------\n", "[1 2 3 4 5 6 7 8 9]\n", "------------------------\n", "[[25 5 2 4 7]\n", " [ 6 8 8 1 6]]\n", "------------------------\n", "[[25 5 2 4 7]\n", " [ 6 8 8 1 6]\n", " [25 5 2 4 7]\n", " [ 6 8 8 1 6]]\n", "------------------------\n", "[[25 5 2 4 7 25 5 2 4 7]\n", " [ 6 8 8 1 6 6 8 8 1 6]]\n", "------------------------\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "RvPc8JyNRgSF", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "SJS2g4YkRLQI", "colab_type": "text" }, "source": [ "## Use np.vstack and np.hstack while you play with different types of Arrays." ] }, { "cell_type": "code", "metadata": { "id": "5WiVqCbVRMHa", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 225 }, "outputId": "011a9dcc-7000-47c0-e6c3-e4ea36769eae" }, "source": [ "a = np.array([1, 2, 3])\n", "print(a)\n", "print(\"------------------------\")\n", "\n", "b = np.array([[4, 5, 6], \n", " [7, 8, 9]])\n", "print(b)\n", "print(\"------------------------\")\n", "\n", "print(np.vstack([a, b]))\n", "print(\"------------------------\")\n", "\n", "\n", "# horizontally stack the arrays\n", "y = np.array([[99],\n", " [99]])\n", "print(np.hstack([b, y]))\n", "print(\"------------------------\")" ], "execution_count": 53, "outputs": [ { "output_type": "stream", "text": [ "[1 2 3]\n", "------------------------\n", "[[4 5 6]\n", " [7 8 9]]\n", "------------------------\n", "[[1 2 3]\n", " [4 5 6]\n", " [7 8 9]]\n", "------------------------\n", "[[ 4 5 6 99]\n", " [ 7 8 9 99]]\n", "------------------------\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "kuVdGrUURd6a", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "il9Ba65MROxA", "colab_type": "text" }, "source": [ "## Splitting of arrays" ] }, { "cell_type": "code", "metadata": { "id": "Fgh6RE7ARP8F", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "1f3d6d18-1639-401d-b104-bfd6f0bfb36b" }, "source": [ "# Splitting of arrays\n", "a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])\n", "print(a)\n", "print(\"------------------------\")\n", "b, c, d = np.split(a, [2, 5])\n", "print(b, c, d)" ], "execution_count": 54, "outputs": [ { "output_type": "stream", "text": [ "[1 2 3 4 5 6 7 8 9 0]\n", "------------------------\n", "[1 2] [3 4 5] [6 7 8 9 0]\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "Q1ehBB5ORTG4", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 87 }, "outputId": "2d00f2a1-5c92-4db5-c6f5-16f3179ed6fd" }, "source": [ "grid = np.arange(16).reshape(4, 4)\n", "print(grid)" ], "execution_count": 55, "outputs": [ { "output_type": "stream", "text": [ "[[ 0 1 2 3]\n", " [ 4 5 6 7]\n", " [ 8 9 10 11]\n", " [12 13 14 15]]\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "ux6ICpj6RVPx", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 104 }, "outputId": "12a20e02-4721-409b-a056-332edb3e88f7" }, "source": [ "upper, lower = np.vsplit(grid, [2])\n", "print(upper)\n", "print(\"------------------------\")\n", "\n", "print(lower)" ], "execution_count": 56, "outputs": [ { "output_type": "stream", "text": [ "[[0 1 2 3]\n", " [4 5 6 7]]\n", "------------------------\n", "[[ 8 9 10 11]\n", " [12 13 14 15]]\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "KipZb1mvRXUi", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 173 }, "outputId": "b2cc95c1-144c-4f97-d864-0be849616566" }, "source": [ "left, right = np.hsplit(grid, [2])\n", "print(left)\n", "print(\"------------------------\")\n", "\n", "print(right)" ], "execution_count": 57, "outputs": [ { "output_type": "stream", "text": [ "[[ 0 1]\n", " [ 4 5]\n", " [ 8 9]\n", " [12 13]]\n", "------------------------\n", "[[ 2 3]\n", " [ 6 7]\n", " [10 11]\n", " [14 15]]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "qkHN0JS7Re8a", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] } ] }