From 6c8cba888e7b852a621e053b59535b9e4d0b7192 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Wed, 7 Jan 2026 02:22:54 -0800 Subject: [PATCH 01/32] initial commit --- AD450_math.py | 5 + week_2_intro_ipython.ipynb | 403 ++++++++++++++++++++++++++++++++----- 2 files changed, 354 insertions(+), 54 deletions(-) create mode 100644 AD450_math.py diff --git a/AD450_math.py b/AD450_math.py new file mode 100644 index 0000000..911df99 --- /dev/null +++ b/AD450_math.py @@ -0,0 +1,5 @@ +def add(num1, num2): + return num1 + num2 + +def multiply(num1, num2): + return num1 * num2 \ No newline at end of file diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index 1a469f7..bb7ba0e 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -24,12 +24,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "id": "BQybla17FRh5" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My name is Joe.\n", + "I am a student at North Seattle College.\n" + ] + } + ], + "source": [ + "name = \"Joe\"\n", + "\n", + "print(f\"My name is {name}.\")\n", + "print(\"I am a student at North Seattle College.\")" + ] }, { "cell_type": "markdown", @@ -43,12 +57,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "id": "P7QDyWncFRh8" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My phone numberis 206-555-5555\n" + ] + } + ], + "source": [ + "phone_num = \"206-555-5555\"\n", + "\n", + "print(f\"My phone numberis {phone_num}\")" + ] }, { "cell_type": "markdown", @@ -62,12 +88,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "id": "OnOau948FRh9" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello my name is Joe and my phone number is 206-555-5555\n" + ] + } + ], + "source": [ + "print(f\"Hello my name is {name} and my phone number is {phone_num}\")" + ] }, { "cell_type": "markdown", @@ -82,12 +118,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "id": "5x2rIFzRFRh9" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I graduate in June of 2027.\n" + ] + } + ], + "source": [ + "month = \"June\"\n", + "year = 2027 \n", + "\n", + "print(f\"I graduate in {month} of {year}.\")" + ] }, { "cell_type": "markdown", @@ -109,12 +158,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "id": "OkyeAVPXFRh-" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans = 58.5\n" + ] + } + ], + "source": [ + "ans = 12 * 5 - 3 / 2\n", + "ans_type = str(type(ans))\n", + "\n", + "print(f\"ans = {ans}\")\n", + "\n", + "# ans is a float" + ] }, { "cell_type": "markdown", @@ -136,12 +200,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "id": "Lo-LEy8bKYmG" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "31 days\n" + ] + } + ], + "source": [ + "oct = \"October\"\n", + "\n", + "if oct == \"October\":\n", + " print(\"31 days\")\n", + "else:\n", + " print(\"30 days\")" + ] }, { "cell_type": "markdown", @@ -157,12 +236,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "id": "qscUkh813QKH" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "between 100 and 150\n" + ] + } + ], + "source": [ + "x = 140\n", + "\n", + "if x > 150:\n", + " print(\"greater than 150\")\n", + "elif x > 100 and x < 150:\n", + " print(\"between 100 and 150\")\n", + "else:\n", + " print(\"less than 100\")" + ] }, { "cell_type": "markdown", @@ -177,12 +273,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "id": "1vxg4nMbFRh_" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number is divisible by 5 and 10.\n", + "Number is divisible by 7 or by 2\n" + ] + } + ], + "source": [ + "y = 70\n", + "\n", + "if y % 5 == 0 and y % 10 == 0:\n", + " print(\"Number is divisible by 5 and 10.\")\n", + "\n", + "if y % 7 == 0 or y % 2 == 0:\n", + " print(\"Number is divisible by 7 or by 2\")\n" + ] }, { "cell_type": "markdown", @@ -195,12 +308,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": { "id": "JY_Ci5v-FRiA" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "14\n", + "21\n", + "28\n", + "35\n", + "42\n", + "49\n", + "56\n", + "63\n", + "70\n", + "77\n", + "84\n", + "91\n", + "98\n" + ] + } + ], + "source": [ + "for i in range(1,101):\n", + " if i % 7 == 0:\n", + " print(i)" + ] }, { "cell_type": "markdown", @@ -213,12 +351,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "id": "4a1D64hpFRiA" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "6\n", + "9\n" + ] + } + ], + "source": [ + "for i in range(1,11):\n", + " if i % 3 == 0:\n", + " print(i)" + ] }, { "cell_type": "markdown", @@ -241,12 +393,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "id": "ntBnYxzi1RPc" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Set: 1\n", + "Set: 2\n", + "Student: 1\n", + "Student: 2\n", + "Student: 3\n", + "Student: 4\n", + "Student: 5\n", + "Set: 3\n", + "Set: 4\n", + "Student: 1\n", + "Student: 2\n", + "Student: 3\n", + "Student: 4\n", + "Student: 5\n", + "Set: 5\n" + ] + } + ], + "source": [ + "for i in range(1,6):\n", + " print(f\"Set: {i}\")\n", + " if i % 2 == 0:\n", + " for j in range(1,6):\n", + " print(f\"Student: {j}\")" + ] }, { "cell_type": "markdown", @@ -259,12 +439,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": { "id": "lppms8ctzfRI" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hog\n", + "warts\n", + "s\n" + ] + } + ], + "source": [ + "school = \"Hogwarts\"\n", + "first = school[:3]\n", + "second = school[3:]\n", + "\n", + "print(first)\n", + "print(second)\n", + "print(school[-1])" + ] }, { "cell_type": "markdown", @@ -280,12 +478,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": { "id": "zw2NezoM0zfl" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thor is a character in the Marvel Cinematic Universe. She-Hulk is a character in the Marvel Cinematic Universe. Loki is a character in the Marvel Cinematic Universe. \n" + ] + } + ], + "source": [ + "chars = [\"Thor\", \"She-Hulk\", \"Loki\"]\n", + "output = \"\"\n", + "\n", + "for i in range(len(chars)):\n", + " output += chars[i]\n", + " output += \" is a character in the Marvel Cinematic Universe. \"\n", + "\n", + "print(output)" + ] }, { "cell_type": "markdown", @@ -299,12 +514,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": { "id": "rbyZif9a1b2V" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "January has 31 days. \n", + "February has 28 days. \n", + "March has 31 days. \n", + "April has 30 days. \n", + "May has 31 days. \n", + "June has 30 days. \n" + ] + } + ], + "source": [ + "months = [[\"January\", 31], [\"February\", 28], [\"March\", 31], [\"April\", 30], [\"May\", 31], [\"June\", 30]]\n", + "\n", + "for i in range(len(months)):\n", + " print(f\"{months[i][0]} has {months[i][1]} days. \")" + ] }, { "cell_type": "markdown", @@ -321,12 +554,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": { "id": "p0YE0nqs4Hcz" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'breed': 'Corgi', 'Age Exp.': 13, 'Type': 'Cattle herding'}\n", + "Corgi\n", + "The Corgi is expected to live about 13 years.\n" + ] + } + ], + "source": [ + "dog = {\"breed\": \"Corgi\", \"Age Exp.\": 13, \"Type\": \"Cattle herding\"}\n", + "\n", + "print(dog)\n", + "\n", + "print(dog[\"breed\"])\n", + "\n", + "print(f\"The {dog['breed']} is expected to live about {dog['Age Exp.']} years.\")\n", + "\n", + "# I used AI to help me with this as I kept getting an error on the last print statement \n", + "# because I was using double quotes for the variable I was placing inside the f-string. \n", + "# I was trying to access it like this dog[\"breed\"] but kept getting an error. So I pasted \n", + "# it into gpt and it explained that the interpreter was interpreting the first double \n", + "# quotation mark as the end of the f-string. Thus causing the error.\n", + "\n", + "# I also learned that in juypter you can't use multi-line comments in juypter notebooks \n", + "# by using the triple quotes before and after a comment\n" + ] }, { "cell_type": "markdown", @@ -337,9 +597,20 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import AD450_math\n", "\n", @@ -359,9 +630,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "module 'AD450_math' has no attribute 'multiply'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[32], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Cell with import error\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mAD450_math\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmultiply\u001b[49m(\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m2\u001b[39m)\n", + "\u001b[0;31mAttributeError\u001b[0m: module 'AD450_math' has no attribute 'multiply'" + ] + } + ], "source": [ "# Cell with import error\n", "AD450_math.multiply(2, 2)" @@ -369,7 +652,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ @@ -378,9 +661,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "module 'AD450_math' has no attribute 'multiply'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[34], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Cell with correct output \u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mAD450_math\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmultiply\u001b[49m(\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m2\u001b[39m)\n", + "\u001b[0;31mAttributeError\u001b[0m: module 'AD450_math' has no attribute 'multiply'" + ] + } + ], "source": [ "# Cell with correct output \n", "AD450_math.multiply(2, 2)" @@ -431,7 +726,7 @@ "provenance": [] }, "kernelspec": { - "display_name": "my_env_2", + "display_name": "ml-env", "language": "python", "name": "python3" }, @@ -445,7 +740,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.10.18" } }, "nbformat": 4, From 7b40d019a88f366c24cf518b8151162b1d427b3a Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Wed, 7 Jan 2026 03:58:05 -0800 Subject: [PATCH 02/32] finished week_2 assignment --- AD450_math.py | 3 +- week_2_intro_ipython.ipynb | 78 ++++++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/AD450_math.py b/AD450_math.py index 911df99..481036f 100644 --- a/AD450_math.py +++ b/AD450_math.py @@ -2,4 +2,5 @@ def add(num1, num2): return num1 + num2 def multiply(num1, num2): - return num1 * num2 \ No newline at end of file + return num1 * num2 + diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index bb7ba0e..894b64b 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -554,7 +554,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 6, "metadata": { "id": "p0YE0nqs4Hcz" }, @@ -597,7 +597,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -606,7 +606,7 @@ "4" ] }, - "execution_count": 23, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -614,7 +614,7 @@ "source": [ "import AD450_math\n", "\n", - "AD450_math.add(2,2)" + "AD450_math.add(2,2)\n" ] }, { @@ -630,7 +630,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -640,7 +640,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[32], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Cell with import error\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mAD450_math\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmultiply\u001b[49m(\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m2\u001b[39m)\n", + "Cell \u001b[0;32mIn[2], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Cell with import error\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mAD450_math\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmultiply\u001b[49m(\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m2\u001b[39m)\n", "\u001b[0;31mAttributeError\u001b[0m: module 'AD450_math' has no attribute 'multiply'" ] } @@ -652,28 +652,40 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Reload cell" + "# Reload cell\n", + "import importlib\n", + "importlib.reload(AD450_math)\n" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 6, "metadata": {}, "outputs": [ { - "ename": "AttributeError", - "evalue": "module 'AD450_math' has no attribute 'multiply'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[34], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Cell with correct output \u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mAD450_math\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmultiply\u001b[49m(\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m2\u001b[39m)\n", - "\u001b[0;31mAttributeError\u001b[0m: module 'AD450_math' has no attribute 'multiply'" - ] + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -695,20 +707,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# Create a" + "# Create a\n", + "a = 1" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], "source": [ - "# Print a" + "# Print a\n", + "print(a)" ] }, { @@ -717,7 +739,13 @@ "metadata": {}, "outputs": [], "source": [ - "# Set a to 4" + "# Set a to 4\n", + "a = 4\n", + "\n", + "\"\"\" \n", + "It looks like after running this block that it is changing the global value of a. \n", + "so when we run the block above it prints out 4 because a has been re-assigned.\n", + "\"\"\";" ] } ], From 63131b994c7ab947a366e732870fb746097d73fd Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Wed, 7 Jan 2026 04:00:47 -0800 Subject: [PATCH 03/32] updated comment on question 15 --- week_2_intro_ipython.ipynb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index 894b64b..5431b83 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -554,7 +554,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "id": "p0YE0nqs4Hcz" }, @@ -578,14 +578,17 @@ "\n", "print(f\"The {dog['breed']} is expected to live about {dog['Age Exp.']} years.\")\n", "\n", - "# I used AI to help me with this as I kept getting an error on the last print statement \n", - "# because I was using double quotes for the variable I was placing inside the f-string. \n", - "# I was trying to access it like this dog[\"breed\"] but kept getting an error. So I pasted \n", - "# it into gpt and it explained that the interpreter was interpreting the first double \n", - "# quotation mark as the end of the f-string. Thus causing the error.\n", + "\"\"\"\n", + "I used AI to help me with this as I kept getting an error on the last print statement \n", + "because I was using double quotes for the variable I was placing inside the f-string. \n", + "I was trying to access it like this dog[\"breed\"] but kept getting an error. So I pasted \n", + "it into gpt and it explained that the interpreter was interpreting the first double \n", + "quotation mark as the end of the f-string. Thus causing the error. \n", "\n", - "# I also learned that in juypter you can't use multi-line comments in juypter notebooks \n", - "# by using the triple quotes before and after a comment\n" + "I also learned that juypter notebook is finicky about mutli-line comments and you have \n", + "to add a semi-colon after the second triple quote otherwise it will print your comment. \n", + "\"\"\";\n", + "\n" ] }, { From 9eca43bb2ecb57d6135bd0ec1e97ebfb0b2935e7 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Thu, 8 Jan 2026 16:19:17 -0800 Subject: [PATCH 04/32] updated a comment --- week_2_intro_ipython.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index 5431b83..2edadce 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -738,7 +738,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ From ed43ba57662989df074271fe79e6593b4352c512 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Tue, 13 Jan 2026 17:23:44 -0800 Subject: [PATCH 05/32] first commit for week2 --- week_2_intro_ipython.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index 2edadce..ad1b663 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "id": "BQybla17FRh5" }, @@ -39,7 +39,7 @@ } ], "source": [ - "name = \"Joe\"\n", + "name = \"Joee\"\n", "\n", "print(f\"My name is {name}.\")\n", "print(\"I am a student at North Seattle College.\")" From 6faf1054c3521d5ffee5e0051a4cc252ff9816cd Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Tue, 13 Jan 2026 17:37:39 -0800 Subject: [PATCH 06/32] fixed name type --- week_2_intro_ipython.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index ad1b663..73ccd25 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -39,7 +39,7 @@ } ], "source": [ - "name = \"Joee\"\n", + "name = \"Joe\"\n", "\n", "print(f\"My name is {name}.\")\n", "print(\"I am a student at North Seattle College.\")" From 411ed8a0c45e273648965853dc658e6c1761b3be Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Tue, 13 Jan 2026 18:32:16 -0800 Subject: [PATCH 07/32] fixed issue with problem 10 --- week_2_intro_ipython.ipynb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index 73ccd25..4a3bb1d 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -351,7 +351,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 6, "metadata": { "id": "4a1D64hpFRiA" }, @@ -360,16 +360,19 @@ "name": "stdout", "output_type": "stream", "text": [ - "3\n", - "6\n", - "9\n" + "3 6 9 \n", + "18\n" ] } ], "source": [ + "sum = 0\n", "for i in range(1,11):\n", " if i % 3 == 0:\n", - " print(i)" + " print(i, end= \" \")\n", + " sum += i\n", + "\n", + "print(f\"\\n{sum}\")" ] }, { From 23d9af13a793fe314faee034eec99abdef55bc27 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Wed, 14 Jan 2026 02:00:36 -0800 Subject: [PATCH 08/32] fixed a few issues where I didn't follow directions correctly --- week_2_intro_ipython.ipynb | 55 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index 4a3bb1d..4eee6b3 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "id": "BQybla17FRh5" }, @@ -39,10 +39,7 @@ } ], "source": [ - "name = \"Joe\"\n", - "\n", - "print(f\"My name is {name}.\")\n", - "print(\"I am a student at North Seattle College.\")" + "print(f\"My name is Joe.\\nI am a student at North Seattle College.\")" ] }, { @@ -57,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "id": "P7QDyWncFRh8" }, @@ -66,12 +63,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "My phone numberis 206-555-5555\n" + "My phone numberis 2065555555\n" ] } ], "source": [ - "phone_num = \"206-555-5555\"\n", + "phone_num = \"2065555555\"\n", "\n", "print(f\"My phone numberis {phone_num}\")" ] @@ -88,7 +85,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "metadata": { "id": "OnOau948FRh9" }, @@ -102,7 +99,10 @@ } ], "source": [ - "print(f\"Hello my name is {name} and my phone number is {phone_num}\")" + "phone = \"206-555-5555\"\n", + "name = \"Joe\"\n", + "\n", + "print(f\"Hello my name is {name} and my phone number is {phone}\")" ] }, { @@ -118,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 8, "metadata": { "id": "5x2rIFzRFRh9" }, @@ -132,8 +132,7 @@ } ], "source": [ - "month = \"June\"\n", - "year = 2027 \n", + "month, year = \"June\", 2027\n", "\n", "print(f\"I graduate in {month} of {year}.\")" ] @@ -273,7 +272,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 10, "metadata": { "id": "1vxg4nMbFRh_" }, @@ -290,7 +289,9 @@ "source": [ "y = 70\n", "\n", - "if y % 5 == 0 and y % 10 == 0:\n", + "# you only need to check for mod 10 below becuase if it is divisible by 10 then \n", + "# it is also divisible by 5\n", + "if y % 10 == 0:\n", " print(\"Number is divisible by 5 and 10.\")\n", "\n", "if y % 7 == 0 or y % 2 == 0:\n", @@ -351,7 +352,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 13, "metadata": { "id": "4a1D64hpFRiA" }, @@ -367,10 +368,13 @@ ], "source": [ "sum = 0\n", - "for i in range(1,11):\n", + "i = 1\n", + "\n", + "while i < 11:\n", " if i % 3 == 0:\n", " print(i, end= \" \")\n", " sum += i\n", + " i += 1\n", "\n", "print(f\"\\n{sum}\")" ] @@ -481,7 +485,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 16, "metadata": { "id": "zw2NezoM0zfl" }, @@ -490,19 +494,15 @@ "name": "stdout", "output_type": "stream", "text": [ - "Thor is a character in the Marvel Cinematic Universe. She-Hulk is a character in the Marvel Cinematic Universe. Loki is a character in the Marvel Cinematic Universe. \n" + "Thor is a character in the Marvel Cinematic Universe. She-Hulk is a character in the Marvel Cinematic Universe. Loki is a character in the Marvel Cinematic Universe. " ] } ], "source": [ "chars = [\"Thor\", \"She-Hulk\", \"Loki\"]\n", - "output = \"\"\n", - "\n", - "for i in range(len(chars)):\n", - " output += chars[i]\n", - " output += \" is a character in the Marvel Cinematic Universe. \"\n", "\n", - "print(output)" + "for name in chars:\n", + " print(f\"{name} is a character in the Marvel Cinematic Universe.\", end = \" \")\n" ] }, { @@ -517,7 +517,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 19, "metadata": { "id": "rbyZif9a1b2V" }, @@ -539,7 +539,8 @@ "months = [[\"January\", 31], [\"February\", 28], [\"March\", 31], [\"April\", 30], [\"May\", 31], [\"June\", 30]]\n", "\n", "for i in range(len(months)):\n", - " print(f\"{months[i][0]} has {months[i][1]} days. \")" + " month, days = months[i][0], months[i][1]\n", + " print(f\"{month} has {days} days. \")" ] }, { From f058899c48f56ca0bb19e37df67fb675163a693b Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Wed, 14 Jan 2026 02:03:18 -0800 Subject: [PATCH 09/32] fixed type on problem #2 --- week_2_intro_ipython.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index 4eee6b3..ca1b174 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -54,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "id": "P7QDyWncFRh8" }, @@ -70,7 +70,7 @@ "source": [ "phone_num = \"2065555555\"\n", "\n", - "print(f\"My phone numberis {phone_num}\")" + "print(f\"My phone number is {phone_num}\")" ] }, { From 8011768acb8102cbe32438e0b9ed2c6434516717 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Wed, 14 Jan 2026 02:04:30 -0800 Subject: [PATCH 10/32] fogot to save changes to typo fix on problem #2 --- week_2_intro_ipython.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index ca1b174..e6bf222 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -54,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": { "id": "P7QDyWncFRh8" }, @@ -63,7 +63,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "My phone numberis 2065555555\n" + "My phone number is 2065555555\n" ] } ], From 39bcf659a19439ebd01dfe494f456221bfdd5962 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Fri, 16 Jan 2026 03:23:48 -0800 Subject: [PATCH 11/32] completed first couple of problems --- week_3_numpy.ipynb | 114 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 90 insertions(+), 24 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index 1db8573..cd28b4e 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "metadata": { "id": "Pt63xmy1qLll" }, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -83,19 +83,37 @@ "id": "AVByQUYKxtNG", "outputId": "238e50f0-366d-41de-a44a-6cf3e063650c" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2 3 4]\n", + " [5 6 7 8]]\n", + "\n", + "Inspect the array\n", + "--------------\n", + "dimensions: 2\n", + "shape:\t (2, 4)\n", + "size:\t 8\n", + "datatype int64\n", + "bytes:\t 64\n" + ] + } + ], "source": [ "# create an array called numbers with 2 rows and 4 columns from literal values. Have your literal values be the integers 1-8\n", - "numbers = None #Replace None with array defintion\n", + "numbers = np.array([[1,2,3,4], [5,6,7,8]])\n", + "\n", "print(numbers)\n", "\n", "# Replace None below with each value for the array. \n", "print(\"\\nInspect the array\\n--------------\")\n", - "print(\"dimensions:\", None) # ndarrays have dimensions\n", - "print(\"shape:\\t\", None) # ndarrays have shape (numer of rows & columns)\n", - "print(\"size:\\t\", None) # number of elements in the array\n", - "print(\"datatype\", None) # numpy determines the datatype\n", - "print(\"bytes:\\t\", None) # total bytes consumed by the array" + "print(\"dimensions:\", numbers.ndim) # ndarrays have dimensions\n", + "print(\"shape:\\t\", numbers.shape) # ndarrays have shape (numer of rows & columns)\n", + "print(\"size:\\t\", numbers.size) # number of elements in the array\n", + "print(\"datatype\", numbers.dtype) # numpy determines the datatype\n", + "print(\"bytes:\\t\", numbers.size * numbers.itemsize) # total bytes consumed by the array" ] }, { @@ -109,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -117,34 +135,76 @@ "id": "pTXgMRYRQS3v", "outputId": "010d4702-bc29-4f87-980e-66fe9b43436a" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ones\n", + "[[1. 1. 1. 1.]\n", + " [1. 1. 1. 1.]\n", + " [1. 1. 1. 1.]] \n", + "\n", + "zeros\n", + "[[0. 0. 0. 0.]\n", + " [0. 0. 0. 0.]\n", + " [0. 0. 0. 0.]] \n", + "\n", + "random\n", + "None \n", + "\n", + "empty\n", + "[[0. 0.]\n", + " [0. 0.]\n", + " [0. 0.]] \n", + "\n", + "full\n", + "[['x' 'x']\n", + " ['x' 'x']] \n", + "\n", + "even\n", + "[10 15 20] \n", + "\n", + "line\n", + "None \n", + "\n" + ] + } + ], "source": [ "# Create an 3 by 4 array of ones\n", - "ones = None\n", + "ones = np.ones((3,4))\n", + "print(\"ones\")\n", "print(ones, \"\\n\")\n", "\n", "# Create an 3 by 4 array of zeros\n", - "zeros = None\n", + "zeros = np.zeros((3,4))\n", + "print(\"zeros\")\n", "print(zeros, \"\\n\")\n", "\n", "# Create an 2 by 2 array with random values\n", "rando = None\n", + "print(\"random\")\n", "print(rando, \"\\n\")\n", "\n", "# Create 3 by 2 an empty array\n", - "empty = None\n", + "empty = np.empty((3,2))\n", + "print(\"empty\")\n", "print(empty, \"\\n\")\n", "\n", "# Create a 2 by 2 array full of 'x's, hint seach numpy full\n", - "full = None\n", + "full = np.full((2,2),'x')\n", + "print(\"full\")\n", "print(full, \"\\n\")\n", "\n", "# Create an array of the form [10, 15, 20] using np.arange\n", - "even = None\n", + "even = np.arange(10,21,5)\n", + "print(\"even\")\n", "print(even, \"\\n\")\n", "\n", "# Create an array of the form [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] using np.linspace\n", "line = None\n", + "print(\"line\")\n", "print(line, \"\\n\")\n" ] }, @@ -172,7 +232,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 43, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -184,7 +244,9 @@ "source": [ "arr1d = np.array([0,1, 2, 3, 4, 5, 6, 7, 8])\n", "\n", - "# using indexing select and output [5, 6, 7] from arr1d. Save this slice to a variable named slice_arr1d\n" + "# using indexing select and output [5, 6, 7] from arr1d. Save this slice to a variable named slice_arr1d\n", + "\n", + "slice_arr1d = arr1d[5:8]\n" ] }, { @@ -196,7 +258,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 46, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -206,7 +268,9 @@ }, "outputs": [], "source": [ - "# Replace 6, 7, and 8 with 12 in arr1d using one line of code. Then print arr1d and slice_arr1d\n" + "# Replace 6, 7, and 8 with 12 in arr1d using one line of code. Then print arr1d and slice_arr1d\n", + "\n", + "arr1d[6:] = 12\n" ] }, { @@ -218,7 +282,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 48, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -228,7 +292,9 @@ }, "outputs": [], "source": [ - "# Create a copy of arr1d and name it new_array. Hint: You can't use new_array = arr1d" + "# Create a copy of arr1d and name it new_array. Hint: You can't use new_array = arr1d\n", + "\n", + "new_array = arr1d.copy()" ] }, { @@ -664,7 +730,7 @@ "provenance": [] }, "kernelspec": { - "display_name": "base", + "display_name": "ml-env", "language": "python", "name": "python3" }, @@ -678,7 +744,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.4" + "version": "3.10.18" } }, "nbformat": 4, From cb80c5c631c60eeb78ffc83ca71228a14a8d75a4 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Sat, 17 Jan 2026 06:55:53 -0800 Subject: [PATCH 12/32] finished problem 3 --- week_3_numpy.ipynb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index cd28b4e..03564c8 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "id": "Pt63xmy1qLll" }, @@ -127,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -151,7 +151,8 @@ " [0. 0. 0. 0.]] \n", "\n", "random\n", - "None \n", + "[[ 0.38370392 -0.69051018]\n", + " [-0.69090326 -0.36937786]] \n", "\n", "empty\n", "[[0. 0.]\n", @@ -166,7 +167,7 @@ "[10 15 20] \n", "\n", "line\n", - "None \n", + "[0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] \n", "\n" ] } @@ -183,7 +184,7 @@ "print(zeros, \"\\n\")\n", "\n", "# Create an 2 by 2 array with random values\n", - "rando = None\n", + "rando = np.random.standard_normal(size = (2,2))\n", "print(\"random\")\n", "print(rando, \"\\n\")\n", "\n", @@ -203,7 +204,7 @@ "print(even, \"\\n\")\n", "\n", "# Create an array of the form [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] using np.linspace\n", - "line = None\n", + "line = np.linspace(0, 2, num = 9)\n", "print(\"line\")\n", "print(line, \"\\n\")\n" ] From e2b5b214a2333ab9f45f930590cb7eae579689df Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Sat, 17 Jan 2026 18:26:45 -0800 Subject: [PATCH 13/32] finished problem 7 --- week_3_numpy.ipynb | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index 03564c8..8e18d61 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -307,13 +307,25 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "3\n" + ] + } + ], "source": [ "arr2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", "\n", - "# There are two ways to select 3 from the 2 dimensional array above. Use both ways to select 3 and print the results. " + "# There are two ways to select 3 from the 2 dimensional array above. Use both ways to select 3 and print the results. \n", + "\n", + "print(arr2d[0][2])\n", + "print(arr2d[0,2])" ] }, { @@ -325,7 +337,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -333,16 +345,33 @@ "id": "kvMvyar216T5", "outputId": "943f42a4-469a-48f0-f4d4-546849ffa08d" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[2 3 4]\n", + " [6 7 8]] \n", + "\n", + "[[3]\n", + " [7]] \n", + "\n", + "[[1 2 3 4]]\n" + ] + } + ], "source": [ "# In mult-dimensional arrays, slicing that omits later indices will return a lower-dimensional ndarray\n", "# Select and print out the following:\n", "\n", "# select first two rows and all but first column\n", + "print(arr2d[:2, 1:], \"\\n\")\n", "\n", "# select first two rows and only 3rd column\n", + "print(arr2d[:2, 2:3], \"\\n\")\n", "\n", - "# select all columns in the first row\n" + "# select all columns in the first row\n", + "print(arr2d[:1, :])\n" ] }, { From 889f1811a00df88863c6690c90f838c68acfad7b Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Sun, 18 Jan 2026 17:19:35 -0800 Subject: [PATCH 14/32] completed through problem 15 --- week_3_numpy.ipynb | 184 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 158 insertions(+), 26 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index 8e18d61..b1c51a5 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": { "id": "Pt63xmy1qLll" }, @@ -394,7 +394,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 33, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -402,14 +402,38 @@ "id": "IMEp_RGh_Fu5", "outputId": "45332ef8-2c42-4c40-af09-6caaf98bca6b" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First and fourth rows of data: \n", + "[[0 0 0 0 1 1 1]\n", + " [0 1 1 1 0 1 0]] \n", + "\n", + "All of data: \n", + "[[0 0 0 0 1 1 1]\n", + " [1 1 0 1 1 0 0]\n", + " [1 0 0 0 1 0 1]\n", + " [0 1 1 1 0 1 0]\n", + " [0 1 1 1 1 1 1]\n", + " [0 0 0 0 1 1 0]\n", + " [0 1 0 0 0 0 0]]\n" + ] + } + ], "source": [ "import numpy as np\n", "names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])\n", "data = np.random.randint(2, size=(7, 7)) # create an array of random values\n", "\n", "# Select and print the first and forth rows from 'data' using just the array 'names' and 'data'. Print out both the selected rows and 'data'. \n", - "# Hint\" The \"Bob\" appears in both the first and forth rows. " + "# Hint\" The \"Bob\" appears in both the first and forth rows. \n", + "\n", + "print(\"First and fourth rows of data: \")\n", + "print(data[names == \"Bob\"], \"\\n\")\n", + "print(\"All of data: \")\n", + "print(data)\n" ] }, { @@ -423,7 +447,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -431,10 +455,22 @@ "id": "BgULatpwt7X_", "outputId": "c31d49b4-92ff-4514-c91b-8eeb42573b5f" }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0, 0, 0, 0, 1, 1, 1],\n", + " [0, 1, 1, 1, 0, 1, 0]])" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Again output the first and fouth rows using the cond below. Meaning replace None with an expression\n", - "cond = None\n", + "cond = (names == \"Bob\")\n", "data[cond]" ] }, @@ -449,7 +485,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 35, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -457,9 +493,27 @@ "id": "EP-SDDVIvK8s", "outputId": "cce4fab9-cd5e-46c2-c615-f2518f1b4a72" }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 1, 0, 1, 1, 0, 0],\n", + " [1, 0, 0, 0, 1, 0, 1],\n", + " [0, 1, 1, 1, 1, 1, 1],\n", + " [0, 0, 0, 0, 1, 1, 0],\n", + " [0, 1, 0, 0, 0, 0, 0]])" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Select all rows except the first and forth" + "# Select all rows except the first and forth\n", + "\n", + "cond = ~(names == \"Bob\")\n", + "data[cond]" ] }, { @@ -473,7 +527,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 36, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -481,9 +535,23 @@ "id": "QYjmF7eFvTmc", "outputId": "172adeb5-31d7-47ad-e3af-59c352f96f97" }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 1, 1],\n", + " [0, 1, 0]])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Select the last 5 columns for the first and forth row" + "# Select the last 5 columns for the first and forth row\n", + "cond = (names == \"Bob\")\n", + "data[cond, 4:]" ] }, { @@ -497,7 +565,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 37, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -505,9 +573,26 @@ "id": "sciz0aprvh8u", "outputId": "24c8c092-cbe9-4665-9e01-9d193bcac8df" }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0, 0, 0, 0, 1, 1, 1],\n", + " [1, 0, 0, 0, 1, 0, 1],\n", + " [0, 1, 1, 1, 0, 1, 0],\n", + " [0, 1, 1, 1, 1, 1, 1]])" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Select the first, third, forth and fifth row using similar conditioins and an OR operator" + "# Select the first, third, forth and fifth row using similar conditioins and an OR operator\n", + "\n", + "cond = (names == \"Bob\") | (names == \"Will\")\n", + "data[cond]" ] }, { @@ -532,7 +617,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -540,18 +625,39 @@ "id": "HdWTjc8CZI5R", "outputId": "e070b71e-bdee-4abc-f378-474978208ad5" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original array: \n", + "[[1. 2. 3.]\n", + " [4. 5. 6.]] \n", + "\n", + "Adding 5: \n", + "[[ 6. 7. 8.]\n", + " [ 9. 10. 11.]] \n", + "\n", + "Multiplying by 10: \n", + "[[10. 20. 30.]\n", + " [40. 50. 60.]]\n" + ] + } + ], "source": [ "array1 = np.array([[1., 2., 3.], [4., 5., 6.]])\n", "\n", - "print(array1)\n", + "print(\"Original array: \")\n", + "print(array1, \"\\n\")\n", "\n", "# Add 5 to all values in array1 and print \n", - "print()\n", + "print(\"Adding 5: \")\n", + "print(array1 + 5, \"\\n\")\n", "\n", "\n", "# Multiple all values in array1 by 10 and print \n", - "print()\n" + "print(\"Multiplying by 10: \")\n", + "print(array1 * 10)\n" ] }, { @@ -565,7 +671,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 48, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -573,9 +679,23 @@ "id": "uHr0DxrmaOEZ", "outputId": "b0f74550-427d-4333-c010-45f886677379" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[False True False]\n", + " [ True False True]]\n" + ] + } + ], "source": [ + "# array1 = np.array([[1., 2., 3.], [4., 5., 6.]])\n", + "\n", "array2 = np.array([[0., 4., 1.], [7., 2., 12.]])\n", + "result14 = np.where(array1 < array2, True, False)\n", + "\n", + "print(result14)\n", "\n", "\n", "# Test to see if pairwise elements in array1 are less than the corresponding element in array2. Output the results.\n", @@ -615,7 +735,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 49, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -623,13 +743,25 @@ "id": "k5pIynI12dmY", "outputId": "cd39dc42-7bab-4a92-c3e0-151a287d0b07" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1. 0. 1. 1. 0.]\n" + ] + } + ], "source": [ "arr1 = np.ones(5)\n", "arr2 = np.zeros(5)\n", "cond = np.array([True, False, True, True, False])\n", "\n", - "# Output the value from `arr1` whenever the corresponding value in `cond` is True, and otherwise take the value from `arr2`. \n" + "# Output the value from `arr1` whenever the corresponding value in `cond` is True, and otherwise take the value from `arr2`. \n", + "\n", + "result15 = np.where(cond, arr1, arr2)\n", + "\n", + "print(result15)\n" ] }, { From 0e59c04083acac8602280a0ac5b57a010274b401 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Mon, 19 Jan 2026 04:17:27 -0800 Subject: [PATCH 15/32] finished --- week_3_numpy.ipynb | 125 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 110 insertions(+), 15 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index b1c51a5..d026fbd 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "metadata": { "id": "Pt63xmy1qLll" }, @@ -792,7 +792,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -800,12 +800,33 @@ "id": "ImvBT7NX3aY7", "outputId": "d04b3b76-91e4-4a40-e047-1e7a55bfd4ed" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 3 3 3]\n", + " [0 3 1 0]\n", + " [1 1 1 2]\n", + " [3 3 2 0]\n", + " [1 3 2 0]] \n", + "\n", + "Mean: 1.6\n", + "Sum: 32\n" + ] + } + ], "source": [ "arr = np.random.randint(4, size=(5, 4))\n", - "print(arr)\n", + "print(arr, \"\\n\")\n", "\n", - "# Calculate and print the mean and sum for all values in the array\n" + "# Calculate and print the mean and sum for all values in the array\n", + "\n", + "mean16 = np.mean(arr)\n", + "sum16 = arr.sum()\n", + "\n", + "print(\"Mean: \", mean16)\n", + "print(\"Sum: \", sum16)\n" ] }, { @@ -817,7 +838,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -825,9 +846,32 @@ "id": "kxw7AUVm31Ea", "outputId": "b1cb6ece-5056-41b1-8c1b-f57612b9dae1" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mean across rows: [2.25 1. 1.25 2. 1.5 ]\n", + "Mean across columns: [1. 2.6 1.8 1. ]\n", + "Sum across rows: [9 4 5 8 6]\n", + "Sum across columns: [ 5 13 9 5]\n" + ] + } + ], "source": [ - "# Calculate the mean and sum across the columns and the rows. Print out both\n" + "# Calculate the mean and sum across the columns and the rows. Print out both\n", + "# axis = 0 columns \n", + "# axis = 1 rows \n", + "\n", + "mean17_rows = arr.mean(axis = 1)\n", + "mean17_columns = arr.mean(axis = 0)\n", + "sum17_rows = arr.sum(axis = 1)\n", + "sum17_columns = arr.sum(axis = 0)\n", + "\n", + "print(\"Mean across rows: \", mean17_rows)\n", + "print(\"Mean across columns: \", mean17_columns)\n", + "print(\"Sum across rows: \", sum17_rows)\n", + "print(\"Sum across columns: \", sum17_columns)\n" ] }, { @@ -839,15 +883,46 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Full cummulative sum: \n", + " [ 0 3 6 9 9 12 13 13 14 15 16 18 21 24 26 26 27 30 32 32] \n", + "\n", + "Columns cumulative sum: \n", + " [[ 0 3 3 3]\n", + " [ 0 6 4 3]\n", + " [ 1 7 5 5]\n", + " [ 4 10 7 5]\n", + " [ 5 13 9 5]] \n", + "\n", + "Rows cumulative sum: \n", + " [[0 3 6 9]\n", + " [0 3 4 4]\n", + " [1 2 3 5]\n", + " [3 6 8 8]\n", + " [1 4 6 6]]\n" + ] + } + ], "source": [ "# Print out the cumulative sum for the full array, the columns and the rows. \n", "# For example the array [[1, 1, 1], [1, 1, 1]] would have the following outputs:\n", "# full array: [1, 2, 3, 4, 5, 6]\n", "# columns: [[1, 1, 1], [2, 2, 2]]\n", - "# rows: [[1, 2, 3], [1, 2, 3]]\n" + "# rows: [[1, 2, 3], [1, 2, 3]]\n", + "\n", + "cumsum18_full = arr.cumsum()\n", + "cumsum18_columns = arr.cumsum(axis = 0)\n", + "cumsum18_rows = arr.cumsum(axis = 1)\n", + "\n", + "print(\"Full cummulative sum: \\n\", cumsum18_full, \"\\n\")\n", + "print(\"Columns cumulative sum: \\n\", cumsum18_columns, \"\\n\")\n", + "print(\"Rows cumulative sum: \\n\", cumsum18_rows)\n" ] }, { @@ -859,7 +934,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -867,16 +942,36 @@ "id": "sDnJpvUR1B1W", "outputId": "c4806b83-0ba9-448e-c4b7-f9ca53a8900d" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Arr: \n", + " [0 1 2 3 4 5 6 7 8 9] \n", + "\n", + "Odds: \n", + " [1 3 5 7 9] \n", + "\n", + "Arr reversed: \n", + " [9 8 7 6 5 4 3 2 1 0]\n" + ] + } + ], "source": [ "\n", "arr = np.arange(10)\n", - "print(arr)\n", + "print(\"Arr: \\n\", arr, \"\\n\")\n", "\n", "\n", "# Print out all all odd values\n", + "cond19 = arr % 2 \n", + "\n", + "odds = arr[arr % 2 == 1]\n", + "print(\"Odds: \\n\", odds, \"\\n\")\n", "\n", - "# Print out all values in reverse order\n" + "# Print out all values in reverse order\n", + "print(\"Arr reversed: \\n\", np.flip(arr))\n" ] } ], From dc217abd31aaacae21f18e606c46bb3dd27c0328 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Mon, 19 Jan 2026 04:20:32 -0800 Subject: [PATCH 16/32] initial week3 commit with new branch that is branched off of the homework branch --- week_3_numpy.ipynb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index d026fbd..879c7bf 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -934,7 +934,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -965,8 +965,6 @@ "\n", "\n", "# Print out all all odd values\n", - "cond19 = arr % 2 \n", - "\n", "odds = arr[arr % 2 == 1]\n", "print(\"Odds: \\n\", odds, \"\\n\")\n", "\n", From 9fb3822a0394388523a57c430957877b2fa5b3f7 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Wed, 21 Jan 2026 01:50:01 -0800 Subject: [PATCH 17/32] fixed a couple issues --- week_3_numpy.ipynb | 127 ++++++++++++++++++++++++++++----------------- 1 file changed, 79 insertions(+), 48 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index 879c7bf..cd71e72 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -20,6 +20,11 @@ "* Can map data directly onto underlying disk or memory representation\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, { "cell_type": "markdown", "metadata": { @@ -75,7 +80,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -113,7 +118,7 @@ "print(\"shape:\\t\", numbers.shape) # ndarrays have shape (numer of rows & columns)\n", "print(\"size:\\t\", numbers.size) # number of elements in the array\n", "print(\"datatype\", numbers.dtype) # numpy determines the datatype\n", - "print(\"bytes:\\t\", numbers.size * numbers.itemsize) # total bytes consumed by the array" + "print(\"bytes:\\t\", numbers.nbytes) # total bytes consumed by the array" ] }, { @@ -127,7 +132,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -151,8 +156,8 @@ " [0. 0. 0. 0.]] \n", "\n", "random\n", - "[[ 0.38370392 -0.69051018]\n", - " [-0.69090326 -0.36937786]] \n", + "[[0.52286842 0.27818666]\n", + " [1.20170613 0.13236204]] \n", "\n", "empty\n", "[[0. 0.]\n", @@ -204,7 +209,7 @@ "print(even, \"\\n\")\n", "\n", "# Create an array of the form [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] using np.linspace\n", - "line = np.linspace(0, 2, num = 9)\n", + "line = np.linspace(0, 2, 9)\n", "print(\"line\")\n", "print(line, \"\\n\")\n" ] @@ -233,7 +238,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -241,13 +246,22 @@ "id": "NSMJt_4x0qan", "outputId": "81e81c55-af1d-4149-dc7c-7edc8a1a582d" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5 6 7]\n" + ] + } + ], "source": [ "arr1d = np.array([0,1, 2, 3, 4, 5, 6, 7, 8])\n", "\n", "# using indexing select and output [5, 6, 7] from arr1d. Save this slice to a variable named slice_arr1d\n", "\n", - "slice_arr1d = arr1d[5:8]\n" + "slice_arr1d = arr1d[5:8]\n", + "print(slice_arr1d)\n" ] }, { @@ -259,7 +273,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -267,11 +281,20 @@ "id": "Xe-PXYhBp4bn", "outputId": "cf387d7a-d51f-4540-8d9a-571a9a24f7b5" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 0 1 2 3 4 5 12 12 12]\n" + ] + } + ], "source": [ "# Replace 6, 7, and 8 with 12 in arr1d using one line of code. Then print arr1d and slice_arr1d\n", "\n", - "arr1d[6:] = 12\n" + "arr1d[6:] = 12\n", + "print(arr1d)\n" ] }, { @@ -283,7 +306,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -291,11 +314,20 @@ "id": "Eh0My7Eaqe77", "outputId": "693ef4b3-97a0-4a42-d77a-740fa2cd81db" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 0 1 2 3 4 5 12 12 12]\n" + ] + } + ], "source": [ "# Create a copy of arr1d and name it new_array. Hint: You can't use new_array = arr1d\n", "\n", - "new_array = arr1d.copy()" + "new_array = arr1d.copy()\n", + "print(new_array)" ] }, { @@ -307,7 +339,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -337,7 +369,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -353,10 +385,9 @@ "[[2 3 4]\n", " [6 7 8]] \n", "\n", - "[[3]\n", - " [7]] \n", + "[3 7] \n", "\n", - "[[1 2 3 4]]\n" + "[1 2 3 4]\n" ] } ], @@ -368,10 +399,10 @@ "print(arr2d[:2, 1:], \"\\n\")\n", "\n", "# select first two rows and only 3rd column\n", - "print(arr2d[:2, 2:3], \"\\n\")\n", + "print(arr2d[:2, 2], \"\\n\")\n", "\n", "# select all columns in the first row\n", - "print(arr2d[:1, :])\n" + "print(arr2d[0, :])\n" ] }, { @@ -394,7 +425,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -408,17 +439,17 @@ "output_type": "stream", "text": [ "First and fourth rows of data: \n", - "[[0 0 0 0 1 1 1]\n", - " [0 1 1 1 0 1 0]] \n", + "[[1 0 0 0 0 1 1]\n", + " [0 1 1 1 0 0 0]] \n", "\n", "All of data: \n", - "[[0 0 0 0 1 1 1]\n", - " [1 1 0 1 1 0 0]\n", - " [1 0 0 0 1 0 1]\n", - " [0 1 1 1 0 1 0]\n", - " [0 1 1 1 1 1 1]\n", - " [0 0 0 0 1 1 0]\n", - " [0 1 0 0 0 0 0]]\n" + "[[1 0 0 0 0 1 1]\n", + " [1 1 0 1 1 1 1]\n", + " [0 1 0 1 0 0 1]\n", + " [0 1 1 1 0 0 0]\n", + " [1 0 1 0 1 1 1]\n", + " [1 1 1 1 1 0 0]\n", + " [1 0 0 1 0 1 0]]\n" ] } ], @@ -447,7 +478,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 13, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -459,11 +490,11 @@ { "data": { "text/plain": [ - "array([[0, 0, 0, 0, 1, 1, 1],\n", - " [0, 1, 1, 1, 0, 1, 0]])" + "array([[1, 0, 0, 0, 0, 1, 1],\n", + " [0, 1, 1, 1, 0, 0, 0]])" ] }, - "execution_count": 34, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -485,7 +516,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 14, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -497,14 +528,14 @@ { "data": { "text/plain": [ - "array([[1, 1, 0, 1, 1, 0, 0],\n", - " [1, 0, 0, 0, 1, 0, 1],\n", - " [0, 1, 1, 1, 1, 1, 1],\n", - " [0, 0, 0, 0, 1, 1, 0],\n", - " [0, 1, 0, 0, 0, 0, 0]])" + "array([[1, 1, 0, 1, 1, 1, 1],\n", + " [0, 1, 0, 1, 0, 0, 1],\n", + " [1, 0, 1, 0, 1, 1, 1],\n", + " [1, 1, 1, 1, 1, 0, 0],\n", + " [1, 0, 0, 1, 0, 1, 0]])" ] }, - "execution_count": 35, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -527,7 +558,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -539,11 +570,11 @@ { "data": { "text/plain": [ - "array([[1, 1, 1],\n", - " [0, 1, 0]])" + "array([[0, 0, 0, 1, 1],\n", + " [1, 1, 0, 0, 0]])" ] }, - "execution_count": 36, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -551,7 +582,7 @@ "source": [ "# Select the last 5 columns for the first and forth row\n", "cond = (names == \"Bob\")\n", - "data[cond, 4:]" + "data[cond, 2:]" ] }, { From 3bd3577334413a14ec56d949b5d7e57369477e70 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Sat, 24 Jan 2026 03:25:36 -0800 Subject: [PATCH 18/32] initial commit for week4 --- week_4_panadas.ipynb | 52 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/week_4_panadas.ipynb b/week_4_panadas.ipynb index 9080a02..07c19f7 100644 --- a/week_4_panadas.ipynb +++ b/week_4_panadas.ipynb @@ -52,10 +52,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 6\n", + "1 7\n", + "2 8\n", + "3 9\n", + "dtype: int64" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = pd.Series([6,7,8,9])\n", + "x" + ] }, { "cell_type": "markdown", @@ -68,8 +86,26 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "a 6\n", + "d 7\n", + "b 8\n", + "c 9\n", + "dtype: int64" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = pd.Series([6,7,8,9], index = [\"a\",\"d\",\"b\",\"c\"])\n", + "x[[\"d\"]]" + ] }, { "cell_type": "markdown", @@ -344,7 +380,7 @@ ], "metadata": { "kernelspec": { - "display_name": "base", + "display_name": "ml-env", "language": "python", "name": "python3" }, @@ -358,7 +394,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.4" + "version": "3.10.18" } }, "nbformat": 4, From 2a4e5aed3f30f0df3dce36dbaaf79addf4f9aa2e Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Sat, 24 Jan 2026 03:50:29 -0800 Subject: [PATCH 19/32] completed up to problem 5 --- week_4_panadas.ipynb | 235 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 219 insertions(+), 16 deletions(-) diff --git a/week_4_panadas.ipynb b/week_4_panadas.ipynb index 07c19f7..d750096 100644 --- a/week_4_panadas.ipynb +++ b/week_4_panadas.ipynb @@ -84,20 +84,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "a 6\n", "d 7\n", - "b 8\n", - "c 9\n", "dtype: int64" ] }, - "execution_count": 5, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -121,10 +118,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "Ohio 35000\n", + "Texas 71000\n", + "Oregon 16000\n", + "Utah 5000\n", + "dtype: int64" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "states_dict = {\"Ohio\":35000, \"Texas\":71000, \"Oregon\":16000, \"Utah\":5000}\n", + "\n", + "states_series = pd.Series(states_dict)\n", + "states_series" + ] }, { "cell_type": "markdown", @@ -135,10 +152,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "OH 35000\n", + "TX 71000\n", + "OR 16000\n", + "UT 5000\n", + "dtype: int64" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "states_series.index = [\"OH\", \"TX\", \"OR\", \"UT\"]\n", + "\n", + "states_series" + ] }, { "cell_type": "markdown", @@ -161,9 +197,91 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
stateyearpop
0Ohio20001.5
1Ohio20011.7
2Ohio20023.6
3Nevada20012.4
4Nevada20022.9
5Nevada20033.2
\n", + "
" + ], + "text/plain": [ + " state year pop\n", + "0 Ohio 2000 1.5\n", + "1 Ohio 2001 1.7\n", + "2 Ohio 2002 3.6\n", + "3 Nevada 2001 2.4\n", + "4 Nevada 2002 2.9\n", + "5 Nevada 2003 3.2" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "data = {\n", " 'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],\n", @@ -184,10 +302,95 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
stateyearpop
zeroOhio20001.5
oneOhio20011.7
twoOhio20023.6
threeNevada20012.4
fourNevada20022.9
fiveNevada20033.2
\n", + "
" + ], + "text/plain": [ + " state year pop\n", + "zero Ohio 2000 1.5\n", + "one Ohio 2001 1.7\n", + "two Ohio 2002 3.6\n", + "three Nevada 2001 2.4\n", + "four Nevada 2002 2.9\n", + "five Nevada 2003 3.2" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.index = [\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\"]\n", + "df" + ] }, { "cell_type": "markdown", From 99c9d37893fac503ed48dbdcac6d26fb95dd4ea4 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Sun, 25 Jan 2026 08:12:13 -0800 Subject: [PATCH 20/32] completed through problem 13 --- week_4_panadas.ipynb | 650 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 615 insertions(+), 35 deletions(-) diff --git a/week_4_panadas.ipynb b/week_4_panadas.ipynb index d750096..014c7ab 100644 --- a/week_4_panadas.ipynb +++ b/week_4_panadas.ipynb @@ -19,7 +19,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -197,7 +197,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -277,7 +277,7 @@ "5 Nevada 2003 3.2" ] }, - "execution_count": 16, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -302,7 +302,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -382,7 +382,7 @@ "five Nevada 2003 3.2" ] }, - "execution_count": 23, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -401,10 +401,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "zero Ohio\n", + "one Ohio\n", + "two Ohio\n", + "three Nevada\n", + "four Nevada\n", + "five Nevada\n", + "Name: state, dtype: object \n", + "\n", + "zero Ohio\n", + "one Ohio\n", + "two Ohio\n", + "three Nevada\n", + "four Nevada\n", + "five Nevada\n", + "Name: state, dtype: object\n" + ] + } + ], + "source": [ + "print(df[\"state\"], \"\\n\")\n", + "print(df.state)\n" + ] }, { "cell_type": "markdown", @@ -415,10 +440,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "state Ohio\n", + "year 2000\n", + "pop 1.5\n", + "Name: zero, dtype: object \n", + "\n", + "state Ohio\n", + "year 2000\n", + "pop 1.5\n", + "Name: zero, dtype: object\n" + ] + } + ], + "source": [ + "print(df.loc[\"zero\"], \"\\n\")\n", + "print(df.iloc[0])" + ] }, { "cell_type": "markdown", @@ -429,10 +473,102 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
stateyearpoprating
zeroOhio20001.55
oneOhio20011.74
twoOhio20023.63
threeNevada20012.42
fourNevada20022.91
fiveNevada20033.20
\n", + "
" + ], + "text/plain": [ + " state year pop rating\n", + "zero Ohio 2000 1.5 5\n", + "one Ohio 2001 1.7 4\n", + "two Ohio 2002 3.6 3\n", + "three Nevada 2001 2.4 2\n", + "four Nevada 2002 2.9 1\n", + "five Nevada 2003 3.2 0" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[\"rating\"] = [5,4,3,2,1,0]\n", + "df" + ] }, { "cell_type": "markdown", @@ -443,10 +579,109 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
stateyearpopratingnonsense
zeroOhio20001.557.5
oneOhio20011.746.8
twoOhio20023.6310.8
threeNevada20012.424.8
fourNevada20022.912.9
fiveNevada20033.200.0
\n", + "
" + ], + "text/plain": [ + " state year pop rating nonsense\n", + "zero Ohio 2000 1.5 5 7.5\n", + "one Ohio 2001 1.7 4 6.8\n", + "two Ohio 2002 3.6 3 10.8\n", + "three Nevada 2001 2.4 2 4.8\n", + "four Nevada 2002 2.9 1 2.9\n", + "five Nevada 2003 3.2 0 0.0" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[\"nonsense\"] = df[\"rating\"] * df[\"pop\"]\n", + "df" + ] }, { "cell_type": "markdown", @@ -463,10 +698,94 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 70, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
abcde
series_numerical0.0000001.0000002.0000003.0000004.000000
series_zeros0.0000000.0000000.0000000.0000000.000000
series_random0.7382310.9013290.1695780.8019320.646176
\n", + "
" + ], + "text/plain": [ + " a b c d e\n", + "series_numerical 0.000000 1.000000 2.000000 3.000000 4.000000\n", + "series_zeros 0.000000 0.000000 0.000000 0.000000 0.000000\n", + "series_random 0.738231 0.901329 0.169578 0.801932 0.646176" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "idx = pd.Index(list(\"abcde\"))\n", + "series_numerical = pd.Series(np.arange(5), index = idx)\n", + "series_zeros = pd.Series(np.zeros(5), index = idx)\n", + "series_random = pd.Series(np.random.rand(5), index = idx)\n", + "\n", + "series_list = list([series_numerical, series_zeros, series_random])\n", + "series_index = pd.Index([\"series_numerical\", \"series_zeros\", \"series_random\"])\n", + "numeric_df = pd.DataFrame(series_list, index = series_index)\n", + "\n", + "numeric_df\n", + "\n", + "# I did consult chatgpt on this concerning the style I wrote with declaring the few extra variables (idx, series_list, series_index) and \n", + "# potential other ways to write it\n", + "\n" + ] }, { "cell_type": "markdown", @@ -477,10 +796,89 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 71, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandom
a0.00.00.738231
b1.00.00.901329
c2.00.00.169578
d3.00.00.801932
e4.00.00.646176
\n", + "
" + ], + "text/plain": [ + " numerical zeros random\n", + "a 0.0 0.0 0.738231\n", + "b 1.0 0.0 0.901329\n", + "c 2.0 0.0 0.169578\n", + "d 3.0 0.0 0.801932\n", + "e 4.0 0.0 0.646176" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transposed_numeric_df = numeric_df.T \n", + "transposed_numeric_df.columns = [\"numerical\", \"zeros\", \"random\"]\n", + "transposed_numeric_df" + ] }, { "cell_type": "markdown", @@ -491,10 +889,88 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandom
b1.00.00.901329
d3.00.00.801932
a0.00.00.738231
e4.00.00.646176
c2.00.00.169578
\n", + "
" + ], + "text/plain": [ + " numerical zeros random\n", + "b 1.0 0.0 0.901329\n", + "d 3.0 0.0 0.801932\n", + "a 0.0 0.0 0.738231\n", + "e 4.0 0.0 0.646176\n", + "c 2.0 0.0 0.169578" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transposed_numeric_df_random_descending = transposed_numeric_df.sort_values(\"random\", ascending = False)\n", + "transposed_numeric_df_random_descending" + ] }, { "cell_type": "markdown", @@ -505,10 +981,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandom
d3.00.00.801932
e4.00.00.646176
\n", + "
" + ], + "text/plain": [ + " numerical zeros random\n", + "d 3.0 0.0 0.801932\n", + "e 4.0 0.0 0.646176" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "greater_than_2 = transposed_numeric_df.loc[transposed_numeric_df.numerical > 2]\n", + "greater_than_2" + ] }, { "cell_type": "markdown", @@ -519,10 +1052,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 91, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "a False\n", + "b False\n", + "c True\n", + "d False\n", + "e True\n", + "dtype: bool" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transposed_numeric_df[\"random5\"] = transposed_numeric_df[\"random\"] * 5\n", + "greater_than_random_5 = transposed_numeric_df[\"numerical\"] > transposed_numeric_df[\"random5\"]\n", + "greater_than_random_5" + ] }, { "cell_type": "markdown", @@ -533,10 +1086,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 90, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "ename": "KeyError", + "evalue": "\"Column(s) ['a', 'b', 'c', 'd', 'e'] do not exist\"", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[90], line 7\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m----> 7\u001b[0m transposed_numeric_df[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124meven\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[43mtransposed_numeric_df\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtransposed_numeric_df\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mnumerical\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 8\u001b[0m transposed_numeric_df\n", + "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/frame.py:10381\u001b[0m, in \u001b[0;36mDataFrame.apply\u001b[0;34m(self, func, axis, raw, result_type, args, by_row, engine, engine_kwargs, **kwargs)\u001b[0m\n\u001b[1;32m 10367\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mpandas\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mapply\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m frame_apply\n\u001b[1;32m 10369\u001b[0m op \u001b[38;5;241m=\u001b[39m frame_apply(\n\u001b[1;32m 10370\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 10371\u001b[0m func\u001b[38;5;241m=\u001b[39mfunc,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 10379\u001b[0m kwargs\u001b[38;5;241m=\u001b[39mkwargs,\n\u001b[1;32m 10380\u001b[0m )\n\u001b[0;32m> 10381\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mop\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39m__finalize__(\u001b[38;5;28mself\u001b[39m, method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mapply\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/apply.py:873\u001b[0m, in \u001b[0;36mFrameApply.apply\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 869\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mengine \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnumba\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 870\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\n\u001b[1;32m 871\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mthe \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnumba\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m engine doesn\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt support lists of callables yet\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 872\u001b[0m )\n\u001b[0;32m--> 873\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply_list_or_dict_like\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 875\u001b[0m \u001b[38;5;66;03m# all empty\u001b[39;00m\n\u001b[1;32m 876\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcolumns) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mindex) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n", + "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/apply.py:628\u001b[0m, in \u001b[0;36mApply.apply_list_or_dict_like\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 625\u001b[0m kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mkwargs\n\u001b[1;32m 627\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_dict_like(func):\n\u001b[0;32m--> 628\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43magg_or_apply_dict_like\u001b[49m\u001b[43m(\u001b[49m\u001b[43mop_name\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mapply\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 629\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 630\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39magg_or_apply_list_like(op_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mapply\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/apply.py:763\u001b[0m, in \u001b[0;36mNDFrameApply.agg_or_apply_dict_like\u001b[0;34m(self, op_name)\u001b[0m\n\u001b[1;32m 760\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124maxis other than 0 is not supported\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 762\u001b[0m selection \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m--> 763\u001b[0m result_index, result_data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompute_dict_like\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 764\u001b[0m \u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mselection\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\n\u001b[1;32m 765\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 766\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mwrap_results_dict_like(obj, result_index, result_data)\n\u001b[1;32m 767\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", + "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/apply.py:462\u001b[0m, in \u001b[0;36mApply.compute_dict_like\u001b[0;34m(self, op_name, selected_obj, selection, kwargs)\u001b[0m\n\u001b[1;32m 460\u001b[0m is_groupby \u001b[38;5;241m=\u001b[39m \u001b[38;5;28misinstance\u001b[39m(obj, (DataFrameGroupBy, SeriesGroupBy))\n\u001b[1;32m 461\u001b[0m func \u001b[38;5;241m=\u001b[39m cast(AggFuncTypeDict, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfunc)\n\u001b[0;32m--> 462\u001b[0m func \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnormalize_dictlike_arg\u001b[49m\u001b[43m(\u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mselected_obj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 464\u001b[0m is_non_unique_col \u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m 465\u001b[0m selected_obj\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m2\u001b[39m\n\u001b[1;32m 466\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m selected_obj\u001b[38;5;241m.\u001b[39mcolumns\u001b[38;5;241m.\u001b[39mnunique() \u001b[38;5;241m<\u001b[39m \u001b[38;5;28mlen\u001b[39m(selected_obj\u001b[38;5;241m.\u001b[39mcolumns)\n\u001b[1;32m 467\u001b[0m )\n\u001b[1;32m 469\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m selected_obj\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 470\u001b[0m \u001b[38;5;66;03m# key only used for output\u001b[39;00m\n", + "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/apply.py:663\u001b[0m, in \u001b[0;36mApply.normalize_dictlike_arg\u001b[0;34m(self, how, obj, func)\u001b[0m\n\u001b[1;32m 661\u001b[0m cols \u001b[38;5;241m=\u001b[39m Index(\u001b[38;5;28mlist\u001b[39m(func\u001b[38;5;241m.\u001b[39mkeys()))\u001b[38;5;241m.\u001b[39mdifference(obj\u001b[38;5;241m.\u001b[39mcolumns, sort\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 662\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(cols) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m--> 663\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mColumn(s) \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlist\u001b[39m(cols)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m do not exist\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 665\u001b[0m aggregator_types \u001b[38;5;241m=\u001b[39m (\u001b[38;5;28mlist\u001b[39m, \u001b[38;5;28mtuple\u001b[39m, \u001b[38;5;28mdict\u001b[39m)\n\u001b[1;32m 667\u001b[0m \u001b[38;5;66;03m# if we have a dict of any non-scalars\u001b[39;00m\n\u001b[1;32m 668\u001b[0m \u001b[38;5;66;03m# eg. {'A' : ['mean']}, normalize all to\u001b[39;00m\n\u001b[1;32m 669\u001b[0m \u001b[38;5;66;03m# be list-likes\u001b[39;00m\n\u001b[1;32m 670\u001b[0m \u001b[38;5;66;03m# Cannot use func.values() because arg may be a Series\u001b[39;00m\n", + "\u001b[0;31mKeyError\u001b[0m: \"Column(s) ['a', 'b', 'c', 'd', 'e'] do not exist\"" + ] + } + ], + "source": [ + "def is_even(x):\n", + " if x[\"numerical\"] % 2 == 0:\n", + " return True\n", + " else:\n", + " False\n", + "\n", + "transposed_numeric_df[\"even\"] = transposed_numeric_df.apply(transposed_numeric_df[\"numerical\"])\n", + "transposed_numeric_df" + ] }, { "cell_type": "markdown", From 9511d22dce7b99236d734fc8a435f47ac8b2b4d3 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Mon, 26 Jan 2026 06:01:47 -0800 Subject: [PATCH 21/32] finished through problem 17 --- week_4_panadas.ipynb | 421 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 336 insertions(+), 85 deletions(-) diff --git a/week_4_panadas.ipynb b/week_4_panadas.ipynb index 014c7ab..0d87d75 100644 --- a/week_4_panadas.ipynb +++ b/week_4_panadas.ipynb @@ -19,11 +19,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "#As allways we import pandas and numpy\n", + "#As always we import pandas and numpy\n", "import pandas as pd\n", "import numpy as np" ] @@ -698,7 +698,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -748,11 +748,11 @@ " \n", " \n", " series_random\n", - " 0.738231\n", - " 0.901329\n", - " 0.169578\n", - " 0.801932\n", - " 0.646176\n", + " 0.693278\n", + " 0.723256\n", + " 0.468126\n", + " 0.056753\n", + " 0.987466\n", " \n", " \n", "\n", @@ -762,10 +762,10 @@ " a b c d e\n", "series_numerical 0.000000 1.000000 2.000000 3.000000 4.000000\n", "series_zeros 0.000000 0.000000 0.000000 0.000000 0.000000\n", - "series_random 0.738231 0.901329 0.169578 0.801932 0.646176" + "series_random 0.693278 0.723256 0.468126 0.056753 0.987466" ] }, - "execution_count": 70, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -796,7 +796,7 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -830,31 +830,31 @@ " a\n", " 0.0\n", " 0.0\n", - " 0.738231\n", + " 0.693278\n", " \n", " \n", " b\n", " 1.0\n", " 0.0\n", - " 0.901329\n", + " 0.723256\n", " \n", " \n", " c\n", " 2.0\n", " 0.0\n", - " 0.169578\n", + " 0.468126\n", " \n", " \n", " d\n", " 3.0\n", " 0.0\n", - " 0.801932\n", + " 0.056753\n", " \n", " \n", " e\n", " 4.0\n", " 0.0\n", - " 0.646176\n", + " 0.987466\n", " \n", " \n", "\n", @@ -862,14 +862,14 @@ ], "text/plain": [ " numerical zeros random\n", - "a 0.0 0.0 0.738231\n", - "b 1.0 0.0 0.901329\n", - "c 2.0 0.0 0.169578\n", - "d 3.0 0.0 0.801932\n", - "e 4.0 0.0 0.646176" + "a 0.0 0.0 0.693278\n", + "b 1.0 0.0 0.723256\n", + "c 2.0 0.0 0.468126\n", + "d 3.0 0.0 0.056753\n", + "e 4.0 0.0 0.987466" ] }, - "execution_count": 71, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -889,7 +889,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -920,34 +920,34 @@ " \n", " \n", " \n", - " b\n", - " 1.0\n", + " e\n", + " 4.0\n", " 0.0\n", - " 0.901329\n", + " 0.987466\n", " \n", " \n", - " d\n", - " 3.0\n", + " b\n", + " 1.0\n", " 0.0\n", - " 0.801932\n", + " 0.723256\n", " \n", " \n", " a\n", " 0.0\n", " 0.0\n", - " 0.738231\n", + " 0.693278\n", " \n", " \n", - " e\n", - " 4.0\n", + " c\n", + " 2.0\n", " 0.0\n", - " 0.646176\n", + " 0.468126\n", " \n", " \n", - " c\n", - " 2.0\n", + " d\n", + " 3.0\n", " 0.0\n", - " 0.169578\n", + " 0.056753\n", " \n", " \n", "\n", @@ -955,14 +955,14 @@ ], "text/plain": [ " numerical zeros random\n", - "b 1.0 0.0 0.901329\n", - "d 3.0 0.0 0.801932\n", - "a 0.0 0.0 0.738231\n", - "e 4.0 0.0 0.646176\n", - "c 2.0 0.0 0.169578" + "e 4.0 0.0 0.987466\n", + "b 1.0 0.0 0.723256\n", + "a 0.0 0.0 0.693278\n", + "c 2.0 0.0 0.468126\n", + "d 3.0 0.0 0.056753" ] }, - "execution_count": 72, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -981,7 +981,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -1015,13 +1015,13 @@ " d\n", " 3.0\n", " 0.0\n", - " 0.801932\n", + " 0.056753\n", " \n", " \n", " e\n", " 4.0\n", " 0.0\n", - " 0.646176\n", + " 0.987466\n", " \n", " \n", "\n", @@ -1029,11 +1029,11 @@ ], "text/plain": [ " numerical zeros random\n", - "d 3.0 0.0 0.801932\n", - "e 4.0 0.0 0.646176" + "d 3.0 0.0 0.056753\n", + "e 4.0 0.0 0.987466" ] }, - "execution_count": 80, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -1052,28 +1052,78 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 15, "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " numerical zeros random random5\n", + "a 0.0 0.0 0.693278 3.466389\n", + "b 1.0 0.0 0.723256 3.616281\n", + "c 2.0 0.0 0.468126 2.340629\n", + "d 3.0 0.0 0.056753 0.283767\n", + "e 4.0 0.0 0.987466 4.937328\n", + "-----------------------------------------------------------------\n", + "All rows where numerical is greater than random5:\n" + ] + }, { "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandomrandom5
d3.00.00.0567530.283767
\n", + "
" + ], "text/plain": [ - "a False\n", - "b False\n", - "c True\n", - "d False\n", - "e True\n", - "dtype: bool" + " numerical zeros random random5\n", + "d 3.0 0.0 0.056753 0.283767" ] }, - "execution_count": 91, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transposed_numeric_df[\"random5\"] = transposed_numeric_df[\"random\"] * 5\n", - "greater_than_random_5 = transposed_numeric_df[\"numerical\"] > transposed_numeric_df[\"random5\"]\n", + "greater_than_random_5 = transposed_numeric_df[transposed_numeric_df[\"numerical\"] > transposed_numeric_df[\"random5\"]]\n", + "print(transposed_numeric_df)\n", + "print(\"-----------------------------------------------------------------\")\n", + "print(\"All rows where numerical is greater than random5:\")\n", "greater_than_random_5" ] }, @@ -1086,35 +1136,98 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 16, "metadata": {}, "outputs": [ { - "ename": "KeyError", - "evalue": "\"Column(s) ['a', 'b', 'c', 'd', 'e'] do not exist\"", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[90], line 7\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m----> 7\u001b[0m transposed_numeric_df[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124meven\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[43mtransposed_numeric_df\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtransposed_numeric_df\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mnumerical\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 8\u001b[0m transposed_numeric_df\n", - "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/frame.py:10381\u001b[0m, in \u001b[0;36mDataFrame.apply\u001b[0;34m(self, func, axis, raw, result_type, args, by_row, engine, engine_kwargs, **kwargs)\u001b[0m\n\u001b[1;32m 10367\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mpandas\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mapply\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m frame_apply\n\u001b[1;32m 10369\u001b[0m op \u001b[38;5;241m=\u001b[39m frame_apply(\n\u001b[1;32m 10370\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 10371\u001b[0m func\u001b[38;5;241m=\u001b[39mfunc,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 10379\u001b[0m kwargs\u001b[38;5;241m=\u001b[39mkwargs,\n\u001b[1;32m 10380\u001b[0m )\n\u001b[0;32m> 10381\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mop\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39m__finalize__(\u001b[38;5;28mself\u001b[39m, method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mapply\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", - "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/apply.py:873\u001b[0m, in \u001b[0;36mFrameApply.apply\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 869\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mengine \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnumba\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 870\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\n\u001b[1;32m 871\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mthe \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnumba\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m engine doesn\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt support lists of callables yet\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 872\u001b[0m )\n\u001b[0;32m--> 873\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply_list_or_dict_like\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 875\u001b[0m \u001b[38;5;66;03m# all empty\u001b[39;00m\n\u001b[1;32m 876\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcolumns) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mindex) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n", - "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/apply.py:628\u001b[0m, in \u001b[0;36mApply.apply_list_or_dict_like\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 625\u001b[0m kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mkwargs\n\u001b[1;32m 627\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_dict_like(func):\n\u001b[0;32m--> 628\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43magg_or_apply_dict_like\u001b[49m\u001b[43m(\u001b[49m\u001b[43mop_name\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mapply\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 629\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 630\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39magg_or_apply_list_like(op_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mapply\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", - "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/apply.py:763\u001b[0m, in \u001b[0;36mNDFrameApply.agg_or_apply_dict_like\u001b[0;34m(self, op_name)\u001b[0m\n\u001b[1;32m 760\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124maxis other than 0 is not supported\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 762\u001b[0m selection \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m--> 763\u001b[0m result_index, result_data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompute_dict_like\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 764\u001b[0m \u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mselection\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\n\u001b[1;32m 765\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 766\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mwrap_results_dict_like(obj, result_index, result_data)\n\u001b[1;32m 767\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", - "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/apply.py:462\u001b[0m, in \u001b[0;36mApply.compute_dict_like\u001b[0;34m(self, op_name, selected_obj, selection, kwargs)\u001b[0m\n\u001b[1;32m 460\u001b[0m is_groupby \u001b[38;5;241m=\u001b[39m \u001b[38;5;28misinstance\u001b[39m(obj, (DataFrameGroupBy, SeriesGroupBy))\n\u001b[1;32m 461\u001b[0m func \u001b[38;5;241m=\u001b[39m cast(AggFuncTypeDict, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfunc)\n\u001b[0;32m--> 462\u001b[0m func \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnormalize_dictlike_arg\u001b[49m\u001b[43m(\u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mselected_obj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 464\u001b[0m is_non_unique_col \u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m 465\u001b[0m selected_obj\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m2\u001b[39m\n\u001b[1;32m 466\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m selected_obj\u001b[38;5;241m.\u001b[39mcolumns\u001b[38;5;241m.\u001b[39mnunique() \u001b[38;5;241m<\u001b[39m \u001b[38;5;28mlen\u001b[39m(selected_obj\u001b[38;5;241m.\u001b[39mcolumns)\n\u001b[1;32m 467\u001b[0m )\n\u001b[1;32m 469\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m selected_obj\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 470\u001b[0m \u001b[38;5;66;03m# key only used for output\u001b[39;00m\n", - "File \u001b[0;32m/opt/anaconda3/envs/ml-env/lib/python3.10/site-packages/pandas/core/apply.py:663\u001b[0m, in \u001b[0;36mApply.normalize_dictlike_arg\u001b[0;34m(self, how, obj, func)\u001b[0m\n\u001b[1;32m 661\u001b[0m cols \u001b[38;5;241m=\u001b[39m Index(\u001b[38;5;28mlist\u001b[39m(func\u001b[38;5;241m.\u001b[39mkeys()))\u001b[38;5;241m.\u001b[39mdifference(obj\u001b[38;5;241m.\u001b[39mcolumns, sort\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 662\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(cols) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m--> 663\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mColumn(s) \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlist\u001b[39m(cols)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m do not exist\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 665\u001b[0m aggregator_types \u001b[38;5;241m=\u001b[39m (\u001b[38;5;28mlist\u001b[39m, \u001b[38;5;28mtuple\u001b[39m, \u001b[38;5;28mdict\u001b[39m)\n\u001b[1;32m 667\u001b[0m \u001b[38;5;66;03m# if we have a dict of any non-scalars\u001b[39;00m\n\u001b[1;32m 668\u001b[0m \u001b[38;5;66;03m# eg. {'A' : ['mean']}, normalize all to\u001b[39;00m\n\u001b[1;32m 669\u001b[0m \u001b[38;5;66;03m# be list-likes\u001b[39;00m\n\u001b[1;32m 670\u001b[0m \u001b[38;5;66;03m# Cannot use func.values() because arg may be a Series\u001b[39;00m\n", - "\u001b[0;31mKeyError\u001b[0m: \"Column(s) ['a', 'b', 'c', 'd', 'e'] do not exist\"" - ] + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandomrandom5even
a0.00.00.6932783.466389True
b1.00.00.7232563.616281False
c2.00.00.4681262.340629True
d3.00.00.0567530.283767False
e4.00.00.9874664.937328True
\n", + "
" + ], + "text/plain": [ + " numerical zeros random random5 even\n", + "a 0.0 0.0 0.693278 3.466389 True\n", + "b 1.0 0.0 0.723256 3.616281 False\n", + "c 2.0 0.0 0.468126 2.340629 True\n", + "d 3.0 0.0 0.056753 0.283767 False\n", + "e 4.0 0.0 0.987466 4.937328 True" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "def is_even(x):\n", - " if x[\"numerical\"] % 2 == 0:\n", - " return True\n", - " else:\n", - " False\n", - "\n", - "transposed_numeric_df[\"even\"] = transposed_numeric_df.apply(transposed_numeric_df[\"numerical\"])\n", + "transposed_numeric_df[\"even\"] = transposed_numeric_df[\"numerical\"] % 2 == 0\n", "transposed_numeric_df" ] }, @@ -1127,10 +1240,121 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandomrandom5eveneven_odd
a0.00.00.6932783.466389Trueeven
b1.00.00.7232563.616281Falseodd
c2.00.00.4681262.340629Trueeven
d3.00.00.0567530.283767Falseodd
e4.00.00.9874664.937328Trueeven
\n", + "
" + ], + "text/plain": [ + " numerical zeros random random5 even even_odd\n", + "a 0.0 0.0 0.693278 3.466389 True even\n", + "b 1.0 0.0 0.723256 3.616281 False odd\n", + "c 2.0 0.0 0.468126 2.340629 True even\n", + "d 3.0 0.0 0.056753 0.283767 False odd\n", + "e 4.0 0.0 0.987466 4.937328 True even" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"\"\" \n", + "I wrote this two ways. I find the one that I commented out with the lambda function \n", + "is cleaner with less code as you don't need to write the separate function. I used the \n", + "second one as that is how it was shown in the book. I found the lambda version on the \n", + "geeksforgeeks website, it's in the url below this line. \n", + "https://www.geeksforgeeks.org/python/ways-to-apply-an-if-condition-in-pandas-dataframe/\n", + "\"\"\";\n", + "\n", + "def ev_odd(x):\n", + " if x == True:\n", + " return \"even\"\n", + " else:\n", + " return \"odd\"\n", + "\n", + "# transposed_numeric_df[\"even_odd\"] = transposed_numeric_df[\"even\"].apply(lambda x: \"even\" if x == True else \"odd\")\n", + "transposed_numeric_df[\"even_odd\"] = transposed_numeric_df[\"even\"].apply(ev_odd)\n", + "transposed_numeric_df" + ] }, { "cell_type": "markdown", @@ -1143,8 +1367,35 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "numerical 10.000000\n", + "zeros 0.000000\n", + "random 2.928879\n", + "random5 14.644394\n", + "even 3.000000\n", + "dtype: float64" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"\"\"\n", + "I purposely filtered out the even_odd column as calculating the sum just \n", + "creates a long string of evenoodevenoddeven. I left the even column in, \n", + "even though is just True/False as technically True == 1 and False == 0. So \n", + "you can technically add them. \n", + "\"\"\";\n", + "\n", + "columns = [\"numerical\", \"zeros\", \"random\", \"random5\", \"even\"]\n", + "\n", + "transposed_numeric_df[columns].sum(axis = 0)" + ] }, { "cell_type": "markdown", From af7655b9f88b25ea59f43b1121e067c3b1f983e8 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Tue, 27 Jan 2026 04:28:44 -0800 Subject: [PATCH 22/32] initial commit --- week_4_panadas.ipynb | 305 +++++++++++++++++++++++++++++-------------- 1 file changed, 206 insertions(+), 99 deletions(-) diff --git a/week_4_panadas.ipynb b/week_4_panadas.ipynb index 0d87d75..5c086de 100644 --- a/week_4_panadas.ipynb +++ b/week_4_panadas.ipynb @@ -19,7 +19,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -698,7 +698,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -748,11 +748,11 @@ " \n", " \n", " series_random\n", - " 0.693278\n", - " 0.723256\n", - " 0.468126\n", - " 0.056753\n", - " 0.987466\n", + " 0.857157\n", + " 0.611313\n", + " 0.290102\n", + " 0.523485\n", + " 0.955533\n", " \n", " \n", "\n", @@ -762,10 +762,10 @@ " a b c d e\n", "series_numerical 0.000000 1.000000 2.000000 3.000000 4.000000\n", "series_zeros 0.000000 0.000000 0.000000 0.000000 0.000000\n", - "series_random 0.693278 0.723256 0.468126 0.056753 0.987466" + "series_random 0.857157 0.611313 0.290102 0.523485 0.955533" ] }, - "execution_count": 3, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -796,7 +796,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -830,31 +830,31 @@ " a\n", " 0.0\n", " 0.0\n", - " 0.693278\n", + " 0.857157\n", " \n", " \n", " b\n", " 1.0\n", " 0.0\n", - " 0.723256\n", + " 0.611313\n", " \n", " \n", " c\n", " 2.0\n", " 0.0\n", - " 0.468126\n", + " 0.290102\n", " \n", " \n", " d\n", " 3.0\n", " 0.0\n", - " 0.056753\n", + " 0.523485\n", " \n", " \n", " e\n", " 4.0\n", " 0.0\n", - " 0.987466\n", + " 0.955533\n", " \n", " \n", "\n", @@ -862,14 +862,14 @@ ], "text/plain": [ " numerical zeros random\n", - "a 0.0 0.0 0.693278\n", - "b 1.0 0.0 0.723256\n", - "c 2.0 0.0 0.468126\n", - "d 3.0 0.0 0.056753\n", - "e 4.0 0.0 0.987466" + "a 0.0 0.0 0.857157\n", + "b 1.0 0.0 0.611313\n", + "c 2.0 0.0 0.290102\n", + "d 3.0 0.0 0.523485\n", + "e 4.0 0.0 0.955533" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -889,7 +889,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -923,31 +923,31 @@ " e\n", " 4.0\n", " 0.0\n", - " 0.987466\n", - " \n", - " \n", - " b\n", - " 1.0\n", - " 0.0\n", - " 0.723256\n", + " 0.955533\n", " \n", " \n", " a\n", " 0.0\n", " 0.0\n", - " 0.693278\n", + " 0.857157\n", " \n", " \n", - " c\n", - " 2.0\n", + " b\n", + " 1.0\n", " 0.0\n", - " 0.468126\n", + " 0.611313\n", " \n", " \n", " d\n", " 3.0\n", " 0.0\n", - " 0.056753\n", + " 0.523485\n", + " \n", + " \n", + " c\n", + " 2.0\n", + " 0.0\n", + " 0.290102\n", " \n", " \n", "\n", @@ -955,14 +955,14 @@ ], "text/plain": [ " numerical zeros random\n", - "e 4.0 0.0 0.987466\n", - "b 1.0 0.0 0.723256\n", - "a 0.0 0.0 0.693278\n", - "c 2.0 0.0 0.468126\n", - "d 3.0 0.0 0.056753" + "e 4.0 0.0 0.955533\n", + "a 0.0 0.0 0.857157\n", + "b 1.0 0.0 0.611313\n", + "d 3.0 0.0 0.523485\n", + "c 2.0 0.0 0.290102" ] }, - "execution_count": 5, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -981,7 +981,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -1015,13 +1015,13 @@ " d\n", " 3.0\n", " 0.0\n", - " 0.056753\n", + " 0.523485\n", " \n", " \n", " e\n", " 4.0\n", " 0.0\n", - " 0.987466\n", + " 0.955533\n", " \n", " \n", "\n", @@ -1029,11 +1029,11 @@ ], "text/plain": [ " numerical zeros random\n", - "d 3.0 0.0 0.056753\n", - "e 4.0 0.0 0.987466" + "d 3.0 0.0 0.523485\n", + "e 4.0 0.0 0.955533" ] }, - "execution_count": 6, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -1052,7 +1052,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -1060,11 +1060,11 @@ "output_type": "stream", "text": [ " numerical zeros random random5\n", - "a 0.0 0.0 0.693278 3.466389\n", - "b 1.0 0.0 0.723256 3.616281\n", - "c 2.0 0.0 0.468126 2.340629\n", - "d 3.0 0.0 0.056753 0.283767\n", - "e 4.0 0.0 0.987466 4.937328\n", + "a 0.0 0.0 0.857157 4.285783\n", + "b 1.0 0.0 0.611313 3.056565\n", + "c 2.0 0.0 0.290102 1.450512\n", + "d 3.0 0.0 0.523485 2.617426\n", + "e 4.0 0.0 0.955533 4.777663\n", "-----------------------------------------------------------------\n", "All rows where numerical is greater than random5:\n" ] @@ -1098,11 +1098,18 @@ " \n", " \n", " \n", + " c\n", + " 2.0\n", + " 0.0\n", + " 0.290102\n", + " 1.450512\n", + " \n", + " \n", " d\n", " 3.0\n", " 0.0\n", - " 0.056753\n", - " 0.283767\n", + " 0.523485\n", + " 2.617426\n", " \n", " \n", "\n", @@ -1110,10 +1117,11 @@ ], "text/plain": [ " numerical zeros random random5\n", - "d 3.0 0.0 0.056753 0.283767" + "c 2.0 0.0 0.290102 1.450512\n", + "d 3.0 0.0 0.523485 2.617426" ] }, - "execution_count": 15, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -1136,7 +1144,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -1172,40 +1180,40 @@ " a\n", " 0.0\n", " 0.0\n", - " 0.693278\n", - " 3.466389\n", + " 0.857157\n", + " 4.285783\n", " True\n", " \n", " \n", " b\n", " 1.0\n", " 0.0\n", - " 0.723256\n", - " 3.616281\n", + " 0.611313\n", + " 3.056565\n", " False\n", " \n", " \n", " c\n", " 2.0\n", " 0.0\n", - " 0.468126\n", - " 2.340629\n", + " 0.290102\n", + " 1.450512\n", " True\n", " \n", " \n", " d\n", " 3.0\n", " 0.0\n", - " 0.056753\n", - " 0.283767\n", + " 0.523485\n", + " 2.617426\n", " False\n", " \n", " \n", " e\n", " 4.0\n", " 0.0\n", - " 0.987466\n", - " 4.937328\n", + " 0.955533\n", + " 4.777663\n", " True\n", " \n", " \n", @@ -1214,14 +1222,14 @@ ], "text/plain": [ " numerical zeros random random5 even\n", - "a 0.0 0.0 0.693278 3.466389 True\n", - "b 1.0 0.0 0.723256 3.616281 False\n", - "c 2.0 0.0 0.468126 2.340629 True\n", - "d 3.0 0.0 0.056753 0.283767 False\n", - "e 4.0 0.0 0.987466 4.937328 True" + "a 0.0 0.0 0.857157 4.285783 True\n", + "b 1.0 0.0 0.611313 3.056565 False\n", + "c 2.0 0.0 0.290102 1.450512 True\n", + "d 3.0 0.0 0.523485 2.617426 False\n", + "e 4.0 0.0 0.955533 4.777663 True" ] }, - "execution_count": 16, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -1240,7 +1248,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -1277,8 +1285,8 @@ " a\n", " 0.0\n", " 0.0\n", - " 0.693278\n", - " 3.466389\n", + " 0.857157\n", + " 4.285783\n", " True\n", " even\n", " \n", @@ -1286,8 +1294,8 @@ " b\n", " 1.0\n", " 0.0\n", - " 0.723256\n", - " 3.616281\n", + " 0.611313\n", + " 3.056565\n", " False\n", " odd\n", " \n", @@ -1295,8 +1303,8 @@ " c\n", " 2.0\n", " 0.0\n", - " 0.468126\n", - " 2.340629\n", + " 0.290102\n", + " 1.450512\n", " True\n", " even\n", " \n", @@ -1304,8 +1312,8 @@ " d\n", " 3.0\n", " 0.0\n", - " 0.056753\n", - " 0.283767\n", + " 0.523485\n", + " 2.617426\n", " False\n", " odd\n", " \n", @@ -1313,8 +1321,8 @@ " e\n", " 4.0\n", " 0.0\n", - " 0.987466\n", - " 4.937328\n", + " 0.955533\n", + " 4.777663\n", " True\n", " even\n", " \n", @@ -1324,14 +1332,14 @@ ], "text/plain": [ " numerical zeros random random5 even even_odd\n", - "a 0.0 0.0 0.693278 3.466389 True even\n", - "b 1.0 0.0 0.723256 3.616281 False odd\n", - "c 2.0 0.0 0.468126 2.340629 True even\n", - "d 3.0 0.0 0.056753 0.283767 False odd\n", - "e 4.0 0.0 0.987466 4.937328 True even" + "a 0.0 0.0 0.857157 4.285783 True even\n", + "b 1.0 0.0 0.611313 3.056565 False odd\n", + "c 2.0 0.0 0.290102 1.450512 True even\n", + "d 3.0 0.0 0.523485 2.617426 False odd\n", + "e 4.0 0.0 0.955533 4.777663 True even" ] }, - "execution_count": 28, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -1365,21 +1373,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "numerical 10.000000\n", - "zeros 0.000000\n", - "random 2.928879\n", - "random5 14.644394\n", - "even 3.000000\n", + "numerical 10.00000\n", + "zeros 0.00000\n", + "random 3.23759\n", + "random5 16.18795\n", + "even 3.00000\n", "dtype: float64" ] }, - "execution_count": 43, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -1408,8 +1416,107 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandomrandom5eveneven_odd
a0.00.00.8571574.285783Trueeven
b1.00.00.6113133.056565Falseodd
c2.00.00.2901021.450512Trueeven
d3.00.00.5234852.617426Falseodd
e4.00.00.9555334.777663Trueeven
\n", + "
" + ], + "text/plain": [ + " numerical zeros random random5 even even_odd\n", + "a 0.0 0.0 0.857157 4.285783 True even\n", + "b 1.0 0.0 0.611313 3.056565 False odd\n", + "c 2.0 0.0 0.290102 1.450512 True even\n", + "d 3.0 0.0 0.523485 2.617426 False odd\n", + "e 4.0 0.0 0.955533 4.777663 True even" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "columns = [\"numerical\", \"zeros\", \"random\", \"random5\", \"even\"]\n", + "\n", + "x = 5\n", + "\n", + "transposed_numeric_df" + ] } ], "metadata": { From b24941930dade70be1eeb98b358d4bcce521b7e9 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Tue, 27 Jan 2026 04:42:57 -0800 Subject: [PATCH 23/32] completed week4 initial homework --- week_4_panadas.ipynb | 99 ++++---------------------------------------- 1 file changed, 9 insertions(+), 90 deletions(-) diff --git a/week_4_panadas.ipynb b/week_4_panadas.ipynb index 5c086de..6ba43e0 100644 --- a/week_4_panadas.ipynb +++ b/week_4_panadas.ipynb @@ -1414,108 +1414,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [ { "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
numericalzerosrandomrandom5eveneven_odd
a0.00.00.8571574.285783Trueeven
b1.00.00.6113133.056565Falseodd
c2.00.00.2901021.450512Trueeven
d3.00.00.5234852.617426Falseodd
e4.00.00.9555334.777663Trueeven
\n", - "
" - ], "text/plain": [ - " numerical zeros random random5 even even_odd\n", - "a 0.0 0.0 0.857157 4.285783 True even\n", - "b 1.0 0.0 0.611313 3.056565 False odd\n", - "c 2.0 0.0 0.290102 1.450512 True even\n", - "d 3.0 0.0 0.523485 2.617426 False odd\n", - "e 4.0 0.0 0.955533 4.777663 True even" + "numerical e\n", + "zeros a\n", + "random e\n", + "random5 e\n", + "dtype: object" ] }, - "execution_count": 10, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "columns = [\"numerical\", \"zeros\", \"random\", \"random5\", \"even\"]\n", - "\n", - "x = 5\n", - "\n", - "transposed_numeric_df" + "transposed_numeric_df[columns].idxmax()\n", + "\n" ] } ], From ec10195ae4360853d903ac22fda6fd02b794d9bf Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Thu, 29 Jan 2026 00:32:43 -0800 Subject: [PATCH 24/32] initial commit --- week_4_panadas.ipynb | 223 ++++++++++++++++++++++--------------------- 1 file changed, 112 insertions(+), 111 deletions(-) diff --git a/week_4_panadas.ipynb b/week_4_panadas.ipynb index 6ba43e0..045cd33 100644 --- a/week_4_panadas.ipynb +++ b/week_4_panadas.ipynb @@ -19,7 +19,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -52,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -65,14 +65,14 @@ "dtype: int64" ] }, - "execution_count": 4, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = pd.Series([6,7,8,9])\n", - "x" + "x\n" ] }, { @@ -100,7 +100,7 @@ } ], "source": [ - "x = pd.Series([6,7,8,9], index = [\"a\",\"d\",\"b\",\"c\"])\n", + "x = pd.Series([6,7,8,9], index = list(\"adbc\"))\n", "x[[\"d\"]]" ] }, @@ -698,7 +698,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -748,11 +748,11 @@ " \n", " \n", " series_random\n", - " 0.857157\n", - " 0.611313\n", - " 0.290102\n", - " 0.523485\n", - " 0.955533\n", + " 0.387098\n", + " 0.242602\n", + " 0.304984\n", + " 0.607978\n", + " 0.380149\n", " \n", " \n", "\n", @@ -762,10 +762,10 @@ " a b c d e\n", "series_numerical 0.000000 1.000000 2.000000 3.000000 4.000000\n", "series_zeros 0.000000 0.000000 0.000000 0.000000 0.000000\n", - "series_random 0.857157 0.611313 0.290102 0.523485 0.955533" + "series_random 0.387098 0.242602 0.304984 0.607978 0.380149" ] }, - "execution_count": 2, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -796,7 +796,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -830,31 +830,31 @@ " a\n", " 0.0\n", " 0.0\n", - " 0.857157\n", + " 0.387098\n", " \n", " \n", " b\n", " 1.0\n", " 0.0\n", - " 0.611313\n", + " 0.242602\n", " \n", " \n", " c\n", " 2.0\n", " 0.0\n", - " 0.290102\n", + " 0.304984\n", " \n", " \n", " d\n", " 3.0\n", " 0.0\n", - " 0.523485\n", + " 0.607978\n", " \n", " \n", " e\n", " 4.0\n", " 0.0\n", - " 0.955533\n", + " 0.380149\n", " \n", " \n", "\n", @@ -862,14 +862,14 @@ ], "text/plain": [ " numerical zeros random\n", - "a 0.0 0.0 0.857157\n", - "b 1.0 0.0 0.611313\n", - "c 2.0 0.0 0.290102\n", - "d 3.0 0.0 0.523485\n", - "e 4.0 0.0 0.955533" + "a 0.0 0.0 0.387098\n", + "b 1.0 0.0 0.242602\n", + "c 2.0 0.0 0.304984\n", + "d 3.0 0.0 0.607978\n", + "e 4.0 0.0 0.380149" ] }, - "execution_count": 3, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -889,7 +889,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -920,34 +920,34 @@ " \n", " \n", " \n", - " e\n", - " 4.0\n", + " d\n", + " 3.0\n", " 0.0\n", - " 0.955533\n", + " 0.607978\n", " \n", " \n", " a\n", " 0.0\n", " 0.0\n", - " 0.857157\n", + " 0.387098\n", " \n", " \n", - " b\n", - " 1.0\n", + " e\n", + " 4.0\n", " 0.0\n", - " 0.611313\n", + " 0.380149\n", " \n", " \n", - " d\n", - " 3.0\n", + " c\n", + " 2.0\n", " 0.0\n", - " 0.523485\n", + " 0.304984\n", " \n", " \n", - " c\n", - " 2.0\n", + " b\n", + " 1.0\n", " 0.0\n", - " 0.290102\n", + " 0.242602\n", " \n", " \n", "\n", @@ -955,14 +955,14 @@ ], "text/plain": [ " numerical zeros random\n", - "e 4.0 0.0 0.955533\n", - "a 0.0 0.0 0.857157\n", - "b 1.0 0.0 0.611313\n", - "d 3.0 0.0 0.523485\n", - "c 2.0 0.0 0.290102" + "d 3.0 0.0 0.607978\n", + "a 0.0 0.0 0.387098\n", + "e 4.0 0.0 0.380149\n", + "c 2.0 0.0 0.304984\n", + "b 1.0 0.0 0.242602" ] }, - "execution_count": 4, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -981,7 +981,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -1015,13 +1015,13 @@ " d\n", " 3.0\n", " 0.0\n", - " 0.523485\n", + " 0.607978\n", " \n", " \n", " e\n", " 4.0\n", " 0.0\n", - " 0.955533\n", + " 0.380149\n", " \n", " \n", "\n", @@ -1029,11 +1029,11 @@ ], "text/plain": [ " numerical zeros random\n", - "d 3.0 0.0 0.523485\n", - "e 4.0 0.0 0.955533" + "d 3.0 0.0 0.607978\n", + "e 4.0 0.0 0.380149" ] }, - "execution_count": 5, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -1052,7 +1052,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -1060,11 +1060,11 @@ "output_type": "stream", "text": [ " numerical zeros random random5\n", - "a 0.0 0.0 0.857157 4.285783\n", - "b 1.0 0.0 0.611313 3.056565\n", - "c 2.0 0.0 0.290102 1.450512\n", - "d 3.0 0.0 0.523485 2.617426\n", - "e 4.0 0.0 0.955533 4.777663\n", + "a 0.0 0.0 0.387098 1.935491\n", + "b 1.0 0.0 0.242602 1.213010\n", + "c 2.0 0.0 0.304984 1.524922\n", + "d 3.0 0.0 0.607978 3.039889\n", + "e 4.0 0.0 0.380149 1.900745\n", "-----------------------------------------------------------------\n", "All rows where numerical is greater than random5:\n" ] @@ -1101,15 +1101,15 @@ " c\n", " 2.0\n", " 0.0\n", - " 0.290102\n", - " 1.450512\n", + " 0.304984\n", + " 1.524922\n", " \n", " \n", - " d\n", - " 3.0\n", + " e\n", + " 4.0\n", " 0.0\n", - " 0.523485\n", - " 2.617426\n", + " 0.380149\n", + " 1.900745\n", " \n", " \n", "\n", @@ -1117,11 +1117,11 @@ ], "text/plain": [ " numerical zeros random random5\n", - "c 2.0 0.0 0.290102 1.450512\n", - "d 3.0 0.0 0.523485 2.617426" + "c 2.0 0.0 0.304984 1.524922\n", + "e 4.0 0.0 0.380149 1.900745" ] }, - "execution_count": 6, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -1144,7 +1144,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -1180,40 +1180,40 @@ " a\n", " 0.0\n", " 0.0\n", - " 0.857157\n", - " 4.285783\n", + " 0.387098\n", + " 1.935491\n", " True\n", " \n", " \n", " b\n", " 1.0\n", " 0.0\n", - " 0.611313\n", - " 3.056565\n", + " 0.242602\n", + " 1.213010\n", " False\n", " \n", " \n", " c\n", " 2.0\n", " 0.0\n", - " 0.290102\n", - " 1.450512\n", + " 0.304984\n", + " 1.524922\n", " True\n", " \n", " \n", " d\n", " 3.0\n", " 0.0\n", - " 0.523485\n", - " 2.617426\n", + " 0.607978\n", + " 3.039889\n", " False\n", " \n", " \n", " e\n", " 4.0\n", " 0.0\n", - " 0.955533\n", - " 4.777663\n", + " 0.380149\n", + " 1.900745\n", " True\n", " \n", " \n", @@ -1222,14 +1222,14 @@ ], "text/plain": [ " numerical zeros random random5 even\n", - "a 0.0 0.0 0.857157 4.285783 True\n", - "b 1.0 0.0 0.611313 3.056565 False\n", - "c 2.0 0.0 0.290102 1.450512 True\n", - "d 3.0 0.0 0.523485 2.617426 False\n", - "e 4.0 0.0 0.955533 4.777663 True" + "a 0.0 0.0 0.387098 1.935491 True\n", + "b 1.0 0.0 0.242602 1.213010 False\n", + "c 2.0 0.0 0.304984 1.524922 True\n", + "d 3.0 0.0 0.607978 3.039889 False\n", + "e 4.0 0.0 0.380149 1.900745 True" ] }, - "execution_count": 7, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -1248,7 +1248,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -1285,8 +1285,8 @@ " a\n", " 0.0\n", " 0.0\n", - " 0.857157\n", - " 4.285783\n", + " 0.387098\n", + " 1.935491\n", " True\n", " even\n", " \n", @@ -1294,8 +1294,8 @@ " b\n", " 1.0\n", " 0.0\n", - " 0.611313\n", - " 3.056565\n", + " 0.242602\n", + " 1.213010\n", " False\n", " odd\n", " \n", @@ -1303,8 +1303,8 @@ " c\n", " 2.0\n", " 0.0\n", - " 0.290102\n", - " 1.450512\n", + " 0.304984\n", + " 1.524922\n", " True\n", " even\n", " \n", @@ -1312,8 +1312,8 @@ " d\n", " 3.0\n", " 0.0\n", - " 0.523485\n", - " 2.617426\n", + " 0.607978\n", + " 3.039889\n", " False\n", " odd\n", " \n", @@ -1321,8 +1321,8 @@ " e\n", " 4.0\n", " 0.0\n", - " 0.955533\n", - " 4.777663\n", + " 0.380149\n", + " 1.900745\n", " True\n", " even\n", " \n", @@ -1332,14 +1332,14 @@ ], "text/plain": [ " numerical zeros random random5 even even_odd\n", - "a 0.0 0.0 0.857157 4.285783 True even\n", - "b 1.0 0.0 0.611313 3.056565 False odd\n", - "c 2.0 0.0 0.290102 1.450512 True even\n", - "d 3.0 0.0 0.523485 2.617426 False odd\n", - "e 4.0 0.0 0.955533 4.777663 True even" + "a 0.0 0.0 0.387098 1.935491 True even\n", + "b 1.0 0.0 0.242602 1.213010 False odd\n", + "c 2.0 0.0 0.304984 1.524922 True even\n", + "d 3.0 0.0 0.607978 3.039889 False odd\n", + "e 4.0 0.0 0.380149 1.900745 True even" ] }, - "execution_count": 8, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -1373,21 +1373,21 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "numerical 10.00000\n", - "zeros 0.00000\n", - "random 3.23759\n", - "random5 16.18795\n", - "even 3.00000\n", + "numerical 10.000000\n", + "zeros 0.000000\n", + "random 1.922811\n", + "random5 9.614057\n", + "even 3.000000\n", "dtype: float64" ] }, - "execution_count": 9, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -1414,7 +1414,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -1422,12 +1422,13 @@ "text/plain": [ "numerical e\n", "zeros a\n", - "random e\n", - "random5 e\n", + "random d\n", + "random5 d\n", + "even a\n", "dtype: object" ] }, - "execution_count": 25, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } From e16b4eefe9caddb43f72c84922e5a3ffaf85e285 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Thu, 29 Jan 2026 01:01:29 -0800 Subject: [PATCH 25/32] initial commit --- week_5_vectors_applications.ipynb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/week_5_vectors_applications.ipynb b/week_5_vectors_applications.ipynb index 11948c0..366ddd5 100644 --- a/week_5_vectors_applications.ipynb +++ b/week_5_vectors_applications.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 2, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -28,15 +28,27 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'woman' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[8], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m women \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m2\u001b[39m])\n\u001b[1;32m 3\u001b[0m man \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m5\u001b[39m])\n\u001b[0;32m----> 4\u001b[0m royalty \u001b[38;5;241m=\u001b[39m queen \u001b[38;5;241m-\u001b[39m \u001b[43mwoman\u001b[49m\n\u001b[1;32m 5\u001b[0m king \u001b[38;5;241m=\u001b[39m royalty \u001b[38;5;241m-\u001b[39m man\n", + "\u001b[0;31mNameError\u001b[0m: name 'woman' is not defined" + ] + } + ], "source": [ "queen = np.array([5, 6, 4])\n", "women = np.array([1, 3, 2])\n", "man = np.array([5, 5, 5])\n", - "royalty = None\n", - "king = None\n", + "royalty = queen - women\n", + "king = royalty - man\n", "\n" ] }, From 7338827c72e5a248dcf962de8d0fd6a94760719e Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Thu, 29 Jan 2026 17:25:05 -0800 Subject: [PATCH 26/32] started working on problem 2 --- week_5_vectors_applications.ipynb | 39 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/week_5_vectors_applications.ipynb b/week_5_vectors_applications.ipynb index 366ddd5..26f172e 100644 --- a/week_5_vectors_applications.ipynb +++ b/week_5_vectors_applications.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 7, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -28,21 +28,9 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'woman' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[8], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m women \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m2\u001b[39m])\n\u001b[1;32m 3\u001b[0m man \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m5\u001b[39m])\n\u001b[0;32m----> 4\u001b[0m royalty \u001b[38;5;241m=\u001b[39m queen \u001b[38;5;241m-\u001b[39m \u001b[43mwoman\u001b[49m\n\u001b[1;32m 5\u001b[0m king \u001b[38;5;241m=\u001b[39m royalty \u001b[38;5;241m-\u001b[39m man\n", - "\u001b[0;31mNameError\u001b[0m: name 'woman' is not defined" - ] - } - ], + "outputs": [], "source": [ "queen = np.array([5, 6, 4])\n", "women = np.array([1, 3, 2])\n", @@ -64,13 +52,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test passed for [5]\n", + "Test failed for [3.2 0.1]\n", + "Test failed for [-2 2 2]\n", + "Test failed for [86 72 51 87 73 21 60 70 66 57]\n" + ] + } + ], "source": [ "def vector_magnitude(np_vector: npt.NDArray) -> np.float64:\n", " #Write your code here\n", - " return \n", + " return 5\n", "\n", "def test_vector_magnitude(np_vector: npt.NDArray) -> None:\n", " if vector_magnitude(np_vector) == np.linalg.norm(np_vector):\n", @@ -277,7 +276,7 @@ ], "metadata": { "kernelspec": { - "display_name": "AD_450_env", + "display_name": "ml-env", "language": "python", "name": "python3" }, @@ -291,7 +290,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.10.18" } }, "nbformat": 4, From 0c2c9ea90a33c9b41548b41d2d5407a2eeba346d Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Mon, 2 Feb 2026 22:36:58 -0800 Subject: [PATCH 27/32] completed problem 3 --- week_5_vectors_applications.ipynb | 61 ++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/week_5_vectors_applications.ipynb b/week_5_vectors_applications.ipynb index 26f172e..3464b2a 100644 --- a/week_5_vectors_applications.ipynb +++ b/week_5_vectors_applications.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -36,7 +36,7 @@ "women = np.array([1, 3, 2])\n", "man = np.array([5, 5, 5])\n", "royalty = queen - women\n", - "king = royalty - man\n", + "king = royalty + man\n", "\n" ] }, @@ -52,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -60,16 +60,29 @@ "output_type": "stream", "text": [ "Test passed for [5]\n", - "Test failed for [3.2 0.1]\n", - "Test failed for [-2 2 2]\n", - "Test failed for [86 72 51 87 73 21 60 70 66 57]\n" + "Test passed for [3.2 0.1]\n", + "Test passed for [-2 2 2]\n", + "Test passed for [70 2 28 36 36 7 34 99 33 82]\n" ] } ], "source": [ + "\"\"\"\n", + "I wrote vector_magnitude two different ways, the commented version use \n", + "a for loop to sum the square of each element and then return the square \n", + "root of that sum. In the second version I compute everything in one line. \n", + "\"\"\"\n", + "\n", + "\n", + "# def vector_magnitude(np_vector: npt.NDArray) -> np.float64:\n", + "# sum = 0 \n", + "# for num in np_vector:\n", + "# sum += num**2 \n", + "\n", + "# return sum**0.5\n", + "\n", "def vector_magnitude(np_vector: npt.NDArray) -> np.float64:\n", - " #Write your code here\n", - " return 5\n", + " return sum((np_vector**2))**0.5\n", "\n", "def test_vector_magnitude(np_vector: npt.NDArray) -> None:\n", " if vector_magnitude(np_vector) == np.linalg.norm(np_vector):\n", @@ -86,7 +99,7 @@ "test_vector_magnitude(d1_vector)\n", "test_vector_magnitude(d2_vector)\n", "test_vector_magnitude(d3_vector)\n", - "test_vector_magnitude(d10_vector)" + "test_vector_magnitude(d10_vector)\n" ] }, { @@ -101,15 +114,37 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test passed for [1.]\n", + "Test passed for [0.99951208 0.03123475]\n", + "Test passed for [-0.57735027 0.57735027 0.57735027]\n" + ] + } + ], "source": [ "d1_unit_vector = np.array([1.])\n", "d2_unit_vector = np.array([0.99951208, 0.03123475])\n", "d3_unit_vector = np.array([-0.57735027, 0.57735027, 0.57735027])\n", "\n", - "#Impliment vector_magnitude and test_vector_magnitude here" + "#Impliment vector_magnitude and test_vector_magnitude here\n", + "def create_unit_vector(arr):\n", + " return arr * (1 / vector_magnitude(arr))\n", + "\n", + "def test_create_unit_vector(arr, test_arr):\n", + " if np.allclose(create_unit_vector(arr), test_arr):\n", + " print(f\"Test passed for {test_arr}\")\n", + " else:\n", + " print(f\"Test failed for {test_arr}\")\n", + "\n", + "test_create_unit_vector(create_unit_vector(d1_vector), d1_unit_vector)\n", + "test_create_unit_vector(create_unit_vector(d2_vector), d2_unit_vector)\n", + "test_create_unit_vector(create_unit_vector(d3_vector), d3_unit_vector)" ] }, { From cb044f84f1538d23b8e06207f2d79df61acbad45 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Mon, 2 Feb 2026 23:06:03 -0800 Subject: [PATCH 28/32] completed problem 4 --- week_5_vectors_applications.ipynb | 35 ++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/week_5_vectors_applications.ipynb b/week_5_vectors_applications.ipynb index 3464b2a..e18d293 100644 --- a/week_5_vectors_applications.ipynb +++ b/week_5_vectors_applications.ipynb @@ -161,10 +161,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Test passed for [5 6 7] and [5 6 7]\n", + "Test passed for [2 3 4] and [7. 8. 9.]\n", + "Test passed for [0.0001 0.0002 0.0003] and [100000 200000 300000]\n" + ] + } + ], + "source": [ + "vector1 = np.array([5,6,7])\n", + "vector2_1 = np.array([2,3,4])\n", + "vector2_2 = np.array([7.0, 8.0, 9.0])\n", + "vector3_1 = np.array([0.0001, 0.0002, 0.0003])\n", + "vector3_2 = np.array([100_000, 200_000, 300_000])\n", + "\n", + "def my_dot(arr1, arr2):\n", + " return sum(arr1 * arr2)\n", + "\n", + "def test_my_dot(arr1, arr2):\n", + " if my_dot(arr1, arr2) == np.dot(arr1, arr2):\n", + " print(f\"Test passed for {arr1} and {arr2}\")\n", + " else:\n", + " print(f\"Test failed\")\n", + "\n", + "test_my_dot(vector1, vector1)\n", + "test_my_dot(vector2_1, vector2_2)\n", + "test_my_dot(vector3_1, vector3_2)" + ] }, { "cell_type": "markdown", From f9e154ef649682fe3818a625e90f7c16229c6561 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Mon, 2 Feb 2026 23:50:54 -0800 Subject: [PATCH 29/32] completed problem 5 --- week_5_vectors_applications.ipynb | 32 ++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/week_5_vectors_applications.ipynb b/week_5_vectors_applications.ipynb index e18d293..d184737 100644 --- a/week_5_vectors_applications.ipynb +++ b/week_5_vectors_applications.ipynb @@ -213,15 +213,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cat 1 prediction: 21.15\n", + "Cat 1 prediction error == 3.06\n", + "\n", + "\n", + "Cat 2 prediction: 15.21\n", + "Cat 2 prediction error == 1.38\n", + "\n" + ] + } + ], "source": [ "cat_1_array = np.array([28.57, 10.435])\n", "cat_1_actual_weight = 24.21\n", "\n", "cat_2_array = np.array([19.04, 6.93])\n", - "cat_2_actual_weight = 13.831" + "cat_2_actual_weight = 13.831\n", + "\n", + "def predict_weight(arr):\n", + " return 0.23 * arr[0] + 1.07 * arr[1] + 3.412\n", + "\n", + "def prediction_error(arr, actual_weight):\n", + " return abs(predict_weight(arr) - actual_weight)\n", + "\n", + "print(f\"Cat 1 prediction: {predict_weight(cat_1_array):.2f}\")\n", + "print(f\"Cat 1 prediction error == {prediction_error(cat_1_array, cat_1_actual_weight):.2f}\\n\")\n", + "print(\"\")\n", + "print(f\"Cat 2 prediction: {predict_weight(cat_2_array):.2f}\")\n", + "print(f\"Cat 2 prediction error == {prediction_error(cat_2_array, cat_2_actual_weight):.2f}\\n\")\n" ] }, { From 1e3890ca5fe906c98eee42b08c91a869d6d96579 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Tue, 3 Feb 2026 00:34:27 -0800 Subject: [PATCH 30/32] completed problem 6 --- week_5_vectors_applications.ipynb | 45 +++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/week_5_vectors_applications.ipynb b/week_5_vectors_applications.ipynb index d184737..dd0e056 100644 --- a/week_5_vectors_applications.ipynb +++ b/week_5_vectors_applications.ipynb @@ -285,8 +285,49 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Loop version is faster!\n" + ] + } + ], + "source": [ + "import time\n", + "\n", + "def linear_comb_loop(scalars_arr, vectors_arr):\n", + " curr = np.array(np.zeros(3))\n", + "\n", + " for i in range(len(vectors_arr)):\n", + " curr += scalars_arr[i] * vectors_arr[i]\n", + "\n", + " return curr\n", + "\n", + "scalars_array = np.array([1, 2, -3])\n", + "vectors_array = np.array([[4,5,1], [-4,0,-4], [1,3,2]])\n", + "\n", + "def linear_comb_vectorized(scalars_arr, vectors_arr):\n", + " return np.dot(scalars_arr, vectors_arr)\n", + "\n", + "def linear_comb_speed_test(scalars_arr, vectors_arr):\n", + " start1 = time.time()\n", + " linear_comb_loop(scalars_arr, vectors_arr)\n", + " end1 = time.time() - start1\n", + "\n", + " start2 = time.time()\n", + " linear_comb_vectorized(scalars_arr, vectors_arr)\n", + " end2 = time.time() - start2\n", + "\n", + " if(end1 > end2):\n", + " print(\"Loop version is faster!\")\n", + " else:\n", + " print(\"Vectorized version is faster!\")\n", + "\n", + "linear_comb_speed_test(scalars_array, vectors_array)\n", + "\n" + ] }, { "cell_type": "markdown", From af0921dbca5a9b36f408dc2502f6291173a34f89 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Tue, 3 Feb 2026 01:15:39 -0800 Subject: [PATCH 31/32] completed problem 7 --- week_5_vectors_applications.ipynb | 38 ++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/week_5_vectors_applications.ipynb b/week_5_vectors_applications.ipynb index dd0e056..2bd8e64 100644 --- a/week_5_vectors_applications.ipynb +++ b/week_5_vectors_applications.ipynb @@ -283,7 +283,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, "outputs": [ { @@ -340,22 +340,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "basis_vector = np.array([1,3])\n", + "rand_dist = np.linspace(-4, 4, 100)\n", + "\n", + "\"\"\"\n", + "this is how I originally completed the problem\n", + "\n", + "sub_space = np.zeros((100, 2))\n", + "for i in range(len(rand_dist)):\n", + " sub_space[i] = rand_dist[i] * basis_vector\n", + "\"\"\"\n", + "\n", + "sub_space = rand_dist[:, None] * basis_vector" + ] }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAioAAAGdCAYAAAA8F1jjAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjUsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvWftoOwAAAAlwSFlzAAAPYQAAD2EBqD+naQAAJPVJREFUeJzt3X1wVPXZ//HPBsMuYLIYErJkCBCIqCDqoBJi6NRUEB2g6ljbWtTEOlYZsAJWgY4RqQ9Bp2MZLQPO3ZbY3vrDThlgHCvV4tPcSMAnpojgmEwsaAhEGLKB0aC75/cH3dRAArvn7NnzsO/XzP6R3Sy5dkDzmXN9r+sEDMMwBAAA4EI5ThcAAADQF4IKAABwLYIKAABwLYIKAABwLYIKAABwLYIKAABwLYIKAABwLYIKAABwrbOcLsCqeDyu1tZW5eXlKRAIOF0OAABIgmEY6uzsVElJiXJy+r5u4vmg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+vi//ASbw+bzP75+Rz+d9fv+MfD5zTnclJYHDtAAAwLUIKgAAwLUIKqcRDAa1dOlSBYNBp0uxBZ/P+/z+Gfl83uf3z8jns5/nD9MCAAD/4ooKAABwLYIKAABwLYIKAABwLYIKAABwLYJKirq6unTJJZcoEAhox44dTpeTNj/84Q81YsQIhUIhDRs2TLfeeqtaW1udLittPvvsM91xxx0qKyvTgAEDNGbMGC1dulTHjx93urS0eeyxx3TFFVdo4MCBGjx4sNPlWLZy5UqNGjVKoVBIFRUV2r59u9Mlpc3bb7+tWbNmqaSkRIFAQBs2bHC6pLSqr6/X5Zdfrry8PA0dOlTXX3+9PvnkE6fLSqtVq1bpoosu6l6EVllZqVdeecXpsmyzfPlyBQIBzZ8/P+M/m6CSogceeEAlJSVOl5F21dXV+utf/6pPPvlE69atU3Nzs370ox85XVba7NmzR/F4XM8++6x27dql3/3ud1q9erV+/etfO11a2hw/flw33XST5syZ43Qplr344otauHChli5dqg8++EAXX3yxpk+froMHDzpdWlocO3ZMF198sVauXOl0KbZ46623NHfuXDU2Nuq1117TN998o6uvvlrHjh1zurS0GT58uJYvX673339f7733nn7wgx/ouuuu065du5wuLe3effddPfvss7roooucKcBA0v7+978b559/vrFr1y5DkvHhhx86XZJtNm7caAQCAeP48eNOl2KbJ5980igrK3O6jLRbs2aNEQ6HnS7DkkmTJhlz587t/joWixklJSVGfX29g1XZQ5Kxfv16p8uw1cGDBw1JxltvveV0KbY655xzjD/84Q9Ol5FWnZ2dxrnnnmu89tprxve//33j3nvvzXgNXFFJ0oEDB3TnnXfqL3/5iwYOHOh0ObY6fPiwnn/+eV1xxRXKzc11uhzbdHR0qKCgwOkycJLjx4/r/fff19SpU7ufy8nJ0dSpU7V161YHK4NZHR0dkuTb/95isZjWrl2rY8eOqbKy0uly0mru3LmaMWNGj/8eM42gkgTDMFRbW6u7775bl112mdPl2GbRokUaNGiQhgwZor1792rjxo1Ol2SbpqYmPfPMM7rrrrucLgUn+fLLLxWLxVRcXNzj+e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Vfjyf/617+M6upqo6CgwAgGg8aoUaOMu+++2/j888+dLi1t1qxZY0jq9eEXNTU1vX6+N954w+nSTHnmmWeMESNGGP379zcmTZpkNDY2Ol1S2rzxxhu9/l3V1NQ4XVpa9PXf2po1a5wuLW1+/vOfGyNHjjT69+9vFBUVGVdddZXx6quvOl2WrZwaT87qMyoAAMDd3N+kBwAAWYugAgAAXIugAgAAXIugAgAAXIugAgAAXIugAgAAXIugAgAAXIugAgAAXIugAgAAXIugAgAAXIugAgAAXIugAgAAXOv/Ayvh2PLF+3P2AAAAAElFTkSuQmCC", + "text/plain": [ + "
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "import matplotlib.pyplot as plt\n", "\n", "def plot_vectors(vectors):\n", " plt.scatter(*zip(*vectors))\n", - " plt.show()" + " plt.show()\n", + "\n", + "plot_vectors(sub_space)" ] }, { From e4047f79533cc12891663fa10d44946c64dae331 Mon Sep 17 00:00:00 2001 From: Joe Klinck Date: Tue, 3 Feb 2026 03:37:23 -0800 Subject: [PATCH 32/32] mostly completed problem 9 --- week_5_vectors_applications.ipynb | 61 +++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/week_5_vectors_applications.ipynb b/week_5_vectors_applications.ipynb index 2bd8e64..3e82f01 100644 --- a/week_5_vectors_applications.ipynb +++ b/week_5_vectors_applications.ipynb @@ -425,10 +425,65 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pearson function passed.\n", + "Cosine test passed.\n", + "My functions are NOT equal to each other.\n" + ] + } + ], + "source": [ + "from scipy.spatial.distance import cosine\n", + "\n", + "coeff_arr1 = np.array([2,3,4])\n", + "coeff_arr2 = np.array([5,6,7])\n", + "\n", + "def pearson_corr(x_arr, y_arr):\n", + " x_mean = x_arr - np.mean(x_arr)\n", + " y_mean = y_arr - np.mean(y_arr)\n", + " numerator = np.sum((x_mean) * (y_mean)) \n", + " denominator = np.sqrt((np.sum((x_mean)**2)) * (np.sum((y_mean)**2)))\n", + " return numerator / denominator\n", + "\n", + "def cosine_simil(x_arr, y_arr):\n", + " numerator = np.dot(x_arr, y_arr)\n", + " denominator = np.linalg.norm(x_arr) * np.linalg.norm(y_arr)\n", + " return numerator / denominator\n", + "\n", + "def test_pearson(arr1, arr2):\n", + " if (pearson_corr(arr1, arr2)) == (np.corrcoef(arr1, arr2)[0,1]):\n", + " print(\"Pearson function passed.\")\n", + " else:\n", + " print(\"Pearson function failed.\")\n", + "\n", + "def test_cosine(arr1, arr2):\n", + " if (cosine_simil(arr1, arr2)) == (1 - cosine(arr1, arr2)):\n", + " print(\"Cosine test passed.\")\n", + " else:\n", + " print(\"Cosine test failed.\")\n", + "\n", + "\n", + "def test_person_cosine_equality(arr1, arr2):\n", + " if (pearson_corr(arr1, arr2)) == (cosine_simil(arr1, arr2)):\n", + " print(\"Both of my functions are equal to each other.\")\n", + " else:\n", + " print(\"My functions are NOT equal to each other.\")\n", + "\n", + "\n", + "\n", + "test_pearson(coeff_arr1, coeff_arr2)\n", + "test_cosine(coeff_arr1, coeff_arr2)\n", + "test_person_cosine_equality(coeff_arr1, coeff_arr2)\n", + "\n", + "\n", + "\n" + ] } ], "metadata": {