From 8b0174c2c0aff4a21531501adebcf28a5b6f1f7f Mon Sep 17 00:00:00 2001 From: Jackson Hammond Date: Tue, 3 Feb 2026 17:12:03 -0700 Subject: [PATCH 1/4] wk4 --- Intro2Python/Module5-LoopsComprehension.ipynb | 66 ++++++++++++++++--- Intro2Python/Module6-Functions.ipynb | 49 +++++++------- NumPy/1. Data Types.ipynb | 14 ++-- 3 files changed, 88 insertions(+), 41 deletions(-) diff --git a/Intro2Python/Module5-LoopsComprehension.ipynb b/Intro2Python/Module5-LoopsComprehension.ipynb index b5a4d26..2460363 100644 --- a/Intro2Python/Module5-LoopsComprehension.ipynb +++ b/Intro2Python/Module5-LoopsComprehension.ipynb @@ -514,10 +514,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 squared is 529\n", + "73 squared is 5329\n", + "12 squared is 144\n", + "84 squared is 7056\n" + ] + } + ], + "source": [ + "n = [23, 73, 12, 84]\n", + "for x in n:\n", + " print(f'{x} squared is {x**2}')\n", + " " + ] }, { "cell_type": "markdown", @@ -535,8 +551,27 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[78.5, 113.04, 200.96, 314.0, 490.625, 803.84]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "\n", + "diameters = [10, 12, 16, 20, 25, 32]\n", + "\n", + "Areas = [3.14*(d/2)**2 for d in diameters]\n", + "Areas\n", + "\n" + ] }, { "cell_type": "markdown", @@ -551,10 +586,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Alpha', 'Bravo', 'Delta']\n" + ] + } + ], + "source": [ + "phonetic_alphabet = ['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot']\n", + "\n", + "five_letter_words = [word for word in phonetic_alphabet if len(word) == 5]\n", + "print(five_letter_words)" + ] }, { "cell_type": "markdown", @@ -572,7 +620,7 @@ "metadata": { "hide_input": false, "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/Intro2Python/Module6-Functions.ipynb b/Intro2Python/Module6-Functions.ipynb index e973c85..7ed13e1 100644 --- a/Intro2Python/Module6-Functions.ipynb +++ b/Intro2Python/Module6-Functions.ipynb @@ -96,7 +96,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -109,7 +109,7 @@ ], "source": [ "def first_char(word): #initiate a function first_char with parameter word\n", - " word[0] # <--- No return statement, function returns None\n", + " word[0] # <--- No return statement, function returns None\n", " \n", " \n", "# Variable a will be equal to None\n", @@ -128,7 +128,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -168,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -184,21 +184,21 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Flow rate Q: 43.86 cfs\n" + "Flow rate Q: 56.20 cfs\n" ] } ], "source": [ "width = 10 #channel width in ft\n", - "depth = 1 #channel depth in ft\n", - "slope = 0.01 #channel slope in ft/ft\n", + "depth = 2 #channel depth in ft\n", + "slope = 0.002 #channel slope in ft/ft\n", "\n", "Q = mannings_equation_rect(width, depth, slope) #call mannings_equation_rect function with width, depth, slope, and default n value\n", "print(f'Flow rate Q: {Q:.2f} cfs') #print" @@ -215,7 +215,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -241,7 +241,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -273,7 +273,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -352,33 +352,32 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "def mannings_equation_rect(width, depth, slope): #initiate function mannings_equation with parameters width, depth, slope\n", + "def mannings_equation_rect(width, depth, slope, n): #initiate function mannings_equation with parameters width, depth, slope\n", " ''' Calculate flow rate Q using Manning's equation for a rectangular channel '''\n", " A = width * depth #cross-sectional flow area in sqft\n", " P = width + 2 * depth #wetted perimeter in ft\n", " R = A / P #hydraulic radius in ft\n", " Q = (1.486/n) * A * R**(2/3) * slope**0.5 #Manning's equation using global n\n", - " return Q #return flow rate Q, cfs" + " return Q, A, P, R #return flow rate Q, A, P, R\n" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 27, "metadata": {}, "outputs": [ { - "ename": "TypeError", - "evalue": "mannings_equation_rect() takes 3 positional arguments but 4 were given", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[12], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m width, depth, slope \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m0.01\u001b[39m \u001b[38;5;66;03m#channel width in ft, channel depth in ft, channel slope in ft/ft\u001b[39;00m\n\u001b[1;32m 3\u001b[0m n \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0.02\u001b[39m \u001b[38;5;66;03m#global variable n\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m Q \u001b[38;5;241m=\u001b[39m \u001b[43mmannings_equation_rect\u001b[49m\u001b[43m(\u001b[49m\u001b[43mwidth\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdepth\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mslope\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mn\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m#initiate function mannings_equation with parameters width, depth, slope, n\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mFlow rate Q: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mQ\u001b[38;5;132;01m:\u001b[39;00m\u001b[38;5;124m.2f\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m cfs\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mArea: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mA\u001b[38;5;132;01m:\u001b[39;00m\u001b[38;5;124m.2f\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m sqft\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;66;03m#this shoould give an error since A is a local variable inside the function, why do we get a value?\u001b[39;00m\n", - "\u001b[0;31mTypeError\u001b[0m: mannings_equation_rect() takes 3 positional arguments but 4 were given" + "name": "stdout", + "output_type": "stream", + "text": [ + "Flow rate Q: 65.80 cfs\n", + "Area: 10.00 sqft\n", + "R: 0.83 ft\n", + "Perimeter: 12.00 ft\n" ] } ], @@ -386,7 +385,7 @@ "#lets go back to our mannings equation function to see local vs global variables\n", "width, depth, slope = 10, 1, 0.01 #channel width in ft, channel depth in ft, channel slope in ft/ft\n", "n = 0.02 #global variable n\n", - "Q = mannings_equation_rect(width, depth, slope, n) #initiate function mannings_equation with parameters width, depth, slope, n\n", + "Q, A, P, R = mannings_equation_rect(width, depth, slope, n) #initiate function mannings_equation with parameters width, depth, slope, n\n", "\n", "print(f'Flow rate Q: {Q:.2f} cfs')\n", "print(f'Area: {A:.2f} sqft') #this shoould give an error since A is a local variable inside the function, why do we get a value?\n", @@ -501,7 +500,7 @@ "metadata": { "hide_input": false, "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, diff --git a/NumPy/1. Data Types.ipynb b/NumPy/1. Data Types.ipynb index f6feb84..bb6bbf4 100644 --- a/NumPy/1. Data Types.ipynb +++ b/NumPy/1. Data Types.ipynb @@ -48,7 +48,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -193,7 +193,7 @@ "17 str_ U" ] }, - "execution_count": 2, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -265,7 +265,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -274,7 +274,7 @@ "dtype('float32')" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -287,7 +287,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -296,7 +296,7 @@ "dtype('float32')" ] }, - "execution_count": 4, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -576,7 +576,7 @@ ], "metadata": { "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, From 256b73f969ae8dbf920c6eb53fb52122150b569c Mon Sep 17 00:00:00 2001 From: Jackson Hammond Date: Mon, 9 Feb 2026 14:45:25 -0700 Subject: [PATCH 2/4] Data Types Excercises --- NumPy/1. Data Types.ipynb | 110 ++++++++++++++++++++++++++---- NumPy/2. Create an Array.ipynb | 40 +++++------ NumPy/3. Inspect an Array.ipynb | 40 +++++++---- NumPy/4. Sampling Methods.ipynb | 28 ++++---- NumPy/5. Math Functions.ipynb | 60 ++++++++-------- NumPy/6. Sort an Array.ipynb | 33 +++++++-- NumPy/7. Slicing & Indexing.ipynb | 42 ++++++------ 7 files changed, 237 insertions(+), 116 deletions(-) diff --git a/NumPy/1. Data Types.ipynb b/NumPy/1. Data Types.ipynb index bb6bbf4..53e359f 100644 --- a/NumPy/1. Data Types.ipynb +++ b/NumPy/1. Data Types.ipynb @@ -502,10 +502,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original array: [250]\n", + "Array after adding 10: [44]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "#Create an NumPy array containing the value 250 with a data type of int16\n", + "arr = np.array([250], dtype='int16')\n", + "print('Original array: ' + str(arr))\n", + "#add 10 to the original array, which will cause an overflow\n", + "np_arr = np_arr + 10\n", + "print('Array after adding 10: ' + str(np_arr))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original array: [250]\n", + "Array after adding 10: [260]\n" + ] + } + ], + "source": [ + "#Fixed Code\n", + "\n", + "#Create an NumPy array containing the value 250 with a data type of int16\n", + "arr2 = np.array([250], dtype='int16')\n", + "print('Original array: ' + str(arr2))\n", + "#add 10 to the original array, which will cause an overflow\n", + "np_arr2 = arr2 + 10\n", + "print('Array after adding 10: ' + str(np_arr2))\n" + ] }, { "cell_type": "markdown", @@ -525,10 +567,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8000000\n", + "2000000\n", + "Memory saved by using float16 instead of float64: 75.0%\n" + ] + } + ], + "source": [ + "#Create a 1D array of 1,000,000 zeros using np.float64\n", + "arr3 = np.zeros(1000000, dtype=np.float64)\n", + "#Create a 1D array of 1,000,000 zeros using np.float16\n", + "arr4 = np.zeros(1000000, dtype=np.float16)\n", + "print(arr3.nbytes) # check the number of bytes used by arr3\n", + "print(arr4.nbytes) # check the number of bytes used by arr4\n", + "\n", + "#Calculate the ratio of memory saved by using float16 instead of float64\n", + "memory_saved = (arr3.nbytes - arr4.nbytes) / arr3.nbytes\n", + "print('Memory saved by using float16 instead of float64: ' + str(memory_saved * 100) + '%')" + ] }, { "cell_type": "markdown", @@ -550,21 +613,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The data type of the array is 4\u001b[0m arr1 \u001b[38;5;241m=\u001b[39m \u001b[43marr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreshape\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Reshape the array to 10x10\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(arr1)\n", + "Cell \u001b[0;32mIn[3], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m arr \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray(\u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m90\u001b[39m)) \u001b[38;5;66;03m# Create a numpy array with values from 0 to 99\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(arr)\n\u001b[0;32m----> 4\u001b[0m arr1 \u001b[38;5;241m=\u001b[39m \u001b[43marr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreshape\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Reshape the array to 10x10\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(arr1)\n", "\u001b[0;31mValueError\u001b[0m: cannot reshape array of size 90 into shape (10,10)" ] } @@ -95,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -122,7 +122,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -130,7 +130,8 @@ "output_type": "stream", "text": [ "55\n", - "55\n" + "55\n", + "61\n" ] } ], @@ -138,7 +139,8 @@ "# Select the element at row 5, column 5 using nested list-style indexing\n", "print(arr[5][5]) #try selecting different indices\n", "# Or more concisely using comma-separated indices\n", - "print(arr[5,5])" + "print(arr[5,5])\n", + "print(arr[6,1])" ] }, { @@ -150,7 +152,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -224,7 +226,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -260,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -291,7 +293,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -318,7 +320,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -349,7 +351,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -372,7 +374,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -397,7 +399,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -435,7 +437,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -456,7 +458,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -474,7 +476,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -496,7 +498,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -619,7 +621,7 @@ ], "metadata": { "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" }, From 0a0043e4b93b3bfd2b7d3810158aec3b5774b66c Mon Sep 17 00:00:00 2001 From: Jackson Hammond Date: Mon, 9 Feb 2026 18:18:12 -0700 Subject: [PATCH 3/4] Numpy.MathFunctions --- NumPy/2. Create an Array.ipynb | 53 +++++++++++++-- NumPy/4. Sampling Methods.ipynb | 34 +++++++++- NumPy/5. Math Functions.ipynb | 116 +++++++++++++++++++++++++++++--- 3 files changed, 187 insertions(+), 16 deletions(-) diff --git a/NumPy/2. Create an Array.ipynb b/NumPy/2. Create an Array.ipynb index 9718329..6c88c6e 100644 --- a/NumPy/2. Create an Array.ipynb +++ b/NumPy/2. Create an Array.ipynb @@ -693,10 +693,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10 20 30]\n", + "24\n", + "1000\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "#Create an array called arr_tuple from a tuple containing the numbers (10, 20, 30).\n", + "arr_tuple = np.array((10, 20, 30))\n", + "print(arr_tuple)\n", + "#create range of values from 0 to 999\n", + "arr_range = np.array(range(0,1000))\n", + "#change the data type of arr_range to int8\n", + "arr_range = arr_range.astype(np.int8)\n", + "print(arr_tuple.nbytes) # check the number of bytes used by arr_tuple\n", + "print(arr_range.nbytes) # check the number of bytes used by arr_range\n", + "\n" + ] }, { "cell_type": "markdown", @@ -718,10 +740,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Kesler Pea' '10420' 'Kings Peak' 'Lone Peak']\n", + "['Kesler Peak' '10420' 'Kings Peak' 'Lone Peak']\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "mts = np.array([\"Superior\",\"10420\",\"Kings Peak\",\"Lone Peak\"]) #create an array of mountain names\n", + "#replace \"Superior\" with \"Kesler Peak\" using indexing\n", + "mts[0] = \"Kesler Peak\"\n", + "print(mts)\n", + "\n", + "mts = np.array([\"Superior\",\"10420\",\"Kings Peak\",\"Lone Peak\"], dtype=' 0.0: [False True True False]\n" + ] + } + ], + "source": [ + "#create a NumPy array of the angles\n", + "angles = np.array([0,np.pi/4, np.pi/2, np.pi])\n", + "#take the Sine results and multiply them by 10, then subract 0.5\n", + "result = np.sin(angles) * 10 - 0.5\n", + "\n", + "result2 = np.log(np.sqrt((np.abs(result))))\n", + "print(\"Angles:\", angles)\n", + "print(\"Result1:\",result) # take the absolute value of the result, then calculate the square root, and then take the natural log\n", + "print(\"Result2:\",result2)\n", + "\n", + "#Create a boolean mask to show which of your result 2 values are greater than 0.0\n", + "mask = result2 > 0.0\n", + "print(\"Boolean mask for result2 > 0.0:\", mask)" + ] }, { "cell_type": "markdown", @@ -884,10 +909,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average temperature for each station: [22.66 19.98 25.62 18.58]\n", + "Maximum temperature for each reading slot: [23.5 20.5 26.5 19. ]\n", + "Index of maximum temperature for each station: [1 2 4 2]\n", + "Range of temperatures: 8.3\n" + ] + } + ], + "source": [ + "temps = np.array([\n", + " [22.1, 23.5, 22.8, 21.9, 23.0],\n", + " [19.5, 20.1, 20.5, 19.8, 20.0],\n", + " [25.4, 26.1, 24.9, 25.2, 26.5],\n", + " [18.2, 18.5, 19.0, 18.8, 18.4]\n", + "])\n", + "\n", + "# Calculate the average temperature for each station (Axis 1)\n", + "avg_temp_per_station = np.mean(temps, axis=1)\n", + "print(\"Average temperature for each station:\", avg_temp_per_station)\n", + "\n", + "# Find the maximum temperature recorded across the entire day for each reading slot (Axis 1)\n", + "max_temp_per_slot = np.max(temps, axis=1)\n", + "print(\"Maximum temperature for each reading slot:\", max_temp_per_slot)\n", + "\n", + "#Outlier Location: Find the index of the maximum temperature for each station using argmax.\n", + "max_temp_indices = np.argmax(temps, axis=1)\n", + "print(\"Index of maximum temperature for each station:\", max_temp_indices)\n", + "# Range: Calculate the difference between the max and min temperature of the entire dataset.\n", + "temp_range = np.max(temps) - np.min(temps)\n", + "print(\"Range of temperatures:\", temp_range)" + ] }, { "cell_type": "markdown", @@ -913,10 +971,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Storage Level at the end of each day: [11.2 14.7 13.9 19.1 23.2 21.7 22.2]\n", + "Total Cumulative Volume of water that entered the reservoir: 14.5\n", + "Days when storage level exceeded the Flood Action Level: [False False False True True True True]\n", + "Standard Deviation of daily net inflows: 2.3729771191824134\n", + "Median of daily net inflows: 1.2\n", + "Day with highest single-day net inflow: 3\n" + ] + } + ], + "source": [ + "Daily_Net_Inflow = np.array([1.2, 3.5, -0.8, 5.2, 4.1, -1.5, 0.5]) \n", + "\n", + "Initial_Storage = 10.0\n", + "\n", + "#Use a cumulative operation to calculate the Storage Level at the end of each day, starting from the initial 10.0 $Mm^3$.\n", + "Storage_Level = Initial_Storage + np.cumsum(Daily_Net_Inflow)\n", + "print(\"Storage Level at the end of each day:\", Storage_Level)\n", + "\n", + "#Calculate the Total Cumulative Volume of water that entered the reservoir (ignoring the days where the net flow was negative) by using a boolean mask to filter the inflow array before summing.\n", + "Total_Cumulative_Inflow = np.sum(Daily_Net_Inflow[Daily_Net_Inflow > 0])\n", + "print(\"Total Cumulative Volume of water that entered the reservoir:\", Total_Cumulative_Inflow)\n", + "\n", + "#The reservoir has a \"Flood Action Level\" of 18.0 $Mm^3$. Create a boolean array to determine on which days the storage level exceeded this threshold. Note, more info on boolean arrays in the next modules.\n", + "Flood_Action_Level = 18.0\n", + "Exceed_Flood_Level = Storage_Level > Flood_Action_Level\n", + "print(\"Days when storage level exceeded the Flood Action Level:\", Exceed_Flood_Level)\n", + "\n", + "#Calculate the Standard Deviation and the Median of the daily net inflows to report on the week's flow variability.\n", + "std_dev_inflow = np.std(Daily_Net_Inflow)\n", + "median_inflow = np.median(Daily_Net_Inflow)\n", + "print(\"Standard Deviation of daily net inflows:\", std_dev_inflow)\n", + "print(\"Median of daily net inflows:\", median_inflow)\n", + "\n", + "#Use argmax to find the day (index) that saw the highest single-day net inflow\n", + "day_with_highest_inflow = np.argmax(Daily_Net_Inflow)\n", + "print(\"Day with highest single-day net inflow:\", day_with_highest_inflow)" + ] }, { "cell_type": "markdown", From 7cea8941be37791287909101c807f1204688f0a1 Mon Sep 17 00:00:00 2001 From: Jackson Hammond Date: Tue, 10 Feb 2026 15:01:44 -0700 Subject: [PATCH 4/4] numpyfinal --- NumPy/7. Slicing & Indexing.ipynb | 87 ++++++++++++++++++++++--- NumPy/8. Combine & Split an Array.ipynb | 59 ++++++++++++++--- 2 files changed, 129 insertions(+), 17 deletions(-) diff --git a/NumPy/7. Slicing & Indexing.ipynb b/NumPy/7. Slicing & Indexing.ipynb index c6f2249..35d1488 100644 --- a/NumPy/7. Slicing & Indexing.ipynb +++ b/NumPy/7. Slicing & Indexing.ipynb @@ -542,10 +542,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Shape: (3, 24)\n", + "Transposed Shape: (24, 3)\n" + ] + } + ], + "source": [ + "level = np.array(np.linspace(1.5, 5.0, 72)) # create an array of 72 values from 1.5 to 5.0\n", + "#convert this 1D arrat to a 2D array with shape (3,24) to represent each day's values\n", + "level = level.reshape((3,24))\n", + "print('Shape:', level.shape)\n", + "transposed_level = level.transpose()\n", + "print('Transposed Shape:', transposed_level.shape)\n", + "\n" + ] }, { "cell_type": "markdown", @@ -573,10 +590,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Soil moisture at dead center of the grid: 52\n", + "Sub-Basin: [[32 35 30]\n", + " [40 42 38]\n", + " [55 58 52]]\n", + "North-East: [28 35 48 58 68]\n" + ] + } + ], + "source": [ + "moisture_grid = np.array([\n", + " [32, 35, 30, 28, 25],\n", + " [40, 42, 38, 35, 30],\n", + " [55, 58, 52, 48, 42],\n", + " [60, 65, 62, 58, 55],\n", + " [70, 72, 70, 68, 65]\n", + "])\n", + "\n", + "print('Soil moisture at dead center of the grid:', moisture_grid[2,2])\n", + "\n", + "# extract a \"sub-basin\" consiting fo the top-left 3x3 cortner of the grid\n", + "sub_basin = moisture_grid[0:3, 0:3]\n", + "print('Sub-Basin:', sub_basin)\n", + "North_East = moisture_grid[:,3]\n", + "print('North-East:', North_East)\n" + ] }, { "cell_type": "markdown", @@ -599,10 +644,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Rain in inches: [0. 0. 0.4875 1.755 0.2028 0. 0.7371 1.2168 0. 0.0819]\n", + "Heavy rain: [45. 31.2]\n", + "Zero rain: (array([0, 1, 5, 8]),)\n", + "Rain at indices 2, 3, 6: [12.5 45. 18.9]\n" + ] + } + ], + "source": [ + "rain = np.array([0, 0, 12.5, 45.0, 5.2, 0, 18.9, 31.2, 0, 2.1])\n", + "\n", + "rain_in = rain * 0.039 # convert mm to inches (1 mm = 0.039 inches)\n", + "print('Rain in inches:', rain_in)\n", + "\n", + "heavy_rain = rain[rain > 20]\n", + "print('Heavy rain:', heavy_rain)\n", + "\n", + "zero_rain = np.where(rain == 0)\n", + "print('Zero rain:', zero_rain)\n", + "\n", + "print('Rain at indices 2, 3, 6:', rain[[2,3,6]]) \n" + ] }, { "cell_type": "markdown", diff --git a/NumPy/8. Combine & Split an Array.ipynb b/NumPy/8. Combine & Split an Array.ipynb index 2838282..5e3d356 100644 --- a/NumPy/8. Combine & Split an Array.ipynb +++ b/NumPy/8. Combine & Split an Array.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -254,10 +254,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Vertical: (2, 5)\n", + "Horizontal: (10,)\n" + ] + } + ], + "source": [ + "Station_A = np.array([2.1, 5.4, 0.0, 1.2, 4.4])\n", + "Station_B = np.array([1.8, 4.9, 0.2, 0.8, 3.9])\n", + "data_vertical = np.vstack((Station_A, Station_B))\n", + "print('Vertical:', data_vertical.shape)\n", + "data_horizontal = np.hstack((Station_A, Station_B))\n", + "print('Horizontal:',data_horizontal.shape)" + ] }, { "cell_type": "markdown", @@ -284,10 +300,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Data:\n", + " [[10.2 45. ]\n", + " [15.5 62.3]\n", + " [ 2.1 58.1]]\n", + "Data with new hour:\n", + " [[10.2 45. ]\n", + " [15.5 62.3]\n", + " [ 2.1 58.1]\n", + " [ 0. 50.5]]\n" + ] + } + ], + "source": [ + "precip = np.array([[10.2], [15.5], [2.1]]) #(3 hours of rain)\n", + "flow = np.array([[45.0], [62.3], [58.1]]) #(3 hours of flow)\n", + "\n", + "# join these arrays along axis 1 to create a 3x2 array using np.concatenate\n", + "data = np.concatenate((precip, flow), axis=1)\n", + "print('Data:\\n', data)\n", + "\n", + "new_data = np.array([[0.0,50.5]]) # new data for a 4th hour\n", + "data = np.concatenate((data, new_data), axis=0)\n", + "print('Data with new hour:\\n', data)" + ] }, { "cell_type": "markdown", @@ -299,7 +342,7 @@ ], "metadata": { "kernelspec": { - "display_name": "p310env", + "display_name": "Python 3", "language": "python", "name": "python3" },