From 8ac6051c54b8aa1ad744e98ad3bb7153cdcdc32e Mon Sep 17 00:00:00 2001 From: David Hoese <david.hoese@ssec.wisc.edu> Date: Sun, 8 Sep 2019 17:04:39 -0500 Subject: [PATCH] Add np.clip and other cleanups --- slideshow.ipynb | 222 ++++++++++++++++++++++++++++-------------------- 1 file changed, 131 insertions(+), 91 deletions(-) diff --git a/slideshow.ipynb b/slideshow.ipynb index 53bd260..bfbf1d1 100644 --- a/slideshow.ipynb +++ b/slideshow.ipynb @@ -10,7 +10,9 @@ "source": [ "# NumPy - A to Z\n", "\n", - "By: David Hoese (@djhoese)" + "By: David Hoese (@djhoese)\n", + "\n", + "**https://gitlab.ssec.wisc.edu/ssec-dev/numpy-atoz**" ] }, { @@ -30,24 +32,13 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3]" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_arr = [1, 2, 3]\n", "my_arr" @@ -55,24 +46,13 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [ - { - "data": { - "text/plain": [ - "[6, 7, 8]" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "result = []\n", "for val in my_arr:\n", @@ -856,7 +836,8 @@ " 7.29166667, 7.75 , 8.20833333, 8.66666667, 9.125 ,\n", " 9.58333333, 10.04166667, 10.5 , 10.95833333, 11.41666667,\n", " 11.875 , 12.33333333, 12.79166667, 13.25 , 13.70833333,\n", - " 14.16666667, 14.625 , 15.08333333, 15.54166667, 16. ]), 0.4583333333333333)" + " 14.16666667, 14.625 , 15.08333333, 15.54166667, 16. ]),\n", + " 0.4583333333333333)" ] }, "execution_count": 32, @@ -1099,8 +1080,8 @@ { "data": { "text/plain": [ - "array([[0.41545787, 0.0157101 ],\n", - " [0.54952562, 0.38949899]])" + "array([[0.79931986, 0.74795771],\n", + " [0.12213411, 0.79055517]])" ] }, "execution_count": 41, @@ -1125,7 +1106,7 @@ { "data": { "text/plain": [ - "7" + "5" ] }, "execution_count": 42, @@ -1149,8 +1130,8 @@ { "data": { "text/plain": [ - "array([[13, 10, 14],\n", - " [ 5, 5, 13]])" + "array([[ 8, 7, 6],\n", + " [12, 14, 6]])" ] }, "execution_count": 43, @@ -2203,11 +2184,11 @@ { "data": { "text/plain": [ - "array([[ True, False, True, True, True, False, False, False],\n", + "array([[False, True, False, False, False, True, True, False],\n", + " [False, False, True, False, True, True, False, False],\n", " [False, False, False, False, False, False, False, False],\n", - " [False, False, False, False, True, False, False, False],\n", - " [False, True, False, False, False, False, False, False],\n", - " [False, True, False, False, True, False, True, True]])" + " [ True, True, False, True, False, False, False, False],\n", + " [ True, False, False, True, False, True, False, False]])" ] }, "execution_count": 82, @@ -2231,7 +2212,7 @@ { "data": { "text/plain": [ - "array([ 0, 2, 3, 4, 20, 25, 33, 36, 38, 39])" + "array([ 1, 5, 6, 10, 12, 13, 24, 25, 27, 32, 35, 37])" ] }, "execution_count": 83, @@ -2255,7 +2236,7 @@ { "data": { "text/plain": [ - "array([ 0, 4, 6, 8, 40, 50, 66, 72, 76, 78])" + "array([ 2, 10, 12, 20, 24, 26, 48, 50, 54, 64, 70, 74])" ] }, "execution_count": 84, @@ -2279,11 +2260,11 @@ { "data": { "text/plain": [ - "array([[ 0, 1, 0, 0, 0, 5, 6, 7],\n", - " [ 8, 9, 10, 11, 12, 13, 14, 15],\n", - " [16, 17, 18, 19, 0, 21, 22, 23],\n", - " [24, 0, 26, 27, 28, 29, 30, 31],\n", - " [32, 0, 34, 35, 0, 37, 0, 0]])" + "array([[ 0, 0, 2, 3, 4, 0, 0, 7],\n", + " [ 8, 9, 0, 11, 0, 0, 14, 15],\n", + " [16, 17, 18, 19, 20, 21, 22, 23],\n", + " [ 0, 0, 26, 0, 28, 29, 30, 31],\n", + " [ 0, 33, 34, 0, 36, 0, 38, 39]])" ] }, "execution_count": 85, @@ -2309,11 +2290,11 @@ { "data": { "text/plain": [ - "array([[ 0, 101, 2, 3, 4, 105, 106, 107],\n", - " [108, 109, 110, 111, 112, 113, 114, 115],\n", - " [116, 117, 118, 119, 20, 121, 122, 123],\n", - " [124, 25, 126, 127, 128, 129, 130, 131],\n", - " [132, 33, 134, 135, 36, 137, 38, 39]])" + "array([[100, 1, 102, 103, 104, 5, 6, 107],\n", + " [108, 109, 10, 111, 12, 13, 114, 115],\n", + " [116, 117, 118, 119, 120, 121, 122, 123],\n", + " [ 24, 25, 126, 27, 128, 129, 130, 131],\n", + " [ 32, 133, 134, 35, 136, 37, 138, 139]])" ] }, "execution_count": 86, @@ -2335,7 +2316,7 @@ } }, "source": [ - "What if we wanted the locations of where things are True?" + "What if we wanted the **locations** of where things are True?" ] }, { @@ -2346,11 +2327,11 @@ { "data": { "text/plain": [ - "array([[13, 9, 13, 13, 16, 8, 11, 12],\n", - " [ 9, 7, 9, 5, 10, 7, 6, 12],\n", - " [ 6, 10, 7, 6, 14, 5, 7, 6],\n", - " [10, 16, 9, 12, 7, 6, 11, 9],\n", - " [12, 16, 11, 7, 14, 11, 16, 13]])" + "array([[ 9, 16, 7, 12, 10, 16, 13, 9],\n", + " [ 8, 11, 15, 7, 14, 15, 9, 12],\n", + " [10, 12, 9, 5, 5, 5, 8, 9],\n", + " [14, 14, 10, 15, 11, 8, 6, 8],\n", + " [16, 6, 11, 14, 10, 13, 11, 6]])" ] }, "execution_count": 87, @@ -2374,7 +2355,8 @@ { "data": { "text/plain": [ - "(array([0, 0, 0, 0, 2, 3, 4, 4, 4, 4]), array([0, 2, 3, 4, 4, 1, 1, 4, 6, 7]))" + "(array([0, 0, 0, 1, 1, 1, 3, 3, 3, 4, 4, 4]),\n", + " array([1, 5, 6, 2, 4, 5, 0, 1, 3, 0, 3, 5]))" ] }, "execution_count": 88, @@ -2399,7 +2381,7 @@ { "data": { "text/plain": [ - "array([ 0, 2, 3, 4, 20, 25, 33, 36, 38, 39])" + "array([ 1, 5, 6, 10, 12, 13, 24, 25, 27, 32, 35, 37])" ] }, "execution_count": 89, @@ -2422,6 +2404,7 @@ "# Tons of Functions\n", "\n", "* np.sum\n", + "* np.clip\n", "* np.mean\n", "* np.std\n", "* np.min\n", @@ -2481,11 +2464,67 @@ { "cell_type": "code", "execution_count": 92, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[10, 10, 10, 10, 10, 10, 10, 10],\n", + " [10, 10, 10, 11, 12, 13, 14, 15],\n", + " [16, 17, 18, 19, 20, 21, 22, 23],\n", + " [24, 25, 26, 27, 28, 29, 30, 30],\n", + " [30, 30, 30, 30, 30, 30, 30, 30]])" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.clip(a, 10, 30)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, "metadata": { "slideshow": { "slide_type": "fragment" } }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[10, 10, 10, 10, 10, 10, 10, 10],\n", + " [10, 10, 10, 11, 12, 13, 14, 15],\n", + " [16, 17, 18, 19, 20, 21, 22, 23],\n", + " [24, 25, 26, 27, 28, 29, 30, 31],\n", + " [32, 33, 34, 35, 36, 37, 38, 39]])" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.clip(a, 10, None)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, "outputs": [ { "data": { @@ -2493,7 +2532,7 @@ "0" ] }, - "execution_count": 92, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } @@ -2504,7 +2543,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 95, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2517,7 +2556,7 @@ "array([ 0, 8, 16, 24, 32])" ] }, - "execution_count": 93, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } @@ -2528,7 +2567,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 96, "metadata": { "slideshow": { "slide_type": "subslide" @@ -2541,7 +2580,7 @@ "39" ] }, - "execution_count": 94, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } @@ -2552,7 +2591,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 97, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2565,7 +2604,7 @@ "(4, 7)" ] }, - "execution_count": 95, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } @@ -2591,7 +2630,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 98, "metadata": {}, "outputs": [ { @@ -2602,7 +2641,7 @@ " fill_value=999999)" ] }, - "execution_count": 96, + "execution_count": 98, "metadata": {}, "output_type": "execute_result" } @@ -2614,7 +2653,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 99, "metadata": {}, "outputs": [ { @@ -2625,7 +2664,7 @@ " fill_value=999999)" ] }, - "execution_count": 97, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } @@ -2636,7 +2675,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 100, "metadata": { "slideshow": { "slide_type": "subslide" @@ -2649,7 +2688,7 @@ "array([1, 2, 3])" ] }, - "execution_count": 98, + "execution_count": 100, "metadata": {}, "output_type": "execute_result" } @@ -2660,7 +2699,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 101, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2673,7 +2712,7 @@ "array([False, True, False])" ] }, - "execution_count": 99, + "execution_count": 101, "metadata": {}, "output_type": "execute_result" } @@ -2684,7 +2723,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 102, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2697,7 +2736,7 @@ "array([1, 0, 3])" ] }, - "execution_count": 100, + "execution_count": 102, "metadata": {}, "output_type": "execute_result" } @@ -2708,7 +2747,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 103, "metadata": { "slideshow": { "slide_type": "subslide" @@ -2723,7 +2762,7 @@ " fill_value=999999)" ] }, - "execution_count": 101, + "execution_count": 103, "metadata": {}, "output_type": "execute_result" } @@ -2750,7 +2789,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 104, "metadata": {}, "outputs": [], "source": [ @@ -2771,7 +2810,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 105, "metadata": {}, "outputs": [], "source": [ @@ -2798,7 +2837,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 106, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2822,7 +2861,7 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 107, "metadata": {}, "outputs": [], "source": [ @@ -2831,7 +2870,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 108, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2844,7 +2883,7 @@ "memmap([0, 1, 2, 3, 4], dtype=uint8)" ] }, - "execution_count": 106, + "execution_count": 108, "metadata": {}, "output_type": "execute_result" } @@ -2876,7 +2915,7 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 109, "metadata": {}, "outputs": [], "source": [ @@ -2887,7 +2926,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 110, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2897,10 +2936,10 @@ { "data": { "text/plain": [ - "<numpy.lib.npyio.NpzFile at 0x10a99ce80>" + "<numpy.lib.npyio.NpzFile at 0x7febaa57b908>" ] }, - "execution_count": 108, + "execution_count": 110, "metadata": {}, "output_type": "execute_result" } @@ -2912,7 +2951,7 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 111, "metadata": { "slideshow": { "slide_type": "fragment" @@ -2925,7 +2964,7 @@ "['my_var1', 'my_var2']" ] }, - "execution_count": 109, + "execution_count": 111, "metadata": {}, "output_type": "execute_result" } @@ -2936,7 +2975,7 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 112, "metadata": {}, "outputs": [ { @@ -2945,7 +2984,7 @@ "array([0, 1, 2, 3, 4], dtype=uint8)" ] }, - "execution_count": 110, + "execution_count": 112, "metadata": {}, "output_type": "execute_result" } @@ -2990,7 +3029,8 @@ "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" - } + }, + "title": "NumPy - A to Z" }, "nbformat": 4, "nbformat_minor": 4 -- GitLab