{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b4db2677-a229-40ea-bb5b-d5064390c906", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "id": "5e6e64f1-5820-4907-90f1-d314cebd8b21", "metadata": {}, "source": [ "### Creating Arrays" ] }, { "cell_type": "code", "execution_count": 2, "id": "7b612c05-8510-4c75-98b9-aa1dd82638ce", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5]\n", "\n" ] } ], "source": [ "arr = np.array([1, 2, 3, 4, 5])\n", "\n", "print(arr)\n", "\n", "print(type(arr))" ] }, { "cell_type": "markdown", "id": "ee4dfe3a-d0bc-44e8-8c13-c81b669fe029", "metadata": {}, "source": [ "### Access Array Elements\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "46ed2f18-325e-433f-b8e8-81a7fcb55a68", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "arr = np.array([1, 2, 3, 4])\n", "\n", "print(arr[0])" ] }, { "cell_type": "markdown", "id": "577d8ad4-1ea1-4766-95d0-eca9eaa2e139", "metadata": {}, "source": [ "### Access 2-D Arrays" ] }, { "cell_type": "code", "execution_count": 4, "id": "f611eb2f-1a51-4922-98cc-e0ca9c23d70c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2nd element on 1st row: 2\n" ] } ], "source": [ "arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])\n", "\n", "print('2nd element on 1st row: ', arr[0, 1])" ] }, { "cell_type": "markdown", "id": "039dd714-035a-47ff-9189-4738d0439a25", "metadata": {}, "source": [ "### Slicing arrays" ] }, { "cell_type": "code", "execution_count": 5, "id": "d4b7ab6b-337b-43cc-94a3-80286023cb64", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 3 4 5]\n" ] } ], "source": [ "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", "\n", "print(arr[1:5])" ] }, { "cell_type": "code", "execution_count": 6, "id": "9f4155d9-85be-412b-a60a-d3d501b3f37e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5 6 7]\n" ] } ], "source": [ "print(arr[4:])" ] }, { "cell_type": "code", "execution_count": 7, "id": "c2a24299-e29a-4ac4-a972-b6c29825974e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4]\n" ] } ], "source": [ "print(arr[:4])" ] }, { "cell_type": "code", "execution_count": 13, "id": "1fb6581f-012f-4e76-92f4-e8334e9d72e1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 4]\n" ] } ], "source": [ "# step -> index 1 to index 5, max 2 elements\n", "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", "\n", "print(arr[1:5:2])" ] }, { "cell_type": "code", "execution_count": 14, "id": "68dc5552-2f13-40dd-9bd6-3db60f025063", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[7 8 9]\n" ] } ], "source": [ "# 2 dim array\n", "arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])\n", "\n", "print(arr[1, 1:4])" ] }, { "cell_type": "markdown", "id": "dbeb9676-d6cf-44f2-83a5-18fc2cfc1999", "metadata": {}, "source": [ "### Data-Types " ] }, { "cell_type": "markdown", "id": "79240bea-6521-4bcb-b630-7bf61e62ce69", "metadata": {}, "source": [ "#### Python\n", "- strings - used to represent text data, the text is given under quote marks. e.g. \"ABCD\"\n", "- integer - used to represent integer numbers. e.g. -1, -2, -3\n", "- float - used to represent real numbers. e.g. 1.2, 42.42\n", "- boolean - used to represent True or False.\n", "- complex - used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j\n", "\n", "#### Numpy\n", "- i - integer\n", "- b - boolean\n", "- u - unsigned integer\n", "- f - float\n", "- c - complex float\n", "- m - timedelta\n", "- M - datetime\n", "- O - object\n", "- S - string\n", "- U - unicode string\n", "- V - fixed chunk of memory for other type ( void )\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "0be7eb7c-1f64-400a-88f4-6104bb778646", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "int64\n" ] } ], "source": [ "arr = np.array([1, 2, 3, 4])\n", "\n", "print(arr.dtype)" ] }, { "cell_type": "code", "execution_count": 17, "id": "ccf3a0c3-7132-4800-9dc3-2c7966ddf0e8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " n-for loops\n", "arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])\n", "\n", "for x in np.nditer(arr):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 39, "id": "3b2ca7d7-28e7-429d-9c1b-a35ae789f50f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3\n", "5\n", "7\n" ] } ], "source": [ "# different step size\n", "arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", "# Iterate through every scalar element of the 2D array skipping 1 element:\n", "for x in np.nditer(arr[:, ::2]):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 40, "id": "b3257129-9727-4647-84e6-ac985580fe73", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 0) 1\n", "(0, 1) 2\n", "(0, 2) 3\n", "(0, 3) 4\n", "(1, 0) 5\n", "(1, 1) 6\n", "(1, 2) 7\n", "(1, 3) 8\n" ] } ], "source": [ "#ndenumerate --> returns index too\n", "arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", "\n", "for idx, x in np.ndenumerate(arr):\n", " print(idx, x)" ] }, { "cell_type": "markdown", "id": "299e02df-ed56-4c1c-a4c8-779a0a71f97a", "metadata": {}, "source": [ "### Joining Arrays" ] }, { "cell_type": "code", "execution_count": 42, "id": "72b19370-5292-4987-b79e-bd8772683bea", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n" ] } ], "source": [ "arr1 = np.array([1, 2, 3])\n", "\n", "arr2 = np.array([4, 5, 6])\n", "\n", "arr = np.concatenate((arr1, arr2))\n", "\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 43, "id": "bb39d7aa-a1ca-41a4-899e-193610ea4bd8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 5 6]\n", " [3 4 7 8]]\n" ] } ], "source": [ "#2 dim array\n", "arr1 = np.array([[1, 2], [3, 4]])\n", "\n", "arr2 = np.array([[5, 6], [7, 8]])\n", "\n", "arr = np.concatenate((arr1, arr2), axis=1)\n", "\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 45, "id": "177a8c9a-2cf8-40a9-bed0-39436910c45b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 4]\n", " [2 5]\n", " [3 6]]\n" ] } ], "source": [ "# stack\n", "arr1 = np.array([1, 2, 3])\n", "\n", "arr2 = np.array([4, 5, 6])\n", "\n", "arr = np.stack((arr1, arr2), axis=1)\n", "\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 46, "id": "d3519009-19ac-4175-913e-8c5d47b1896f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n" ] } ], "source": [ "# Stacking Along Rows\n", "arr1 = np.array([1, 2, 3])\n", "\n", "arr2 = np.array([4, 5, 6])\n", "\n", "arr = np.hstack((arr1, arr2))\n", "\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 47, "id": "d3a2b831-6e4d-4caa-b270-372958b70d49", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]]\n" ] } ], "source": [ "# Stacking Along Columns\n", "arr1 = np.array([1, 2, 3])\n", "\n", "arr2 = np.array([4, 5, 6])\n", "\n", "arr = np.vstack((arr1, arr2))\n", "\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 48, "id": "64a57420-2507-4abd-b573-d9976d2bb385", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[1 4]\n", " [2 5]\n", " [3 6]]]\n" ] } ], "source": [ "# Stacking Along Height (depth)\n", "arr1 = np.array([1, 2, 3])\n", "\n", "arr2 = np.array([4, 5, 6])\n", "\n", "arr = np.dstack((arr1, arr2))\n", "\n", "print(arr)" ] }, { "cell_type": "markdown", "id": "7b226ad5-addb-4ee1-93d4-748df1de2e85", "metadata": {}, "source": [ "### Splitting Arrays" ] }, { "cell_type": "code", "execution_count": 51, "id": "f357a0b4-7dbf-4cd0-b586-23e06c882f6d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[array([1, 2]), array([3, 4]), array([5, 6])]\n" ] } ], "source": [ "# The return value is a list containing three arrays.\n", "arr = np.array([1, 2, 3, 4, 5, 6])\n", "\n", "newarr = np.array_split(arr, 3)\n", "\n", "print(newarr)" ] }, { "cell_type": "code", "execution_count": 53, "id": "437b8f01-1309-48c5-a4ef-13dd26f73025", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[array([[1, 2],\n", " [3, 4]]), array([[5, 6],\n", " [7, 8]]), array([[ 9, 10],\n", " [11, 12]])]\n" ] } ], "source": [ "# splitting 2dim arrays\n", "arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])\n", "\n", "newarr = np.array_split(arr, 3)\n", "\n", "print(newarr)" ] }, { "cell_type": "markdown", "id": "54416dbe-202c-461c-9417-79043e44207f", "metadata": {}, "source": [ "### Searching Arrays" ] }, { "cell_type": "code", "execution_count": 55, "id": "c3dc56a2-9928-40b1-9e89-0f61e22efc11", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array([3, 5, 6]),)\n" ] } ], "source": [ "arr = np.array([1, 2, 3, 4, 5, 4, 4])\n", "\n", "x = np.where(arr == 4)\n", "\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 56, "id": "b07a60bd-ddb1-45b8-bae8-452b4140133b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(array([1, 3, 5, 7]),)\n" ] } ], "source": [ "# Find the indexes where the values are even (modulo)\n", "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", "\n", "x = np.where(arr%2 == 0)\n", "\n", "print(x)" ] }, { "cell_type": "markdown", "id": "0400ef9f-09f4-4e8d-b183-56e401fa8a5a", "metadata": {}, "source": [ "#### Sorting Arrays" ] }, { "cell_type": "code", "execution_count": 61, "id": "ad6ff4df-0260-46c6-ace8-f716baaa81ff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3]\n" ] } ], "source": [ "arr = np.array([3, 2, 0, 1])\n", "sorted_array = np.sort(arr)\n", "print(sorted_array)" ] }, { "cell_type": "code", "execution_count": 62, "id": "2f51f21d-4ddc-4677-907f-07038c4e3a80", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3 2 1 0]\n" ] } ], "source": [ "# reverse\n", "reverse_array = sorted_array[::-1]\n", "print(reverse_array)" ] }, { "cell_type": "code", "execution_count": 59, "id": "7264d51f-6d8b-4571-91ef-d8550b539d86", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple' 'banana' 'cherry']\n" ] } ], "source": [ "arr = np.array(['banana', 'cherry', 'apple'])\n", "\n", "print(np.sort(arr))" ] }, { "cell_type": "code", "execution_count": 60, "id": "056524c9-a534-4b82-bb7f-4772efb94296", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2 3 4]\n", " [0 1 5]]\n" ] } ], "source": [ "# 2-dim\n", "arr = np.array([[3, 2, 4], [5, 0, 1]])\n", "\n", "print(np.sort(arr))" ] }, { "cell_type": "markdown", "id": "f5a6b9f1-6a39-4f3b-8a98-a45bb6a803fb", "metadata": {}, "source": [ "### Filter Arrays" ] }, { "cell_type": "code", "execution_count": 64, "id": "101256a3-98a7-4ee8-bb6d-5386a42043f8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False False True True]\n", "[43 44]\n" ] } ], "source": [ "arr = np.array([41, 42, 43, 44])\n", "\n", "filter_arr = arr > 42\n", "\n", "newarr = arr[filter_arr]\n", "\n", "print(filter_arr)\n", "print(newarr)" ] }, { "cell_type": "code", "execution_count": 65, "id": "79b7bfaf-1056-42f8-948e-bb6c2811a25a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False True False True False True False]\n", "[2 4 6]\n" ] } ], "source": [ "arr = np.array([1, 2, 3, 4, 5, 6, 7])\n", "\n", "filter_arr = arr % 2 == 0\n", "\n", "newarr = arr[filter_arr]\n", "\n", "print(filter_arr)\n", "print(newarr)" ] }, { "cell_type": "code", "execution_count": null, "id": "d8fcf398-28f3-4380-8aab-7692578ab277", "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.10.8" } }, "nbformat": 4, "nbformat_minor": 5 }