X Tutup
{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array from an iterable\n", "Such as\n", "- ```list```\n", "- ```tuple```\n", "- ```range``` iterator\n", "\n", "Notice that not all iterables can be used to create a numpy array, such as ```set``` and ```dict```" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5]\n" ] } ], "source": [ "arr = np.array([1,2,3,4,5])\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5]\n" ] } ], "source": [ "arr = np.array((1,2,3,4,5))\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5 6 7 8 9]\n" ] } ], "source": [ "arr = np.array(range(10))\n", "print(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array with specified data type" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]]\n", "Data Type: int16\n" ] } ], "source": [ "arr = np.array([[1,2,3], [4,5,6]], dtype='i2')\n", "print(arr)\n", "print('Data Type: ' + str(arr.dtype))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an aray within specified range\n", "```np.arange()``` method can be used to replace ```np.array(range())``` method" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0 2 4 6 8 10 12 14 16 18]\n" ] } ], "source": [ "# np.arange(start, stop, step)\n", "arr = np.arange(0, 20, 2) \n", "print(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array of evenly spaced numbers within specified range\n", "```np.linspace(start, stop, num_of_elements, endpoint=True, retstep=False)``` has 5 parameters:\n", "- ```start```: start number (inclusive)\n", "- ```stop```: end number (inclusive unless ```endpoint``` set to ```False```)\n", "- ```num_of_elements```: number of elements contained in the array\n", "- ```endpoint```: boolean value representing whether the ```stop``` number is inclusive or not\n", "- ```retstep```: boolean value representing whether to return the step size" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.625 1.25 1.875 2.5 3.125 3.75 4.375]\n", "The step size is 0.625\n" ] } ], "source": [ "arr, step_size = np.linspace(0, 5, 8, endpoint=False, retstep=True)\n", "print(arr)\n", "print('The step size is ' + str(step_size))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array of random values of given shape\n", "```np.random.rand()``` method returns values in the range [0,1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[9.73081534e-01 7.63102228e-01 8.35216521e-02]\n", " [1.06040873e-02 8.12409335e-01 3.48074349e-01]\n", " [9.32181543e-01 3.51751631e-06 8.69013365e-01]]\n" ] } ], "source": [ "arr = np.random.rand(3, 3)\n", "print(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array of zeros of given shape \n", "- ```np.zeros()```: create array of all zeros in given shape\n", "- ```np.zeros_like()```: create array of all zeros with the same shape and data type as the given input array" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0.]\n", " [0. 0. 0.]]\n" ] } ], "source": [ "zeros = np.zeros((2,3))\n", "print(zeros)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.+0.j 0.+0.j]\n", " [0.+0.j 0.+0.j]\n", " [0.+0.j 0.+0.j]]\n", "Data Type: complex64\n" ] } ], "source": [ "arr = np.array([[1,2], [3,4],[5,6]], dtype=np.complex64)\n", "zeros = np.zeros_like(arr)\n", "print(zeros)\n", "print('Data Type: ' + str(zeros.dtype))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array of ones of given shape \n", "- ```np.ones()```: create array of all ones in given shape\n", "- ```np.ones_like()```: create array of all ones with the same shape and data type as the given input array" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 1.]\n", " [1. 1.]\n", " [1. 1.]]\n" ] } ], "source": [ "ones = np.ones((3,2))\n", "print(ones)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 1 1]\n", " [1 1 1]]\n", "Data Type: int32\n" ] } ], "source": [ "arr = [[1,2,3], [4,5,6]]\n", "ones = np.ones_like(arr)\n", "print(ones)\n", "print('Data Type: ' + str(ones.dtype))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an empty array of given shape \n", "- ```np.empty()```: create array of empty values in given shape\n", "- ```np.empty_like()```: create array of empty values with the same shape and data type as the given input array\n", "\n", "Notice that the initial values are not necessarily set to zeroes.\n", "\n", "They are just some garbage values in random memory addresses." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[6.23042070e-307 4.67296746e-307 1.69121096e-306 7.56605919e-307\n", " 1.89146896e-307]\n", " [7.56571288e-307 3.11525958e-307 1.24610723e-306 1.33511969e-306\n", " 1.86921143e-306]\n", " [1.29060531e-306 8.45599366e-307 7.56593017e-307 1.11261027e-306\n", " 1.11261502e-306]\n", " [1.42410839e-306 7.56597770e-307 6.23059726e-307 1.42419530e-306\n", " 7.56602523e-307]\n", " [1.29061821e-306 1.11260144e-306 6.89812281e-307 2.22522596e-306\n", " 2.56761491e-312]]\n" ] } ], "source": [ "empty = np.empty((5,5))\n", "print(empty)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1065353216 1073741824 1077936128]\n", " [1082130432 1084227584 1086324736]]\n", "Data Type: int64\n" ] } ], "source": [ "arr = np.array([[1,2,3], [4,5,6]], dtype=np.int64)\n", "empty = np.empty_like(arr)\n", "print(empty)\n", "print('Data Type: ' + str(empty.dtype))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array of constant values of given shape \n", "- ```np.full()```: create array of constant values in given shape\n", "- ```np.full_like()```: create array of constant values with the same shape and data type as the given input array" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[5 5 5 5]\n", " [5 5 5 5]\n", " [5 5 5 5]\n", " [5 5 5 5]]\n" ] } ], "source": [ "full = np.full((4,4), 5)\n", "print(full)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[5. 5.]\n", " [5. 5.]]\n", "Data Type: float64\n" ] } ], "source": [ "arr = np.array([[1,2], [3,4]], dtype=np.float64)\n", "full = np.full_like(arr, 5)\n", "print(full)\n", "print('Data Type: ' + str(full.dtype))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array in a repetitive manner\n", "- ```np.repeat(iterable, reps, axis=None)```: repeat each element by n times\n", " - ```iterable```: input array\n", " - ```reps```: number of repetitions\n", " - ```axis```: which axis to repeat along, default is ```None``` which will flatten the input array and then repeat\n", "- ```np.tile()```: repeat the whole array by n times\n", " - ```iterable```: input array\n", " - ```reps```: number of repetitions, it can be a tuple to represent repetitions along x-axis and y-axis" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5]\n" ] } ], "source": [ "# No axis specified, then flatten the input array first and repeat\n", "arr = [[0, 1, 2], [3, 4, 5]]\n", "print(np.repeat(arr, 3)) " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [0 1 2]\n", " [0 1 2]\n", " [3 4 5]\n", " [3 4 5]\n", " [3 4 5]]\n" ] } ], "source": [ "# An example of repeating along x-axis\n", "arr = [[0, 1, 2], [3, 4, 5]]\n", "print(np.repeat(arr, 3, axis=0)) " ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 0 0 1 1 1 2 2 2]\n", " [3 3 3 4 4 4 5 5 5]]\n" ] } ], "source": [ "# An example of repeating along y-axis\n", "arr = [[0, 1, 2], [3, 4, 5]]\n", "print(np.repeat(arr, 3, axis=1)) " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 0 1 2 0 1 2]\n" ] } ], "source": [ "# Repeat the whole array by a specified number of times\n", "arr = [0, 1, 2]\n", "print(np.tile(arr, 3))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2 0 1 2]\n", " [0 1 2 0 1 2]]\n" ] } ], "source": [ "# Repeat along specified axes\n", "print(np.tile(arr, (2,2)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an identity matrix of given size\n", "- ```np.eye(size, k=0)```: create an identity matrix of given size\n", " - ```size```: the size of the identity matrix\n", " - ```k```: the diagonal offset\n", "- ```np.identity()```: same as ```np.eye()``` but does not carry parameters" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 0. 0. 0. 0.]\n", " [0. 1. 0. 0. 0.]\n", " [0. 0. 1. 0. 0.]\n", " [0. 0. 0. 1. 0.]\n", " [0. 0. 0. 0. 1.]]\n" ] } ], "source": [ "identity_matrix = np.eye(5)\n", "print(identity_matrix)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0. 0. 0.]\n", " [1. 0. 0. 0. 0.]\n", " [0. 1. 0. 0. 0.]\n", " [0. 0. 1. 0. 0.]\n", " [0. 0. 0. 1. 0.]]\n" ] } ], "source": [ "# An example of diagonal offset\n", "identity_matrix = np.eye(5, k=-1)\n", "print(identity_matrix)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 0. 0. 0. 0.]\n", " [0. 1. 0. 0. 0.]\n", " [0. 0. 1. 0. 0.]\n", " [0. 0. 0. 1. 0.]\n", " [0. 0. 0. 0. 1.]]\n" ] } ], "source": [ "identity_matrix = np.identity(5)\n", "print(identity_matrix)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array with given values on the diagonal" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.04481892 0.32920508 0.09746329 0.88717729 0.73704537]\n", " [0.99842459 0.3842842 0.55263992 0.76554596 0.55017239]\n", " [0.48540154 0.85297201 0.82429168 0.27125055 0.79634355]\n", " [0.41327049 0.68932437 0.10586385 0.48118436 0.476337 ]\n", " [0.05556156 0.27141898 0.11705936 0.83691925 0.49439961]]\n", "Values on the diagonal: [0.04481892 0.3842842 0.82429168 0.48118436 0.49439961]\n" ] } ], "source": [ "arr = np.random.rand(5,5)\n", "print(arr)\n", "# Extract values on the diagonal\n", "print('Values on the diagonal: ' + str(np.diag(arr)))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.44008485 0.19254758 0.65707551]\n", " [0.06838026 0.43745833 0.22630033]\n", " [0.86801201 0.65430091 0.93617918]\n", " [0.72927673 0.61550749 0.87328398]\n", " [0.11083138 0.15582005 0.54587358]\n", " [0.80298985 0.89705779 0.18045105]\n", " [0.93153603 0.84343715 0.39326583]\n", " [0.29509183 0.49859358 0.32320668]\n", " [0.13389313 0.6620257 0.50146751]\n", " [0.69110925 0.97077549 0.11649333]]\n", "Values on the diagonal: [0.44008485 0.43745833 0.93617918]\n" ] } ], "source": [ "# Not necessarily to be a square matrix\n", "arr = np.random.rand(10,3)\n", "print(arr)\n", "# Extract values on the diagonal\n", "print('Values on the diagonal: ' + str(np.diag(arr)))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 0 0 0 0]\n", " [0 2 0 0 0]\n", " [0 0 3 0 0]\n", " [0 0 0 4 0]\n", " [0 0 0 0 5]]\n" ] } ], "source": [ "# Create a matrix given values on the diagonal\n", "# All non-diagonal values set to zeros\n", "arr = np.diag([1,2,3,4,5])\n", "print(arr)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }
X Tutup