X Tutup
{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "arr = np.random.rand(5,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### element-wise addition, subtraction, multiplication and division" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[10.76303985 10.06149651 10.57904337 10.8291741 10.34909157]\n", " [10.79875216 10.81553256 10.33263298 10.94754927 10.26929677]\n", " [10.91324204 10.36290234 10.47837507 10.11076725 10.1412222 ]\n", " [10.41560224 10.08529364 10.60372547 10.02257422 10.44915824]\n", " [10.35742146 10.74715734 10.65180952 10.9195904 10.02214632]]\n", "[[-9.23696015 -9.93850349 -9.42095663 -9.1708259 -9.65090843]\n", " [-9.20124784 -9.18446744 -9.66736702 -9.05245073 -9.73070323]\n", " [-9.08675796 -9.63709766 -9.52162493 -9.88923275 -9.8587778 ]\n", " [-9.58439776 -9.91470636 -9.39627453 -9.97742578 -9.55084176]\n", " [-9.64257854 -9.25284266 -9.34819048 -9.0804096 -9.97785368]]\n", "[[7.63039852 0.61496507 5.79043373 8.29174099 3.49091569]\n", " [7.98752158 8.15532564 3.32632975 9.47549269 2.69296773]\n", " [9.13242041 3.62902344 4.78375066 1.10767253 1.41222201]\n", " [4.15602235 0.85293636 6.03725474 0.22574224 4.49158239]\n", " [3.57421456 7.47157342 6.51809517 9.19590397 0.22146316]]\n", "[[0.07630399 0.00614965 0.05790434 0.08291741 0.03490916]\n", " [0.07987522 0.08155326 0.0332633 0.09475493 0.02692968]\n", " [0.0913242 0.03629023 0.04783751 0.01107673 0.01412222]\n", " [0.04156022 0.00852936 0.06037255 0.00225742 0.04491582]\n", " [0.03574215 0.07471573 0.06518095 0.09195904 0.00221463]]\n" ] } ], "source": [ "print(arr + 10)\n", "print(arr - 10)\n", "print(arr * 10)\n", "print(arr / 10)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 9 11 13]\n", "[1 2 3]\n", "[1 4 9]\n" ] } ], "source": [ "# the above operations can be performed using numpy built-in functions\n", "# which can save memory as the output can be stored in the original array rather than assigning new memory\n", "arr1 = np.array([1,2,3])\n", "np.add(arr1, [8,9,10], out=arr1)\n", "print(arr1)\n", "\n", "np.subtract(arr1, [8,9,10], out=arr1)\n", "print(arr1)\n", "\n", "np.multiply(arr1, [1,2,3], out=arr1)\n", "print(arr1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### element-wise exponentiation" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2.14478615 1.06342678 1.78433068 2.29142547 1.41777901]\n", " [2.22276554 2.26037915 1.39463534 2.57938054 1.30904357]\n", " [2.49238988 1.43749547 1.61345052 1.11713487 1.15168052]\n", " [1.51528302 1.0890368 1.82891972 1.02283095 1.5669926 ]\n", " [1.42963827 2.11099066 1.91901017 2.50826279 1.02239337]]\n" ] } ], "source": [ "print(np.exp(arr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### element-wise logorithm" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-0.27044502 -2.78877489 -0.54637789 -0.18732513 -1.05242102]\n", " [-0.22470457 -0.20391393 -1.10071557 -0.05387634 -1.31194126]\n", " [-0.09075433 -1.01362151 -0.7373602 -2.2003241 -1.95742073]\n", " [-0.87802664 -2.46165544 -0.5046357 -3.79094655 -0.80038003]\n", " [-1.02883965 -0.29147948 -0.42800291 -0.08382693 -3.81008411]]\n" ] } ], "source": [ "# natural log\n", "print(np.log(arr)) " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-0.39016969 -4.02335171 -0.78825668 -0.27025304 -1.51832258]\n", " [-0.32418017 -0.29418561 -1.5879969 -0.07772714 -1.89273115]\n", " [-0.13093082 -1.46234672 -1.0637859 -3.17439666 -2.82396118]\n", " [-1.26672468 -3.55141809 -0.72803542 -5.46917978 -1.1547043 ]\n", " [-1.48430185 -0.42051601 -0.61747768 -0.12093669 -5.49678945]]\n" ] } ], "source": [ "# base 2\n", "print(np.log2(arr)) " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-0.11745278 -1.21114955 -0.2372889 -0.08135427 -0.45706064]\n", " [-0.09758796 -0.08855869 -0.4780347 -0.0233982 -0.56976885]\n", " [-0.0394141 -0.44021023 -0.32023147 -0.95558861 -0.85009702]\n", " [-0.38132213 -1.06908337 -0.2191605 -1.64638717 -0.34760063]\n", " [-0.44681938 -0.12658793 -0.1858793 -0.03640557 -1.65469851]]\n" ] } ], "source": [ "# base 10\n", "print(np.log10(arr)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### element-wise square root" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.87352152 0.24798489 0.76094899 0.91058997 0.59083971]\n", " [0.89372935 0.90306842 0.57674342 0.97342142 0.51893812]\n", " [0.95563698 0.60241376 0.69164663 0.33281715 0.37579542]\n", " [0.64467219 0.29205074 0.77699773 0.15024721 0.67019269]\n", " [0.59784735 0.86438264 0.80734721 0.95895276 0.14881638]]\n" ] } ], "source": [ "print(np.sqrt(arr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### element-wise sine and cosine" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.69112165 0.06145775 0.5472235 0.73737374 0.34204431]\n", " [0.71648615 0.72809076 0.32653281 0.81198751 0.26605362]\n", " [0.79148938 0.35498904 0.46033726 0.11054088 0.14075325]\n", " [0.40374097 0.08519025 0.56771331 0.02257231 0.43420742]\n", " [0.34985981 0.67955607 0.60662594 0.79535341 0.02214451]]\n" ] } ], "source": [ "print(np.sin(arr))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.72273845 0.99810969 0.83698652 0.67548499 0.93968382]\n", " [0.69760131 0.68548074 0.94518587 0.5836748 0.96395823]\n", " [0.61118293 0.93487046 0.88774411 0.99387158 0.99004471]\n", " [0.91487334 0.9963647 0.82322633 0.99974521 0.90081292]\n", " [0.93680207 0.73362358 0.7949874 0.60614599 0.99975478]]\n" ] } ], "source": [ "print(np.cos(arr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### sum along a specified axis" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3.24805774 2.07238239 2.64558641 2.82965524 1.2309151 ]\n" ] } ], "source": [ "# sum along the row\n", "print(np.sum(arr, axis=0)) " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2.5818454 3.16376374 2.0065089 1.57635381 2.69812503]\n" ] } ], "source": [ "# sum along the column\n", "print(np.sum(arr, axis=1)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the min and max along a specified axis" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.35742146 0.06149651 0.33263298 0.02257422 0.02214632]\n" ] } ], "source": [ "# calculate min along the row\n", "print(np.min(arr, axis=0))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.8291741 0.94754927 0.91324204 0.60372547 0.9195904 ]\n" ] } ], "source": [ "# calculate max along the column\n", "print(np.max(arr, axis=1)) " ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9475492686719678\n", "0.022146316110606912\n" ] } ], "source": [ "# if axis not specified, calculate the max/min value of all elements\n", "print(np.max(arr))\n", "print(np.min(arr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the indices of the min and max along a specified axis" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4 0 1 3 4]\n", "[2 1 4 1 3]\n" ] } ], "source": [ "# along the row\n", "print(np.argmin(arr, axis=0))\n", "print(np.argmax(arr, axis=0))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 4 3 3 4]\n", "[3 3 0 2 3]\n" ] } ], "source": [ "# along the column\n", "print(np.argmin(arr, axis=1))\n", "print(np.argmax(arr, axis=1))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "24\n", "8\n" ] } ], "source": [ "# if axis not specified, return the index of the flattened array\n", "print(np.argmin(arr))\n", "print(np.argmax(arr))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute element-wise min and max of two arrays" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 4 5 8 9]\n", "[0 3 3 7 7]\n" ] } ], "source": [ "arr1 = np.array([1, 3, 5, 7, 9])\n", "arr2 = np.array([0, 4, 3, 8, 7])\n", "print(np.maximum(arr1, arr2))\n", "print(np.minimum(arr1, arr2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### split fractional and integral parts of a floating-point array" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "fractional: [0.21779326 0.58882555 0.32195097 0.45853207 0.50994103 0.42358094\n", " 0.32348379 0.31232886 0.16890174 0.49021443]\n", "integral: [0. 0. 5. 5. 5. 1. 2. 1. 6. 8.]\n" ] } ], "source": [ "arr1 = np.random.rand(10) * 10\n", "re, intg = np.modf(arr1)\n", "print('fractional: ', re)\n", "print('integral: ', intg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the mean" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.4810638752799312\n" ] } ], "source": [ "# compute the overall mean\n", "print(np.mean(arr))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.64961155 0.41447648 0.52911728 0.56593105 0.24618302]\n" ] } ], "source": [ "# compute the mean along the row\n", "print(np.mean(arr, axis=0)) " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.51636908 0.63275275 0.40130178 0.31527076 0.53962501]\n" ] } ], "source": [ "# compute the mean along the column\n", "print(np.mean(arr, axis=1)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the median" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.4491582392434029\n" ] } ], "source": [ "# compute the overall median\n", "print(np.median(arr))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.76303985 0.36290234 0.57904337 0.8291741 0.26929677]\n" ] } ], "source": [ "# compute the median along the row\n", "print(np.median(arr, axis=0)) " ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.57904337 0.79875216 0.36290234 0.41560224 0.65180952]\n" ] } ], "source": [ "# compute the median along the column\n", "print(np.median(arr, axis=1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the percentile" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.02821037 0.5914621 0.89273939]\n" ] } ], "source": [ "arr1 = np.random.rand(100)\n", "# compute 5, 65, and 95 percentiles of the array\n", "print(np.percentile(arr1, [5, 65, 95]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the standard deviation & variance" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3006485476445816\n" ] } ], "source": [ "# compute the overall standard deviation\n", "print(np.std(arr))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.22124437 0.3184772 0.11339598 0.41046681 0.15068681]\n" ] } ], "source": [ "# compute the standard deviation along the row\n", "print(np.std(arr, axis=0))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.28196182 0.27648987 0.29036161 0.22349905 0.31654733]\n" ] } ], "source": [ "# compute the standard deviation along the column\n", "print(np.std(arr, axis=1))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.09038954920079624\n" ] } ], "source": [ "# compute the overall variance\n", "print(np.var(arr))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.04894907 0.10142773 0.01285865 0.168483 0.02270651]\n" ] } ], "source": [ "# compute the variance along the row\n", "print(np.var(arr, axis=0))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.07950247 0.07644665 0.08430987 0.04995182 0.10020221]\n" ] } ], "source": [ "# compute the variance along the column\n", "print(np.var(arr, axis=1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the covariance & correlation" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "arr = np.random.rand(5,8)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0.0730498 0.04997737 -0.00836842 0.00865103 -0.03076003]\n", " [ 0.04997737 0.06921278 -0.02750488 0.0401044 -0.04116094]\n", " [-0.00836842 -0.02750488 0.05298148 -0.04966884 0.03063382]\n", " [ 0.00865103 0.0401044 -0.04966884 0.06762098 -0.02100972]\n", " [-0.03076003 -0.04116094 0.03063382 -0.02100972 0.07502637]]\n" ] } ], "source": [ "print(np.cov(arr))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 0.11028402]\n", " [0.11028402 1. ]]\n" ] } ], "source": [ "print(np.corrcoef(arr[:,0], arr[:,1]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute cumulative sum & product" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.22630778 0.767801 0.58288104 0.91212964 0.16866086 0.71225777\n", " 0.75992693 0.43036535]\n", " [0.59072776 1.41363555 1.26836278 1.39281883 0.17541256 1.09197627\n", " 1.56096648 0.6397989 ]\n", " [0.86915997 1.77345229 1.64305337 2.01662839 0.70369834 1.28990428\n", " 1.86675912 1.54481741]\n", " [1.73953799 2.39132679 2.30715387 2.2988209 0.89970976 2.05231818\n", " 2.61580011 1.82762798]\n", " [2.73272009 3.02599488 2.8500615 3.11255427 1.53154629 2.58196071\n", " 2.74551764 2.76644411]]\n" ] } ], "source": [ "# calculate the cumulative sums along the row\n", "print(np.cumsum(arr, axis=0)) " ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.22630778 0.99410878 1.57698982 2.48911946 2.65778031 3.37003808\n", " 4.12996501 4.56033036]\n", " [0.36441999 1.01025454 1.69573627 2.17642546 2.18317717 2.56289567\n", " 3.36393522 3.57336877]\n", " [0.2784322 0.63824894 1.01293953 1.6367491 2.16503487 2.36296289\n", " 2.66875553 3.57377405]\n", " [0.87037803 1.48825253 2.15235303 2.43454554 2.63055696 3.39297085\n", " 4.14201184 4.42482241]\n", " [0.9931821 1.62785019 2.17075782 2.98449119 3.61632772 4.14597025\n", " 4.27568777 5.2145039 ]]\n" ] } ], "source": [ "# calculate the cumulative sums along the column\n", "print(np.cumsum(arr, axis=1)) " ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2.26307776e-01 7.67801004e-01 5.82881038e-01 9.12129639e-01\n", " 1.68660858e-01 7.12257767e-01 7.59926931e-01 4.30365348e-01]\n", " [8.24710772e-02 4.95872414e-01 3.99554306e-01 4.38450855e-01\n", " 1.13874845e-03 2.70457451e-01 6.08731528e-01 9.01329430e-02]\n", " [2.29626035e-02 1.78423193e-01 1.49709241e-01 2.73509837e-01\n", " 6.01584614e-04 5.35311066e-02 1.86145622e-01 8.15719823e-02]\n", " [1.99861456e-02 1.10243142e-01 9.94219814e-02 7.71824276e-02\n", " 1.17917453e-04 4.08128596e-02 1.39430701e-01 2.30694183e-02]\n", " [1.98498821e-02 6.99678041e-02 5.39769526e-02 6.28059168e-02\n", " 7.45045544e-05 2.16162260e-02 1.80866051e-02 2.16579420e-02]]\n" ] } ], "source": [ "# calculate the cumulative product along the row\n", "print(np.cumprod(arr, axis=0)) " ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[2.26307776e-01 1.73759338e-01 1.01281023e-01 9.23814230e-02\n", " 1.55811300e-02 1.10977809e-02 8.43350257e-03 3.62948727e-03]\n", " [3.64419989e-01 2.35355019e-01 1.61331567e-01 7.75503399e-02\n", " 5.23597062e-04 1.98819491e-04 1.59262276e-04 3.33548640e-05]\n", " [2.78432201e-01 1.00184566e-01 3.75382145e-02 2.34166972e-02\n", " 1.23707081e-02 2.44850971e-03 7.48736253e-04 6.77620172e-04]\n", " [8.70378028e-01 5.37784391e-01 3.57142882e-01 1.00783047e-01\n", " 1.97546278e-02 1.50612027e-02 1.12814582e-02 3.19051555e-03]\n", " [9.93182100e-01 6.30340985e-01 3.42216932e-01 2.78473337e-01\n", " 1.75949628e-01 9.31904051e-02 1.20884285e-02 1.13488117e-02]]\n" ] } ], "source": [ "# calculate the cumulative product along the column\n", "print(np.cumprod(arr, axis=1)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### element-wise comparison" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "arr1 = np.array([1,2,3,4,5])\n", "arr2 = np.array([5,4,3,2,1])" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False False True False False]\n", "[ True True False False False]\n" ] } ], "source": [ "# return an array of bools\n", "print(arr1 == arr2) \n", "print(arr1 < 3)" ] } ], "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