{ "cells": [ { "cell_type": "markdown", "id": "d9f819b9", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "# NumPy: n-dimensional arrays\n", "- Sum of elements and store in sum_of_matrix variable\n", "- Find max/min element in matrix and store in max_value/min_value variable\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "c74880a5", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 3, "id": "34acfbec", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "50 9 0\n" ] } ], "source": [ "matrix = np.array([[1, 5, 3], [9, 2, 6], [4, 8, 7], [0, 3, 2]])\n", "sum_of_matrix = np.sum(matrix)\n", "max_value, min_value = np.max(matrix), np.min(matrix)\n", "print(sum_of_matrix, max_value, min_value)" ] }, { "cell_type": "code", "execution_count": 5, "id": "72d76c18", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9 0\n" ] } ], "source": [ "matrix = np.array([[1, 5, 3], [9, 2, 6], [4, 8, 7], [0, 3, 2]])\n", "max_idx = np.unravel_index(np.argmax(matrix), matrix.shape)\n", "min_idx = np.unravel_index(np.argmin(matrix), matrix.shape)\n", "max_value = matrix[max_idx]\n", "min_value = matrix[min_idx]\n", "print(max_value, min_value)" ] }, { "cell_type": "markdown", "id": "4c94ba47", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Create Numpy array" ] }, { "cell_type": "markdown", "id": "ac905469", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### From List" ] }, { "cell_type": "markdown", "id": "25cbec63", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "- #### Create 1D array from list `[1, 2, 3, 4, 5]`\n", "- #### Create 2D array of shape 2x3 from list `[[1, 2, 3], [4, 5, 6]]`\n", "- #### Create 3D array of shape 2x2x3 from list `[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]`" ] }, { "cell_type": "code", "execution_count": 8, "id": "bb79d1bc", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list_1d: [1 2 3 4 5]\n", "list_2d: [[1 2 3]\n", " [4 5 6]]\n", "list_3d: [[[ 1 2 3]\n", " [ 4 5 6]]\n", "\n", " [[ 7 8 9]\n", " [10 11 12]]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "list_1d = np.array([1, 2, 3, 4, 5])\n", "list_2d = np.array([[1, 2, 3], [4, 5, 6]])\n", "list_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n", "print('list_1d: ', list_1d)\n", "print('list_2d: ', list_2d)\n", "print('list_3d: ', list_3d)" ] }, { "cell_type": "code", "execution_count": 9, "id": "7bc4dcf3", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "data": { "text/plain": [ "(dtype('int64'), (2, 2, 3), 3)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list_3d.dtype, list_3d.shape, list_3d.ndim" ] }, { "cell_type": "markdown", "id": "143f3e70", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Array Creation Functions" ] }, { "cell_type": "markdown", "id": "4bc1e5a5", "metadata": {}, "source": [ "### np.zeros\n", "\n", "`np.zeros(shape, dtype=float, order='C', *, like=None)`" ] }, { "cell_type": "code", "execution_count": 11, "id": "7f3c4ec5", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0.]\n", " [0. 0. 0.]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "zeros_matrix_2x3 = np.zeros((2,3))\n", "print(zeros_matrix_2x3)" ] }, { "cell_type": "markdown", "id": "8667cb1d", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### np.ones\n", "\n", "`np.ones(shape, dtype=None, order='C', *, like=None)`" ] }, { "cell_type": "code", "execution_count": 12, "id": "f1d8fcb4", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1. 1.]\n", " [1. 1. 1.]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "ones_matrix_2x3 = np.ones((2,3))\n", "print(ones_matrix_2x3)" ] }, { "cell_type": "markdown", "id": "3aeb0a51", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### np.full\n", "\n", "`np.full(shape, fill_value, dtype=None, order='C', *, like=None)`" ] }, { "cell_type": "code", "execution_count": 13, "id": "3b3f7ac4", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[9 9 9]\n", " [9 9 9]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "full_of_9_matrix = np.full((2,3), 9)\n", "print(full_of_9_matrix)" ] }, { "cell_type": "markdown", "id": "04acedab", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### np.arange\n", "\n", "`np.arange([start, ]stop, [step, ]dtype=None, *, like=None)`" ] }, { "cell_type": "code", "execution_count": 14, "id": "1d770e41", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4]\n" ] } ], "source": [ "import numpy as np\n", "\n", "arange_numpy = np.arange(start=0, stop=5, step=1)\n", "print(arange_numpy)" ] }, { "cell_type": "markdown", "id": "0df6e4fc", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### np.eye\n", "\n", "`np.eye(N, M=None, k=0, dtype=, order='C', *, like=None)`" ] }, { "cell_type": "code", "execution_count": 15, "id": "28203cc5", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 0. 0.]\n", " [0. 1. 0.]\n", " [0. 0. 1.]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "ones_in_diagonal_zeros_elsewhere = np.eye(3)\n", "print(ones_in_diagonal_zeros_elsewhere)" ] }, { "cell_type": "markdown", "id": "6361e2b9", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### np.random.random\n", "\n", "`np.random.random(size=None)`" ] }, { "cell_type": "markdown", "id": "da09f6ed", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "Return random floats in the half-open interval `[0.0, 1.0)`." ] }, { "cell_type": "code", "execution_count": 17, "id": "132dcc99", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.3511155 0.15105555 0.04231165]\n", " [0.38675743 0.85587757 0.31778496]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "my_arr = np.random.random((2,3))\n", "print(my_arr)" ] }, { "cell_type": "markdown", "id": "3d482457", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "## Important NumPy functions" ] }, { "cell_type": "markdown", "id": "5725c25c", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### np.clip\n", "\n", "`np.clip(a, a_min, a_max, out=None, **kwargs)`" ] }, { "cell_type": "markdown", "id": "df45bd17", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "Any values less than `a_min` are set to `a_min`, and any values greater than `a_max` are set to `a_max`." ] }, { "cell_type": "code", "execution_count": 19, "id": "98b90e1d", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3 5 8 8 3 6]\n" ] } ], "source": [ "import numpy as np\n", "\n", "arr = np.array([1,5,8,10,3,6])\n", "clipped_arr = np.clip(arr, a_min=3, a_max=8)\n", "print(clipped_arr)" ] }, { "cell_type": "markdown", "id": "f9153e66", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### np.concatenate\n", "\n", "`np.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting=\"same_kind\")`" ] }, { "cell_type": "code", "execution_count": 22, "id": "6610893b", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n" ] } ], "source": [ "import numpy as np\n", "\n", "arr1 = np.array([1,2,3])\n", "arr2 = np.array([4,5,6])\n", "concatenated = np.concatenate((arr1, arr2))\n", "print(concatenated)" ] }, { "cell_type": "markdown", "id": "e71e1e4d", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### Add more axes to the array" ] }, { "cell_type": "code", "execution_count": 24, "id": "acf89f32", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(5, 1) [[1]\n", " [2]\n", " [3]\n", " [4]\n", " [5]]\n", "(1, 5) [[1 2 3 4 5]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "arr = np.array([1,2,3,4,5])\n", "new_arr1 = arr[:, np.newaxis]\n", "new_arr2 = arr[np.newaxis, :]\n", "print(new_arr1.shape, new_arr1)\n", "print(new_arr2.shape, new_arr2)" ] }, { "cell_type": "markdown", "id": "7bc75608", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### np.vectorize\n", "\n", "`np.vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False, signature=None)`" ] }, { "cell_type": "markdown", "id": "6ce9b3fe", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "This function converts a Python function that operates on scalars into a vectorized function that can operate on NumPy arrays." ] }, { "cell_type": "code", "execution_count": 25, "id": "dda9ba9d", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 3.14159265 12.56637061 28.27433388 50.26548246]\n" ] } ], "source": [ "import numpy as np\n", "\n", "def calculate_circle_area(radius):\n", " return np.pi * (radius**2)\n", "\n", "radius_values = np.array([1,2,3,4])\n", "vectorized_func = np.vectorize(calculate_circle_area)\n", "area_values = vectorized_func(radius_values)\n", "print(area_values)" ] }, { "cell_type": "markdown", "id": "d450695b", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### np.where\n", "\n", "`np.where(condition, x=None, y=None, *, out=None)`" ] }, { "cell_type": "code", "execution_count": 26, "id": "a6853c80", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 0 0]\n" ] } ], "source": [ "import numpy as np\n", "\n", "arr = np.array([1,2,3,4,5])\n", "greater_than_3 = np.where(arr > 3, 0, arr)\n", "print(greater_than_3)" ] }, { "cell_type": "markdown", "id": "296b85ed", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "source": [ "### flatten\n", "\n", "`.flatten(order='C')`" ] }, { "cell_type": "code", "execution_count": 28, "id": "98a482cf", "metadata": { "tags": [ "hide-output", "scroll-output" ] }, "outputs": [], "source": [ "import numpy as np\n", "\n", "arr = np.array([[1, 2, 3], [4, 5, 6]])\n", "flattened_arr = arr.flatten()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 (WSL code-venv)", "language": "python", "name": "wsl-code-venv" }, "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }