From c1635695ef9156d3bcc8ca7bd5ade0024517dcb4 Mon Sep 17 00:00:00 2001 From: Julien Smith Date: Mon, 12 Jan 2026 22:10:11 -0800 Subject: [PATCH 01/10] wk2 ipython cur progress --- AD450_math.py | 5 + week_2_intro_ipython.ipynb | 195 ++++++++++++++++++++++++++++++++----- 2 files changed, 175 insertions(+), 25 deletions(-) create mode 100644 AD450_math.py diff --git a/AD450_math.py b/AD450_math.py new file mode 100644 index 0000000..357720a --- /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..b62b64d 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -28,8 +28,20 @@ "metadata": { "id": "BQybla17FRh5" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My name is Julien.\n", + "I am a student at North Seattle College\n" + ] + } + ], + "source": [ + "name = \"Julien\"\n", + "print(\"My name is \" + name + \".\\nI am a student at North Seattle College\")" + ] }, { "cell_type": "markdown", @@ -47,8 +59,19 @@ "metadata": { "id": "P7QDyWncFRh8" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My phone number is 2069991111\n" + ] + } + ], + "source": [ + "phone = 2069991111\n", + "print(\"My phone number is \" + str(phone))" + ] }, { "cell_type": "markdown", @@ -66,8 +89,21 @@ "metadata": { "id": "OnOau948FRh9" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello my name is Julien and my phone number is 206-999-1111\n" + ] + } + ], + "source": [ + "name = \"Julien\"\n", + "phone_number = \"206-999-1111\"\n", + "\n", + "print(f\"Hello my name is {name} and my phone number is {phone_number}\")" + ] }, { "cell_type": "markdown", @@ -86,8 +122,20 @@ "metadata": { "id": "5x2rIFzRFRh9" }, - "outputs": [], - "source": [] + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax. Maybe you meant '==' or ':=' instead of '='? (1440112413.py, line 1)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[19]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mmonth = \"June\", year = 2027\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax. Maybe you meant '==' or ':=' instead of '='?\n" + ] + } + ], + "source": [ + "month, year = \"June\", 2027\n", + "print(f\"I graduate in {month} of {year}\")" + ] }, { "cell_type": "markdown", @@ -113,8 +161,20 @@ "metadata": { "id": "OkyeAVPXFRh-" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "58.5\n" + ] + } + ], + "source": [ + "ans = 12 * 5 - 3 / 2\n", + "print(ans)\n", + "# it is float because there are trailing decimals\n" + ] }, { "cell_type": "markdown", @@ -140,8 +200,24 @@ "metadata": { "id": "Lo-LEy8bKYmG" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "31 days\n" + ] + } + ], + "source": [ + "month = \"October\"\n", + "\n", + "if month == \"October\":\n", + " print(\"31 days\")\n", + "else:\n", + " print(\"30 days\")\n", + " \n" + ] }, { "cell_type": "markdown", @@ -161,8 +237,25 @@ "metadata": { "id": "qscUkh813QKH" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "between 100 and 150\n" + ] + } + ], + "source": [ + "num = 140\n", + "\n", + "if num > 150:\n", + " print(\"greater than 150\")\n", + "elif num > 100:\n", + " print(\"between 100 and 150\")\n", + "else:\n", + " print(\"less than 100\")" + ] }, { "cell_type": "markdown", @@ -181,8 +274,24 @@ "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 2\n" + ] + } + ], + "source": [ + "num = 70 \n", + "if num % 5 == 0 and num % 10 == 0:\n", + " print(\"Number is divisible by 5 and 10\")\n", + "\n", + "if num % 7 == 0 or num % 2 == 0:\n", + " print(\"Number is divisible by 7 or 2\")" + ] }, { "cell_type": "markdown", @@ -199,8 +308,34 @@ "metadata": { "id": "JY_Ci5v-FRiA" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "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(0, 100):\n", + " if i % 7 == 0:\n", + " print(i)" + ] }, { "cell_type": "markdown", @@ -214,9 +349,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "id": "4a1D64hpFRiA" - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -337,9 +470,21 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'AD450_math'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[33]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mAD450_math\u001b[39;00m\n\u001b[32m 3\u001b[39m AD450_math.add(\u001b[32m2\u001b[39m,\u001b[32m2\u001b[39m)\n", + "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'AD450_math'" + ] + } + ], "source": [ "import AD450_math\n", "\n", @@ -431,7 +576,7 @@ "provenance": [] }, "kernelspec": { - "display_name": "my_env_2", + "display_name": "base", "language": "python", "name": "python3" }, @@ -445,7 +590,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.13.5" } }, "nbformat": 4, From d434de3185a625991fd51c84ad607cb204fede85 Mon Sep 17 00:00:00 2001 From: Julien Smith Date: Mon, 12 Jan 2026 22:12:25 -0800 Subject: [PATCH 02/10] wk2 cur progress --- week_2_intro_ipython.ipynb | 196 +++++++++++++++++++++++++++---------- 1 file changed, 147 insertions(+), 49 deletions(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index b62b64d..78bed5b 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": 22, "metadata": { "id": "BQybla17FRh5" }, @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": { "id": "P7QDyWncFRh8" }, @@ -85,7 +85,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": { "id": "OnOau948FRh9" }, @@ -118,17 +118,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { "id": "5x2rIFzRFRh9" }, "outputs": [ { - "ename": "SyntaxError", - "evalue": "invalid syntax. Maybe you meant '==' or ':=' instead of '='? (1440112413.py, line 1)", - "output_type": "error", - "traceback": [ - " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[19]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mmonth = \"June\", year = 2027\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax. Maybe you meant '==' or ':=' instead of '='?\n" + "name": "stdout", + "output_type": "stream", + "text": [ + "I graduate in June of 2027\n" ] } ], @@ -157,7 +156,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": { "id": "OkyeAVPXFRh-" }, @@ -196,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": { "id": "Lo-LEy8bKYmG" }, @@ -233,7 +232,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": { "id": "qscUkh813QKH" }, @@ -270,7 +269,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": { "id": "1vxg4nMbFRh_" }, @@ -304,7 +303,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": { "id": "JY_Ci5v-FRiA" }, @@ -348,10 +347,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 173, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 3 6 9 \n", + "18\n" + ] + } + ], + "source": [ + "i = 0\n", + "nums = \"\"\n", + "while i < 10: \n", + " if i % 3 == 0:\n", + " nums += str(i) + \" \"\n", + " sums = i + i \n", + " i += 1\n", + "print(nums)\n", + "print(sums)\n" + ] }, { "cell_type": "markdown", @@ -392,12 +410,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 174, "metadata": { "id": "lppms8ctzfRI" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hogwarts\n", + "s\n" + ] + } + ], + "source": [ + "school = \"Hogwarts\"\n", + "first = school[0:3]\n", + "second = school[3:8]\n", + "print(first + second)\n", + "print(school[-1])" + ] }, { "cell_type": "markdown", @@ -413,12 +446,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 175, "metadata": { "id": "zw2NezoM0zfl" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thor is a character in the Marvel Cinematic Universe\n", + "She-Hulk is a character in the Marvel Cinematic Universe\n", + "Loki is a character in the Marvel Cinematic Universe\n" + ] + } + ], + "source": [ + "chars = [\"Thor\", \"She-Hulk\", \"Loki\"]\n", + "for char in chars:\n", + " print(char +\" is a character in the Marvel Cinematic Universe\")" + ] }, { "cell_type": "markdown", @@ -454,12 +501,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 176, "metadata": { "id": "p0YE0nqs4Hcz" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Breed': 'Corgi', 'Age': 13, 'Type': 'Cattle herding'}\n", + "Corgi\n", + "The Corgi is expected to live about 13 years\n" + ] + } + ], + "source": [ + "dog = {\n", + " \"Breed\": \"Corgi\",\n", + " \"Age\": 13,\n", + " \"Type\": \"Cattle herding\"\n", + "}\n", + "print(dog)\n", + "print(dog[\"Breed\"])\n", + "print(f\"The {dog['Breed']} is expected to live about {dog['Age']} years\")" + ] }, { "cell_type": "markdown", @@ -470,19 +536,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 177, "metadata": {}, "outputs": [ { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'AD450_math'", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[33]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mAD450_math\u001b[39;00m\n\u001b[32m 3\u001b[39m AD450_math.add(\u001b[32m2\u001b[39m,\u001b[32m2\u001b[39m)\n", - "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'AD450_math'" - ] + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 177, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -504,9 +569,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 178, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 178, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Cell with import error\n", "AD450_math.multiply(2, 2)" @@ -514,18 +590,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 179, "metadata": {}, "outputs": [], "source": [ - "# Reload cell" + "# Reload cell\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 180, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 180, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Cell with correct output \n", "AD450_math.multiply(2, 2)" @@ -545,29 +632,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 181, "metadata": {}, "outputs": [], "source": [ - "# Create a" + "# Create a\n", + "a = 1 " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 182, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ - "# Print a" + "# Print a\n", + "print(a)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 183, "metadata": {}, "outputs": [], "source": [ - "# Set a to 4" + "# Set a to 4\n", + "a = 4 " ] } ], From 05f432c0130db30076d3214938417fac71a476b5 Mon Sep 17 00:00:00 2001 From: Julien Smith Date: Mon, 12 Jan 2026 23:56:56 -0800 Subject: [PATCH 03/10] cur wk2 --- week_2_intro_ipython.ipynb | 54 +++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index 78bed5b..b503869 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -347,7 +347,7 @@ }, { "cell_type": "code", - "execution_count": 173, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -361,11 +361,12 @@ ], "source": [ "i = 0\n", + "sums = 0\n", "nums = \"\"\n", "while i < 10: \n", " if i % 3 == 0:\n", " nums += str(i) + \" \"\n", - " sums = i + i \n", + " sums += i\n", " i += 1\n", "print(nums)\n", "print(sums)\n" @@ -410,7 +411,7 @@ }, { "cell_type": "code", - "execution_count": 174, + "execution_count": 3, "metadata": { "id": "lppms8ctzfRI" }, @@ -446,7 +447,7 @@ }, { "cell_type": "code", - "execution_count": 175, + "execution_count": 4, "metadata": { "id": "zw2NezoM0zfl" }, @@ -479,12 +480,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "id": "rbyZif9a1b2V" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "janurary 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 = [[\"janurary\", 31], [\"february\", 28], [\"march\", 31], [\"april\", 30], [\"may\", 31], [\"june\", 30]]\n", + "for i in range(len(months)):\n", + " print(f\"{months[i][0]} has {months[i][1]} days\")\n" + ] }, { "cell_type": "markdown", @@ -501,7 +519,7 @@ }, { "cell_type": "code", - "execution_count": 176, + "execution_count": 6, "metadata": { "id": "p0YE0nqs4Hcz" }, @@ -536,7 +554,7 @@ }, { "cell_type": "code", - "execution_count": 177, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -545,7 +563,7 @@ "4" ] }, - "execution_count": 177, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -569,7 +587,7 @@ }, { "cell_type": "code", - "execution_count": 178, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -578,7 +596,7 @@ "4" ] }, - "execution_count": 178, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -590,7 +608,7 @@ }, { "cell_type": "code", - "execution_count": 179, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -599,7 +617,7 @@ }, { "cell_type": "code", - "execution_count": 180, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -608,7 +626,7 @@ "4" ] }, - "execution_count": 180, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -632,7 +650,7 @@ }, { "cell_type": "code", - "execution_count": 181, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -642,7 +660,7 @@ }, { "cell_type": "code", - "execution_count": 182, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -660,7 +678,7 @@ }, { "cell_type": "code", - "execution_count": 183, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ From a6cbe4012399efbd2d5eeb638c31afc3e9105f98 Mon Sep 17 00:00:00 2001 From: Julien Smith Date: Tue, 13 Jan 2026 13:09:38 -0800 Subject: [PATCH 04/10] init wk 2 submission --- week_2_intro_ipython.ipynb | 49 +++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index b503869..7746dc9 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -393,12 +393,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": { "id": "ntBnYxzi1RPc" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Student 2\n", + "Student 4\n" + ] + } + ], + "source": [ + "sets = [[1,], [2], [3], [4], [5]]\n", + "for i in sets:\n", + " for j in i:\n", + " if j % 2 == 0:\n", + " print(\"Student \" + str(j))\n" + ] }, { "cell_type": "markdown", @@ -411,7 +426,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 40, "metadata": { "id": "lppms8ctzfRI" }, @@ -447,7 +462,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 41, "metadata": { "id": "zw2NezoM0zfl" }, @@ -480,7 +495,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 42, "metadata": { "id": "rbyZif9a1b2V" }, @@ -519,7 +534,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 43, "metadata": { "id": "p0YE0nqs4Hcz" }, @@ -554,7 +569,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 44, "metadata": {}, "outputs": [ { @@ -563,7 +578,7 @@ "4" ] }, - "execution_count": 7, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } @@ -587,7 +602,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 45, "metadata": {}, "outputs": [ { @@ -596,7 +611,7 @@ "4" ] }, - "execution_count": 8, + "execution_count": 45, "metadata": {}, "output_type": "execute_result" } @@ -608,7 +623,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ @@ -617,7 +632,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 47, "metadata": {}, "outputs": [ { @@ -626,7 +641,7 @@ "4" ] }, - "execution_count": 10, + "execution_count": 47, "metadata": {}, "output_type": "execute_result" } @@ -650,7 +665,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ @@ -660,7 +675,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 49, "metadata": {}, "outputs": [ { @@ -678,7 +693,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ From c9ccb348b41352d30ed9b55e5c6af1c0ae07e1e7 Mon Sep 17 00:00:00 2001 From: Julien Smith Date: Tue, 20 Jan 2026 11:11:36 -0800 Subject: [PATCH 05/10] cur progress for init submission --- week_3_numpy.ipynb | 412 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 348 insertions(+), 64 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index 1db8573..e449f1c 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -83,19 +83,46 @@ "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" + ] + }, + { + "ename": "AttributeError", + "evalue": "'numpy.ndarray' object has no attribute 'bytes'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[16]\u001b[39m\u001b[32m, line 11\u001b[39m\n\u001b[32m 9\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33msize:\u001b[39m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[33m\"\u001b[39m, numbers.size) \u001b[38;5;66;03m# number of elements in the array\u001b[39;00m\n\u001b[32m 10\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mdatatype:\u001b[39m\u001b[33m\"\u001b[39m, numbers.dtype) \u001b[38;5;66;03m# numpy determines the datatype\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m11\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mbytes:\u001b[39m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[33m\"\u001b[39m, \u001b[43mnumbers\u001b[49m\u001b[43m.\u001b[49m\u001b[43mbytes\u001b[49m) \u001b[38;5;66;03m# total bytes consumed by the array\u001b[39;00m\n", + "\u001b[31mAttributeError\u001b[39m: 'numpy.ndarray' object has no attribute 'bytes'" + ] + } + ], "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", "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.bytes) # total bytes consumed by the array" ] }, { @@ -109,7 +136,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -117,34 +144,63 @@ "id": "pTXgMRYRQS3v", "outputId": "010d4702-bc29-4f87-980e-66fe9b43436a" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 1. 1. 1.]\n", + " [1. 1. 1. 1.]\n", + " [1. 1. 1. 1.]] \n", + "\n", + "[[0. 0. 0. 0.]\n", + " [0. 0. 0. 0.]\n", + " [0. 0. 0. 0.]] \n", + "\n", + "[[-0.00791867 0.60836559]\n", + " [ 0.32089487 0.94509106]] \n", + "\n", + "[[0.375 0.75 ]\n", + " [1.125 1.5 ]\n", + " [1.875 2.25 ]] \n", + "\n", + "[['x' 'x']\n", + " ['x' 'x']] \n", + "\n", + "[10 15 20] \n", + "\n", + "[0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] \n", + "\n" + ] + } + ], "source": [ "# Create an 3 by 4 array of ones\n", - "ones = None\n", + "ones = np.ones([3, 4])\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\")\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(rando, \"\\n\")\n", "\n", "# Create 3 by 2 an empty array\n", - "empty = None\n", + "empty = np.empty((3, 2))\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\")\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\")\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\")\n" ] }, @@ -172,7 +228,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 64, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -180,9 +236,19 @@ "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", + "slice_arr1d = arr1d[5:8]\n", + "print(slice_arr1d)\n", "\n", "# using indexing select and output [5, 6, 7] from arr1d. Save this slice to a variable named slice_arr1d\n" ] @@ -196,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 62, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -204,9 +270,21 @@ "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", + "[ 5 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" + "# Replace 6, 7, and 8 with 12 in arr1d using one line of code. Then print arr1d and slice_arr1d\n", + "arr1d[6:9] = 12\n", + "print(arr1d)\n", + "print(slice_arr1d)\n" ] }, { @@ -218,7 +296,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 65, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -228,7 +306,8 @@ }, "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", + "new_array = arr1d.copy()" ] }, { @@ -240,11 +319,25 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 70, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "3\n", + "[[1 2 3 4]\n", + " [5 6 7 8]]\n" + ] + } + ], "source": [ "arr2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", + "print(arr2d[0][2])\n", + "print(arr2d[0, 2])\n", + "print(arr2d)\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. " ] @@ -258,7 +351,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 90, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -266,16 +359,36 @@ "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", + "\n", + "[[3]\n", + " [7]]\n", + "\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", - "\n", + "print(arr2d[:2, 1:4])\n", + "print(\"\\n\")\n", "# select first two rows and only 3rd column\n", + "print(arr2d[:2, 2:3])\n", + "print(\"\\n\")\n", "\n", - "# select all columns in the first row\n" + "# select all columns in the first row\n", + "print(arr2d[:1, :4])" ] }, { @@ -298,7 +411,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 99, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -306,12 +419,37 @@ "id": "IMEp_RGh_Fu5", "outputId": "45332ef8-2c42-4c40-af09-6caaf98bca6b" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 1 1 1 1 1 0]\n", + " [0 0 1 1 1 0 1]\n", + " [0 1 0 1 0 1 0]\n", + " [1 1 0 1 0 0 1]\n", + " [1 1 0 0 0 0 0]\n", + " [0 0 1 1 0 0 0]\n", + " [0 1 1 0 1 1 1]]\n", + "['Bob' 'Joe' 'Will' 'Bob' 'Will' 'Joe' 'Joe']\n", + "\n", + "\n", + "[[1 1 1 1 1 1 0]\n", + " [1 1 0 1 0 0 1]]\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", + "print(data)\n", + "print(names)\n", "\n", + "\n", + " \n", + "print(\"\\n\")\n", + "print(data[names == \"Bob\"])\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. " ] @@ -335,11 +473,20 @@ "id": "BgULatpwt7X_", "outputId": "c31d49b4-92ff-4514-c91b-8eeb42573b5f" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 1 1 1 1 1 0]\n", + " [1 1 0 1 0 0 1]]\n" + ] + } + ], "source": [ "# Again output the first and fouth rows using the cond below. Meaning replace None with an expression\n", - "cond = None\n", - "data[cond]" + "cond = data[names == \"Bob\"]\n", + "print(cond)" ] }, { @@ -353,7 +500,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 108, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -361,9 +508,25 @@ "id": "EP-SDDVIvK8s", "outputId": "cce4fab9-cd5e-46c2-c615-f2518f1b4a72" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 1 1 1 1 1 0]\n", + " [0 0 1 1 1 0 1]\n", + " [0 1 0 1 0 1 0]\n", + " [1 1 0 1 0 0 1]\n", + " [1 1 0 0 0 0 0]\n", + " [0 0 1 1 0 0 0]\n", + " [0 1 1 0 1 1 1]]\n" + ] + } + ], "source": [ - "# Select all rows except the first and forth" + "# Select all rows except the first and forth\n", + "data[~(names == \"Bob\")]\n", + "print(data)\n" ] }, { @@ -377,7 +540,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -387,7 +550,8 @@ }, "outputs": [], "source": [ - "# Select the last 5 columns for the first and forth row" + "# Select the last 5 columns for the first and forth row\n", + "data[()]" ] }, { @@ -436,7 +600,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 92, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -444,18 +608,36 @@ "id": "HdWTjc8CZI5R", "outputId": "e070b71e-bdee-4abc-f378-474978208ad5" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 2. 3.]\n", + " [4. 5. 6.]]\n", + "\n", + "\n", + "[[ 6. 7. 8.]\n", + " [ 9. 10. 11.]]\n", + "\n", + "\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(\"\\n\")\n", "\n", "# Add 5 to all values in array1 and print \n", - "print()\n", - "\n", + "print(5 + array1)\n", + "print(\"\\n\")\n", "\n", "# Multiple all values in array1 by 10 and print \n", - "print()\n" + "print(10 * array1)\n" ] }, { @@ -469,7 +651,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 94, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -477,11 +659,20 @@ "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": [ "array2 = np.array([[0., 4., 1.], [7., 2., 12.]])\n", "\n", - "\n", + "print(array1 < array2)\n", "# Test to see if pairwise elements in array1 are less than the corresponding element in array2. Output the results.\n", "# i.e. compariing the first elements of the first rows would output False because 1 is not less than 0" ] @@ -519,7 +710,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 145, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -527,13 +718,23 @@ "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", + "answer = np.where(cond == 1, arr1, arr2)\n", + "print(answer)" ] }, { @@ -564,7 +765,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 117, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -572,12 +773,30 @@ "id": "ImvBT7NX3aY7", "outputId": "d04b3b76-91e4-4a40-e047-1e7a55bfd4ed" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[2 1 0 1]\n", + " [3 0 1 0]\n", + " [3 2 0 3]\n", + " [0 2 1 3]\n", + " [0 2 2 2]]\n", + "1.4\n", + "28\n" + ] + } + ], "source": [ "arr = np.random.randint(4, size=(5, 4))\n", "print(arr)\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", + "mean = np.mean(arr)\n", + "sum = np.sum(arr)\n", + "print(mean)\n", + "print(sum)" ] }, { @@ -589,7 +808,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 124, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -597,9 +816,33 @@ "id": "kxw7AUVm31Ea", "outputId": "b1cb6ece-5056-41b1-8c1b-f57612b9dae1" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.33169958 0.51012861]\n", + "[0.66339916 1.02025723]\n", + "[0.69508798 0.14674021]\n", + "[1.39017596 0.29348042]\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", + "array_nums = np.random.standard_normal((2,2))\n", + "\n", + "mean_of_rows = np.mean(array_nums, axis=1)\n", + "sum_of_rows = np.sum(array_nums, axis=1)\n", + "\n", + "mean_of_columns = np.mean(array_nums, axis=0)\n", + "sum_of_columns = np.sum(array_nums, axis=0)\n", + "\n", + "print(mean_of_rows)\n", + "print(sum_of_rows)\n", + "\n", + "print(mean_of_columns)\n", + "print(sum_of_columns)\n" ] }, { @@ -611,15 +854,42 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 142, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "full array: [1 2 3 4 5 6]\n", + "\n", + "\n", + "columns [[1 1 1]\n", + " [2 2 2]]\n", + "\n", + "\n", + "rows: [[1 2 3]\n", + " [1 2 3]]\n" + ] + } + ], "source": [ + "arr6 = np.array([[1, 1, 1], [1, 1, 1]])\n", "# 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", + "rows = arr6.cumsum(axis=1)\n", + "columns = arr6.cumsum(axis=0)\n", + "fullarray = arr6.cumsum()\n", + "\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", + "print(f\"full array: {fullarray}\")\n", + "print(\"\\n\")\n", + "print(f\"columns {columns}\")\n", + "print(\"\\n\")\n", + "print(f\"rows: {rows}\")\n", + "\n" ] }, { @@ -631,7 +901,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 148, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -639,7 +909,17 @@ "id": "sDnJpvUR1B1W", "outputId": "c4806b83-0ba9-448e-c4b7-f9ca53a8900d" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 1 2 3 4 5 6 7 8 9]\n", + "[1 3 5 7 9]\n", + "[9 8 7 6 5 4 3 2 1 0]\n" + ] + } + ], "source": [ "\n", "arr = np.arange(10)\n", @@ -647,8 +927,12 @@ "\n", "\n", "# Print out all all odd values\n", + "odd_numbers = arr[arr % 2 == 1]\n", + "print(odd_numbers)\n", "\n", - "# Print out all values in reverse order\n" + "# Print out all values in reverse order\n", + "reverse_array = arr[::-1]\n", + "print(reverse_array)" ] } ], @@ -678,7 +962,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.4" + "version": "3.13.5" } }, "nbformat": 4, From 091f96fa94be377cb112a6d64dd638f1d55bdaa1 Mon Sep 17 00:00:00 2001 From: Julien Smith Date: Tue, 20 Jan 2026 11:18:50 -0800 Subject: [PATCH 06/10] cur progress for wk 3 init submission --- week_3_numpy.ipynb | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index e449f1c..3ec4774 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 153, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -96,18 +96,8 @@ "dimensions: 2\n", "shape:\t (2, 4)\n", "size:\t 8\n", - "datatype: int64\n" - ] - }, - { - "ename": "AttributeError", - "evalue": "'numpy.ndarray' object has no attribute 'bytes'", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[16]\u001b[39m\u001b[32m, line 11\u001b[39m\n\u001b[32m 9\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33msize:\u001b[39m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[33m\"\u001b[39m, numbers.size) \u001b[38;5;66;03m# number of elements in the array\u001b[39;00m\n\u001b[32m 10\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mdatatype:\u001b[39m\u001b[33m\"\u001b[39m, numbers.dtype) \u001b[38;5;66;03m# numpy determines the datatype\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m11\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mbytes:\u001b[39m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[33m\"\u001b[39m, \u001b[43mnumbers\u001b[49m\u001b[43m.\u001b[49m\u001b[43mbytes\u001b[49m) \u001b[38;5;66;03m# total bytes consumed by the array\u001b[39;00m\n", - "\u001b[31mAttributeError\u001b[39m: 'numpy.ndarray' object has no attribute 'bytes'" + "datatype: int64\n", + "bytes:\t 64\n" ] } ], @@ -115,6 +105,7 @@ "# 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 = np.array([[1,2,3,4], [5,6,7,8]])\n", "print(numbers)\n", + "numbers_bytes = numbers.nbytes\n", "\n", "# Replace None below with each value for the array. \n", "print(\"\\nInspect the array\\n--------------\")\n", @@ -122,7 +113,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.bytes) # total bytes consumed by the array" + "print(\"bytes:\\t\", numbers_bytes) # total bytes consumed by the array" ] }, { @@ -551,7 +542,7 @@ "outputs": [], "source": [ "# Select the last 5 columns for the first and forth row\n", - "data[()]" + "data[[])]" ] }, { From c0e11991a2deecf9ed001d31d437f41bdeab53f8 Mon Sep 17 00:00:00 2001 From: Julien Smith Date: Tue, 20 Jan 2026 19:40:37 -0800 Subject: [PATCH 07/10] final submission for numpy --- week_3_numpy.ipynb | 147 +++++++++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 60 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index 3ec4774..9978844 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -116,6 +116,13 @@ "print(\"bytes:\\t\", numbers_bytes) # total bytes consumed by the array" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": { @@ -253,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -273,7 +280,7 @@ ], "source": [ "# Replace 6, 7, and 8 with 12 in arr1d using one line of code. Then print arr1d and slice_arr1d\n", - "arr1d[6:9] = 12\n", + "arr1d[6:] = 12\n", "print(arr1d)\n", "print(slice_arr1d)\n" ] @@ -287,7 +294,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 154, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -310,7 +317,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 155, "metadata": {}, "outputs": [ { @@ -318,9 +325,7 @@ "output_type": "stream", "text": [ "3\n", - "3\n", - "[[1 2 3 4]\n", - " [5 6 7 8]]\n" + "3\n" ] } ], @@ -328,7 +333,6 @@ "arr2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", "print(arr2d[0][2])\n", "print(arr2d[0, 2])\n", - "print(arr2d)\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. " ] @@ -342,7 +346,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 159, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -372,7 +376,7 @@ "# Select and print out the following:\n", "\n", "# select first two rows and all but first column\n", - "print(arr2d[:2, 1:4])\n", + "print(arr2d[:2, 1:])\n", "print(\"\\n\")\n", "# select first two rows and only 3rd column\n", "print(arr2d[:2, 2:3])\n", @@ -402,7 +406,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -415,18 +419,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "[[1 1 1 1 1 1 0]\n", - " [0 0 1 1 1 0 1]\n", - " [0 1 0 1 0 1 0]\n", - " [1 1 0 1 0 0 1]\n", - " [1 1 0 0 0 0 0]\n", - " [0 0 1 1 0 0 0]\n", - " [0 1 1 0 1 1 1]]\n", + "[[1 1 0 1 0 1 0]\n", + " [1 1 1 1 1 0 0]\n", + " [1 1 1 1 1 0 0]\n", + " [0 1 1 1 1 0 0]\n", + " [1 1 1 0 0 1 0]\n", + " [0 1 1 1 1 0 1]\n", + " [1 0 1 0 1 1 1]]\n", "['Bob' 'Joe' 'Will' 'Bob' 'Will' 'Joe' 'Joe']\n", "\n", "\n", - "[[1 1 1 1 1 1 0]\n", - " [1 1 0 1 0 0 1]]\n" + "[[1 1 0 1 0 1 0]\n", + " [0 1 1 1 1 0 0]]\n" ] } ], @@ -476,7 +480,7 @@ ], "source": [ "# Again output the first and fouth rows using the cond below. Meaning replace None with an expression\n", - "cond = data[names == \"Bob\"]\n", + "cond = names ==\n", "print(cond)" ] }, @@ -491,7 +495,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -501,23 +505,20 @@ }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[1 1 1 1 1 1 0]\n", - " [0 0 1 1 1 0 1]\n", - " [0 1 0 1 0 1 0]\n", - " [1 1 0 1 0 0 1]\n", - " [1 1 0 0 0 0 0]\n", - " [0 0 1 1 0 0 0]\n", - " [0 1 1 0 1 1 1]]\n" + "ename": "IndexError", + "evalue": "boolean index did not match indexed array along axis 0; size of axis is 7 but size of corresponding boolean axis is 5", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mIndexError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[162]\u001b[39m\u001b[32m, line 4\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m# Select all rows except the first and forth\u001b[39;00m\n\u001b[32m 2\u001b[39m not_bob = ~cond\n\u001b[32m----> \u001b[39m\u001b[32m4\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[43mdata\u001b[49m\u001b[43m[\u001b[49m\u001b[43mnot_bob\u001b[49m\u001b[43m]\u001b[49m)\n", + "\u001b[31mIndexError\u001b[39m: boolean index did not match indexed array along axis 0; size of axis is 7 but size of corresponding boolean axis is 5" ] } ], "source": [ "# Select all rows except the first and forth\n", - "data[~(names == \"Bob\")]\n", - "print(data)\n" + "cond = names == \"Bob\"" ] }, { @@ -531,7 +532,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -539,10 +540,20 @@ "id": "QYjmF7eFvTmc", "outputId": "172adeb5-31d7-47ad-e3af-59c352f96f97" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 1 0 1 0]\n", + "[1 1 1 0 0]\n" + ] + } + ], "source": [ "# Select the last 5 columns for the first and forth row\n", - "data[[])]" + "print(data[0, 2:])\n", + "print(data[3, 2:])" ] }, { @@ -556,7 +567,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -564,9 +575,22 @@ "id": "sciz0aprvh8u", "outputId": "24c8c092-cbe9-4665-9e01-9d193bcac8df" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 1 0 1 0 1 0]\n", + " [1 1 1 1 1 0 0]\n", + " [0 1 1 1 1 0 0]\n", + " [1 1 1 0 0 1 0]]\n" + ] + } + ], "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", + "cond2 = (names == \"Bob\") | (names == \"Will\")\n", + "print(data[cond2])" ] }, { @@ -756,7 +780,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -769,13 +793,15 @@ "name": "stdout", "output_type": "stream", "text": [ - "[[2 1 0 1]\n", - " [3 0 1 0]\n", - " [3 2 0 3]\n", - " [0 2 1 3]\n", - " [0 2 2 2]]\n", - "1.4\n", - "28\n" + "[[1 1 2 3]\n", + " [0 0 0 0]\n", + " [1 2 3 2]\n", + " [3 2 1 1]\n", + " [1 2 1 3]]\n", + "\n", + "\n", + "the mean is 1.45\n", + "the sum is 29\n" ] } ], @@ -786,8 +812,9 @@ "# Calculate and print the mean and sum for all values in the array\n", "mean = np.mean(arr)\n", "sum = np.sum(arr)\n", - "print(mean)\n", - "print(sum)" + "print(\"\\n\")\n", + "print(f\"the mean is {mean}\")\n", + "print(f\"the sum is {sum}\")" ] }, { @@ -799,7 +826,7 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -812,22 +839,22 @@ "name": "stdout", "output_type": "stream", "text": [ - "[0.33169958 0.51012861]\n", - "[0.66339916 1.02025723]\n", - "[0.69508798 0.14674021]\n", - "[1.39017596 0.29348042]\n" + "[1.75 0. 2. 1.75 1.75]\n", + "[7 0 8 7 7]\n", + "[1.2 1.4 1.4 1.8]\n", + "[6 7 7 9]\n" ] } ], "source": [ "# Calculate the mean and sum across the columns and the rows. Print out both\n", - "array_nums = np.random.standard_normal((2,2))\n", "\n", - "mean_of_rows = np.mean(array_nums, axis=1)\n", - "sum_of_rows = np.sum(array_nums, axis=1)\n", "\n", - "mean_of_columns = np.mean(array_nums, axis=0)\n", - "sum_of_columns = np.sum(array_nums, axis=0)\n", + "mean_of_rows = np.mean(arr, axis=1)\n", + "sum_of_rows = np.sum(arr, axis=1)\n", + "\n", + "mean_of_columns = np.mean(arr, axis=0)\n", + "sum_of_columns = np.sum(arr, axis=0)\n", "\n", "print(mean_of_rows)\n", "print(sum_of_rows)\n", From a94e11f93d8f1c76dd49aad09c4c30b34d04ec10 Mon Sep 17 00:00:00 2001 From: Julien Smith Date: Fri, 23 Jan 2026 15:36:27 -0800 Subject: [PATCH 08/10] final submission for wk3 exercise --- week_3_numpy.ipynb | 121 ++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 62 deletions(-) diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index 9978844..893f386 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" }, @@ -116,13 +116,6 @@ "print(\"bytes:\\t\", numbers_bytes) # total bytes consumed by the array" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": { @@ -226,7 +219,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -260,7 +253,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -294,7 +287,7 @@ }, { "cell_type": "code", - "execution_count": 154, + "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -317,13 +310,14 @@ }, { "cell_type": "code", - "execution_count": 155, + "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "3\n", "3\n", "3\n" ] @@ -333,6 +327,7 @@ "arr2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", "print(arr2d[0][2])\n", "print(arr2d[0, 2])\n", + "print(arr2d[-2][2])\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. " ] @@ -346,7 +341,7 @@ }, { "cell_type": "code", - "execution_count": 159, + "execution_count": 20, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -406,7 +401,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -419,18 +414,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "[[1 1 0 1 0 1 0]\n", - " [1 1 1 1 1 0 0]\n", - " [1 1 1 1 1 0 0]\n", - " [0 1 1 1 1 0 0]\n", + "[[1 0 0 1 1 1 1]\n", + " [0 1 1 0 1 1 0]\n", + " [1 1 0 0 0 0 1]\n", " [1 1 1 0 0 1 0]\n", - " [0 1 1 1 1 0 1]\n", - " [1 0 1 0 1 1 1]]\n", + " [0 1 0 0 0 1 1]\n", + " [0 1 1 1 0 0 1]\n", + " [1 1 1 1 1 0 0]]\n", "['Bob' 'Joe' 'Will' 'Bob' 'Will' 'Joe' 'Joe']\n", "\n", "\n", - "[[1 1 0 1 0 1 0]\n", - " [0 1 1 1 1 0 0]]\n" + "[[1 0 0 1 1 1 1]\n", + " [1 1 1 0 0 1 0]]\n" ] } ], @@ -460,7 +455,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -473,14 +468,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "[[1 1 1 1 1 1 0]\n", - " [1 1 0 1 0 0 1]]\n" + "[[1 0 0 1 1 1 1]\n", + " [1 1 1 0 0 1 0]]\n" ] } ], "source": [ "# Again output the first and fouth rows using the cond below. Meaning replace None with an expression\n", - "cond = names ==\n", + "cond = data[names == \"Bob\"]\n", "print(cond)" ] }, @@ -495,7 +490,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -505,20 +500,21 @@ }, "outputs": [ { - "ename": "IndexError", - "evalue": "boolean index did not match indexed array along axis 0; size of axis is 7 but size of corresponding boolean axis is 5", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mIndexError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[162]\u001b[39m\u001b[32m, line 4\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m# Select all rows except the first and forth\u001b[39;00m\n\u001b[32m 2\u001b[39m not_bob = ~cond\n\u001b[32m----> \u001b[39m\u001b[32m4\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[43mdata\u001b[49m\u001b[43m[\u001b[49m\u001b[43mnot_bob\u001b[49m\u001b[43m]\u001b[49m)\n", - "\u001b[31mIndexError\u001b[39m: boolean index did not match indexed array along axis 0; size of axis is 7 but size of corresponding boolean axis is 5" + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 1 1 0 1 1 0]\n", + " [1 1 0 0 0 0 1]\n", + " [0 1 0 0 0 1 1]\n", + " [0 1 1 1 0 0 1]\n", + " [1 1 1 1 1 0 0]]\n" ] } ], "source": [ "# Select all rows except the first and forth\n", - "cond = names == \"Bob\"" + "negatedData = data[~(names == \"Bob\")]\n", + "print(negatedData)" ] }, { @@ -532,7 +528,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -545,8 +541,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "[0 1 0 1 0]\n", - "[1 1 1 0 0]\n" + "[0 1 1 1 1]\n", + "[1 0 0 1 0]\n" ] } ], @@ -567,7 +563,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -580,10 +576,10 @@ "name": "stdout", "output_type": "stream", "text": [ - "[[1 1 0 1 0 1 0]\n", - " [1 1 1 1 1 0 0]\n", - " [0 1 1 1 1 0 0]\n", - " [1 1 1 0 0 1 0]]\n" + "[[1 0 0 1 1 1 1]\n", + " [1 1 0 0 0 0 1]\n", + " [1 1 1 0 0 1 0]\n", + " [0 1 0 0 0 1 1]]\n" ] } ], @@ -615,7 +611,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 26, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -666,7 +662,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 27, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -725,7 +721,7 @@ }, { "cell_type": "code", - "execution_count": 145, + "execution_count": 28, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -780,7 +776,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -793,15 +789,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "[[1 1 2 3]\n", - " [0 0 0 0]\n", - " [1 2 3 2]\n", - " [3 2 1 1]\n", - " [1 2 1 3]]\n", + "[[3 2 0 1]\n", + " [2 1 3 3]\n", + " [1 1 1 3]\n", + " [0 2 3 0]\n", + " [1 0 3 2]]\n", "\n", "\n", - "the mean is 1.45\n", - "the sum is 29\n" + "the mean is 1.6\n", + "the sum is 32\n", + "(5, 4)\n" ] } ], @@ -826,7 +823,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 43, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -839,10 +836,10 @@ "name": "stdout", "output_type": "stream", "text": [ - "[1.75 0. 2. 1.75 1.75]\n", - "[7 0 8 7 7]\n", - "[1.2 1.4 1.4 1.8]\n", - "[6 7 7 9]\n" + "[2. 1.75 1.25 1.5 1.5 ]\n", + "[8 7 5 6 6]\n", + "[2. 1.2 1.8 1.4]\n", + "[10 6 9 7]\n" ] } ], @@ -872,7 +869,7 @@ }, { "cell_type": "code", - "execution_count": 142, + "execution_count": 37, "metadata": {}, "outputs": [ { @@ -919,7 +916,7 @@ }, { "cell_type": "code", - "execution_count": 148, + "execution_count": 36, "metadata": { "colab": { "base_uri": "https://localhost:8080/" From ed81ecd5ab5c6558c6dc182dd6c93af2d6586f9d Mon Sep 17 00:00:00 2001 From: Julien Smith Date: Tue, 27 Jan 2026 13:50:45 -0800 Subject: [PATCH 09/10] initial submission for week 4 --- week_4_panadas.ipynb | 568 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 495 insertions(+), 73 deletions(-) diff --git a/week_4_panadas.ipynb b/week_4_panadas.ipynb index 9080a02..ed5ae00 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": [ @@ -52,10 +52,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 1\n", + "1 2\n", + "2 3\n", + "3 4\n", + "dtype: int64\n", + "\n", + "[np.int64(1), np.int64(2), np.int64(3), np.int64(4)]\n", + "Length: 4, dtype: int64\n", + "RangeIndex(start=0, stop=4, step=1)\n" + ] + } + ], + "source": [ + "pandas_series = pd.Series([1, 2, 3, 4])\n", + "print(pandas_series)\n", + "print(pandas_series.array)\n", + "print(pandas_series.index)" + ] }, { "cell_type": "markdown", @@ -66,10 +87,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "series2 = pd.Series([1, 2, 3, 4], index = [\"a\", \"b\", \"c\", \"d\"])\n", + "print(series2[\"a\"])" + ] }, { "cell_type": "markdown", @@ -85,10 +117,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ohio 35000\n", + "Texas 71000\n", + "Oregon 16000\n", + "Utah 5000\n", + "dtype: int64\n" + ] + } + ], + "source": [ + "states_dict = {\"Ohio\": 35000, \"Texas\": 71000,\n", + " \"Oregon\": 16000, \"Utah\":5000}\n", + "states_series = pd.Series(states_dict)\n", + "print(states_series) " + ] }, { "cell_type": "markdown", @@ -99,10 +148,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "OH 35000\n", + "TX 71000\n", + "OR 16000\n", + "UT 5000\n", + "dtype: int64\n" + ] + } + ], + "source": [ + "states_series.index = [\"OH\", \"TX\", \"OR\", \"UT\"]\n", + "print(states_series)" + ] }, { "cell_type": "markdown", @@ -125,9 +189,91 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 24, + "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", + "
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": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "data = {\n", " 'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],\n", @@ -148,10 +294,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 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\n" + ] + } + ], + "source": [ + "df.index = [\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\"]\n", + "print(df)" + ] }, { "cell_type": "markdown", @@ -162,10 +325,37 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "zero 2000\n", + "one 2001\n", + "two 2002\n", + "three 2001\n", + "four 2002\n", + "five 2003\n", + "Name: year, dtype: int64\n", + "\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[\"year\"])\n", + "print(\"\\n\")\n", + "print(df.iloc[:, 0])" + ] }, { "cell_type": "markdown", @@ -176,10 +366,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "state Ohio\n", + "year 2002\n", + "pop 3.6\n", + "Name: two, dtype: object\n", + "state Ohio\n", + "year 2002\n", + "pop 3.6\n", + "Name: two, dtype: object\n" + ] + } + ], + "source": [ + "print(df.loc[\"two\"])\n", + "print(df.iloc[2])" + ] }, { "cell_type": "markdown", @@ -190,10 +398,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 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\n" + ] + } + ], + "source": [ + "df[\"rating\"] = np.arange(5, -1, -1)\n", + "print(df)" + ] }, { "cell_type": "markdown", @@ -204,10 +429,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 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\n" + ] + } + ], + "source": [ + "df[\"nonsense\"] = df[\"rating\"] * df[\"pop\"]\n", + "print(df)" + ] }, { "cell_type": "markdown", @@ -224,10 +466,33 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " series_numerical series_zeros series_random\n", + "a 0 0 0.783306\n", + "b 1 0 0.423269\n", + "c 2 0 0.538404\n", + "d 3 0 0.157979\n", + "e 4 0 0.431871\n" + ] + } + ], + "source": [ + "series_numerical = pd.Series([0, 1, 2, 3, 4], index=[\"a\",\"b\",\"c\",\"d\",\"e\"])\n", + "series_zeros = pd.Series(data = 0,index = [\"a\",\"b\",\"c\",\"d\",\"e\"])\n", + "random_nums = np.random.rand(5)\n", + "series_random = pd.Series(random_nums, index =[\"a\",\"b\",\"c\",\"d\",\"e\"])\n", + "\n", + "numeric_df = pd.DataFrame({\"series_numerical\": series_numerical,\n", + " \"series_zeros\": series_zeros,\n", + " \"series_random\": series_random})\n", + "print(numeric_df)\n" + ] }, { "cell_type": "markdown", @@ -238,10 +503,25 @@ }, { "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " numerical zeroes random 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.783306 0.423269 0.538404 0.157979 0.431871\n" + ] + } + ], + "source": [ + "transposed = numeric_df.T\n", + "transposed_numeric_df = transposed.rename(columns ={\"a\": \"numerical\", \"b\": \"zeroes\", \"c\": \"random\"})\n", + "print(transposed_numeric_df)" + ] }, { "cell_type": "markdown", @@ -252,10 +532,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " numerical zeroes random d e\n", + "series_numerical 0.000000 1.000000 2.000000 3.000000 4.000000\n", + "series_random 0.783306 0.423269 0.538404 0.157979 0.431871\n", + "series_zeros 0.000000 0.000000 0.000000 0.000000 0.000000\n" + ] + } + ], + "source": [ + "sorted_df = transposed_numeric_df.sort_values(by=\"random\", ascending = False)\n", + "print(sorted_df)" + ] }, { "cell_type": "markdown", @@ -266,10 +560,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "series_numerical False\n", + "series_random False\n", + "series_zeros False\n", + "Name: numerical, dtype: bool\n" + ] + } + ], + "source": [ + "print(sorted_df[\"numerical\"] > 2 )" + ] }, { "cell_type": "markdown", @@ -280,10 +587,52 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " numerical zeroes random d e random_5\n", + "series_numerical 0.000000 1.000000 2.000000 3.000000 4.000000 10.000000\n", + "series_random 0.783306 0.423269 0.538404 0.157979 0.431871 2.692021\n", + "series_zeros 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\yahar\\AppData\\Local\\Temp\\ipykernel_57908\\1350366751.py:6: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n", + " select_rows = df[cond]\n" + ] + }, + { + "ename": "IndexingError", + "evalue": "Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mIndexingError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[75]\u001b[39m\u001b[32m, line 6\u001b[39m\n\u001b[32m 2\u001b[39m \u001b[38;5;28mprint\u001b[39m(sorted_df)\n\u001b[32m 4\u001b[39m cond = sorted_df[\u001b[33m\"\u001b[39m\u001b[33mnumerical\u001b[39m\u001b[33m\"\u001b[39m] > sorted_df[\u001b[33m\"\u001b[39m\u001b[33mrandom_5\u001b[39m\u001b[33m\"\u001b[39m]\n\u001b[32m----> \u001b[39m\u001b[32m6\u001b[39m select_rows = \u001b[43mdf\u001b[49m\u001b[43m[\u001b[49m\u001b[43mcond\u001b[49m\u001b[43m]\u001b[49m\n\u001b[32m 8\u001b[39m \u001b[38;5;28mprint\u001b[39m(select_rows) \n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\yahar\\anaconda3\\Lib\\site-packages\\pandas\\core\\frame.py:4093\u001b[39m, in \u001b[36mDataFrame.__getitem__\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 4091\u001b[39m \u001b[38;5;66;03m# Do we have a (boolean) 1d indexer?\u001b[39;00m\n\u001b[32m 4092\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m com.is_bool_indexer(key):\n\u001b[32m-> \u001b[39m\u001b[32m4093\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_getitem_bool_array\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 4095\u001b[39m \u001b[38;5;66;03m# We are left with two options: a single key, and a collection of keys,\u001b[39;00m\n\u001b[32m 4096\u001b[39m \u001b[38;5;66;03m# We interpret tuples as collections only for non-MultiIndex\u001b[39;00m\n\u001b[32m 4097\u001b[39m is_single_key = \u001b[38;5;28misinstance\u001b[39m(key, \u001b[38;5;28mtuple\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_list_like(key)\n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\yahar\\anaconda3\\Lib\\site-packages\\pandas\\core\\frame.py:4149\u001b[39m, in \u001b[36mDataFrame._getitem_bool_array\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 4143\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[32m 4144\u001b[39m \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mItem wrong length \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(key)\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m instead of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m.index)\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 4145\u001b[39m )\n\u001b[32m 4147\u001b[39m \u001b[38;5;66;03m# check_bool_indexer will throw exception if Series key cannot\u001b[39;00m\n\u001b[32m 4148\u001b[39m \u001b[38;5;66;03m# be reindexed to match DataFrame rows\u001b[39;00m\n\u001b[32m-> \u001b[39m\u001b[32m4149\u001b[39m key = \u001b[43mcheck_bool_indexer\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 4151\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m key.all():\n\u001b[32m 4152\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m.copy(deep=\u001b[38;5;28;01mNone\u001b[39;00m)\n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\yahar\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexing.py:2662\u001b[39m, in \u001b[36mcheck_bool_indexer\u001b[39m\u001b[34m(index, key)\u001b[39m\n\u001b[32m 2660\u001b[39m indexer = result.index.get_indexer_for(index)\n\u001b[32m 2661\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m -\u001b[32m1\u001b[39m \u001b[38;5;129;01min\u001b[39;00m indexer:\n\u001b[32m-> \u001b[39m\u001b[32m2662\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m IndexingError(\n\u001b[32m 2663\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mUnalignable boolean Series provided as \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 2664\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mindexer (index of the boolean Series and of \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 2665\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mthe indexed object do not match).\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 2666\u001b[39m )\n\u001b[32m 2668\u001b[39m result = result.take(indexer)\n\u001b[32m 2670\u001b[39m \u001b[38;5;66;03m# fall through for boolean\u001b[39;00m\n", + "\u001b[31mIndexingError\u001b[39m: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match)." + ] + } + ], + "source": [ + "sorted_df[\"random_5\"] = sorted_df[\"random\"] * 5 \n", + "print(sorted_df)\n", + "\n", + "cond = sorted_df[\"numerical\"] > sorted_df[\"random_5\"]\n", + "\n", + "select_rows = df[cond]\n", + "\n", + "print(select_rows) " + ] }, { "cell_type": "markdown", @@ -294,10 +643,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " numerical zeroes random d e \\\n", + "series_numerical 0.000000 1.000000 2.000000 3.000000 4.000000 \n", + "series_random 0.783306 0.423269 0.538404 0.157979 0.431871 \n", + "series_zeros 0.000000 0.000000 0.000000 0.000000 0.000000 \n", + "\n", + " random_5 even \n", + "series_numerical 10.000000 True \n", + "series_random 2.692021 False \n", + "series_zeros 0.000000 True \n" + ] + } + ], + "source": [ + "sorted_df[\"even\"] = sorted_df[\"numerical\"] % 2 == 0 \n", + "\n", + "print(sorted_df)" + ] }, { "cell_type": "markdown", @@ -308,10 +677,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "cannot assign to literal (3413497956.py, line 1)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[77]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31msorted_df[\"even_odd\"] = \"odd\" = sorted_df[\"numerical\"] % 2 == 0\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m cannot assign to literal\n" + ] + } + ], + "source": [ + "sorted_df[\"even_odd\"] = \"odd\" = sorted_df[\"numerical\"] % 2 == 0 " + ] }, { "cell_type": "markdown", @@ -322,10 +702,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "numerical 0.783306\n", + "zeroes 1.423269\n", + "random 2.538404\n", + "d 3.157979\n", + "e 4.431871\n", + "random_5 12.692021\n", + "even 2.000000\n", + "dtype: float64\n" + ] + } + ], + "source": [ + "sum_columns = sorted_df.sum()\n", + "print(sum_columns)" + ] }, { "cell_type": "markdown", @@ -338,8 +736,32 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'series_random'}\n", + "series_numerical\n", + "series_numerical\n", + "series_numerical\n", + "series_numerical\n" + ] + } + ], + "source": [ + "max_index_numerical = sorted_df[\"numerical\"].idxmax()\n", + "max_index_zeroes = sorted_df[\"zeroes\"].idxmax()\n", + "max_index_random = sorted_df[\"random\"].idxmax()\n", + "max_index_e = sorted_df[\"e\"].idxmax()\n", + "max_index_random_5 = sorted_df[\"random_5\"].idxmax()\n", + "\n", + "print(max_index_numerical)\n", + "print(max_index_zeroes)\n", + "print(max_index_random)\n", + "print(max_index_e)\n", + "print(max_index_random_5)" + ] } ], "metadata": { @@ -358,7 +780,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.4" + "version": "3.13.5" } }, "nbformat": 4, From 168d68231f6e0925400f3c4d84536f3676b318f9 Mon Sep 17 00:00:00 2001 From: Julien Smith Date: Fri, 30 Jan 2026 13:48:07 -0800 Subject: [PATCH 10/10] final submission for week 4 pandas --- week_4_panadas.ipynb | 462 +++++++++++++++++++++++++++++++------------ 1 file changed, 337 insertions(+), 125 deletions(-) diff --git a/week_4_panadas.ipynb b/week_4_panadas.ipynb index ed5ae00..f0fa221 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": [ @@ -466,7 +466,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -474,20 +474,25 @@ "output_type": "stream", "text": [ " series_numerical series_zeros series_random\n", - "a 0 0 0.783306\n", - "b 1 0 0.423269\n", - "c 2 0 0.538404\n", - "d 3 0 0.157979\n", - "e 4 0 0.431871\n" + "a 0 0.0 0.516706\n", + "b 1 0.0 0.742704\n", + "c 2 0.0 0.487931\n", + "d 3 0.0 0.460146\n", + "e 4 0.0 0.542536\n" ] } ], "source": [ - "series_numerical = pd.Series([0, 1, 2, 3, 4], index=[\"a\",\"b\",\"c\",\"d\",\"e\"])\n", - "series_zeros = pd.Series(data = 0,index = [\"a\",\"b\",\"c\",\"d\",\"e\"])\n", + "numbers = np.array([0, 1, 2, 3, 4])\n", + "zeros = np.zeros(5)\n", "random_nums = np.random.rand(5)\n", - "series_random = pd.Series(random_nums, index =[\"a\",\"b\",\"c\",\"d\",\"e\"])\n", "\n", + "\n", + "\n", + "\n", + "series_numerical = pd.Series(numbers, index=[\"a\",\"b\",\"c\",\"d\",\"e\"])\n", + "series_zeros = pd.Series(zeros, index=[\"a\",\"b\",\"c\",\"d\",\"e\"])\n", + "series_random = pd.Series(random_nums, index =[\"a\",\"b\",\"c\",\"d\",\"e\"])\n", "numeric_df = pd.DataFrame({\"series_numerical\": series_numerical,\n", " \"series_zeros\": series_zeros,\n", " \"series_random\": series_random})\n", @@ -503,24 +508,75 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 26, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - " numerical zeroes random 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.783306 0.423269 0.538404 0.157979 0.431871\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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandom
series_numerical0.0000001.0000002.000000
series_zeros0.0000000.0000000.000000
series_random0.5167060.7427040.487931
\n", + "
" + ], + "text/plain": [ + " numerical zeros random\n", + "series_numerical 0.000000 1.000000 2.000000\n", + "series_zeros 0.000000 0.000000 0.000000\n", + "series_random 0.516706 0.742704 0.487931" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "transposed = numeric_df.T\n", - "transposed_numeric_df = transposed.rename(columns ={\"a\": \"numerical\", \"b\": \"zeroes\", \"c\": \"random\"})\n", - "print(transposed_numeric_df)" + "transposed_numeric_df = numeric_df.T.loc[:,[\"a\", \"b\", \"c\" ]].rename(\n", + " columns={\"a\": \"numerical\", \"b\": \"zeros\", \"c\": \"random\"}\n", + ")\n", + "transposed_numeric_df" ] }, { @@ -532,23 +588,23 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - " numerical zeroes random d e\n", - "series_numerical 0.000000 1.000000 2.000000 3.000000 4.000000\n", - "series_random 0.783306 0.423269 0.538404 0.157979 0.431871\n", - "series_zeros 0.000000 0.000000 0.000000 0.000000 0.000000\n" + " numerical zeros random\n", + "series_numerical 0.000000 1.000000 2.000000\n", + "series_zeros 0.000000 0.000000 0.000000\n", + "series_random 0.516706 0.742704 0.487931\n" ] } ], "source": [ - "sorted_df = transposed_numeric_df.sort_values(by=\"random\", ascending = False)\n", - "print(sorted_df)" + "transposed_numeric_df.sort_values(by=\"random\", ascending = False)\n", + "print(transposed_numeric_df)" ] }, { @@ -560,22 +616,54 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 28, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "series_numerical False\n", - "series_random False\n", - "series_zeros False\n", - "Name: numerical, dtype: bool\n" - ] + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandom
\n", + "
" + ], + "text/plain": [ + "Empty DataFrame\n", + "Columns: [numerical, zeros, random]\n", + "Index: []" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print(sorted_df[\"numerical\"] > 2 )" + "greater_than_two = transposed_numeric_df[transposed_numeric_df[\"numerical\"] > 2]\n", + "greater_than_two" ] }, { @@ -587,51 +675,68 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - " numerical zeroes random d e random_5\n", - "series_numerical 0.000000 1.000000 2.000000 3.000000 4.000000 10.000000\n", - "series_random 0.783306 0.423269 0.538404 0.157979 0.431871 2.692021\n", - "series_zeros 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "C:\\Users\\yahar\\AppData\\Local\\Temp\\ipykernel_57908\\1350366751.py:6: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n", - " select_rows = df[cond]\n" + " numerical zeros random random_5\n", + "series_numerical 0.000000 1.000000 2.000000 10.000000\n", + "series_zeros 0.000000 0.000000 0.000000 0.000000\n", + "series_random 0.516706 0.742704 0.487931 2.439657\n" ] }, { - "ename": "IndexingError", - "evalue": "Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mIndexingError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[75]\u001b[39m\u001b[32m, line 6\u001b[39m\n\u001b[32m 2\u001b[39m \u001b[38;5;28mprint\u001b[39m(sorted_df)\n\u001b[32m 4\u001b[39m cond = sorted_df[\u001b[33m\"\u001b[39m\u001b[33mnumerical\u001b[39m\u001b[33m\"\u001b[39m] > sorted_df[\u001b[33m\"\u001b[39m\u001b[33mrandom_5\u001b[39m\u001b[33m\"\u001b[39m]\n\u001b[32m----> \u001b[39m\u001b[32m6\u001b[39m select_rows = \u001b[43mdf\u001b[49m\u001b[43m[\u001b[49m\u001b[43mcond\u001b[49m\u001b[43m]\u001b[49m\n\u001b[32m 8\u001b[39m \u001b[38;5;28mprint\u001b[39m(select_rows) \n", - "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\yahar\\anaconda3\\Lib\\site-packages\\pandas\\core\\frame.py:4093\u001b[39m, in \u001b[36mDataFrame.__getitem__\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 4091\u001b[39m \u001b[38;5;66;03m# Do we have a (boolean) 1d indexer?\u001b[39;00m\n\u001b[32m 4092\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m com.is_bool_indexer(key):\n\u001b[32m-> \u001b[39m\u001b[32m4093\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_getitem_bool_array\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 4095\u001b[39m \u001b[38;5;66;03m# We are left with two options: a single key, and a collection of keys,\u001b[39;00m\n\u001b[32m 4096\u001b[39m \u001b[38;5;66;03m# We interpret tuples as collections only for non-MultiIndex\u001b[39;00m\n\u001b[32m 4097\u001b[39m is_single_key = \u001b[38;5;28misinstance\u001b[39m(key, \u001b[38;5;28mtuple\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_list_like(key)\n", - "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\yahar\\anaconda3\\Lib\\site-packages\\pandas\\core\\frame.py:4149\u001b[39m, in \u001b[36mDataFrame._getitem_bool_array\u001b[39m\u001b[34m(self, key)\u001b[39m\n\u001b[32m 4143\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[32m 4144\u001b[39m \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mItem wrong length \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(key)\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m instead of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m.index)\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 4145\u001b[39m )\n\u001b[32m 4147\u001b[39m \u001b[38;5;66;03m# check_bool_indexer will throw exception if Series key cannot\u001b[39;00m\n\u001b[32m 4148\u001b[39m \u001b[38;5;66;03m# be reindexed to match DataFrame rows\u001b[39;00m\n\u001b[32m-> \u001b[39m\u001b[32m4149\u001b[39m key = \u001b[43mcheck_bool_indexer\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 4151\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m key.all():\n\u001b[32m 4152\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m.copy(deep=\u001b[38;5;28;01mNone\u001b[39;00m)\n", - "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\yahar\\anaconda3\\Lib\\site-packages\\pandas\\core\\indexing.py:2662\u001b[39m, in \u001b[36mcheck_bool_indexer\u001b[39m\u001b[34m(index, key)\u001b[39m\n\u001b[32m 2660\u001b[39m indexer = result.index.get_indexer_for(index)\n\u001b[32m 2661\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m -\u001b[32m1\u001b[39m \u001b[38;5;129;01min\u001b[39;00m indexer:\n\u001b[32m-> \u001b[39m\u001b[32m2662\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m IndexingError(\n\u001b[32m 2663\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mUnalignable boolean Series provided as \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 2664\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mindexer (index of the boolean Series and of \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 2665\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mthe indexed object do not match).\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 2666\u001b[39m )\n\u001b[32m 2668\u001b[39m result = result.take(indexer)\n\u001b[32m 2670\u001b[39m \u001b[38;5;66;03m# fall through for boolean\u001b[39;00m\n", - "\u001b[31mIndexingError\u001b[39m: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match)." - ] + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandomrandom_5
\n", + "
" + ], + "text/plain": [ + "Empty DataFrame\n", + "Columns: [numerical, zeros, random, random_5]\n", + "Index: []" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "sorted_df[\"random_5\"] = sorted_df[\"random\"] * 5 \n", - "print(sorted_df)\n", + "transposed_numeric_df[\"random_5\"] = transposed_numeric_df[\"random\"] * 5 \n", + "print(transposed_numeric_df)\n", + "greater_than_random_5 = transposed_numeric_df[transposed_numeric_df[\"numerical\"] > transposed_numeric_df[\"random_5\"]]\n", "\n", - "cond = sorted_df[\"numerical\"] > sorted_df[\"random_5\"]\n", - "\n", - "select_rows = df[cond]\n", - "\n", - "print(select_rows) " + "greater_than_random_5" ] }, { @@ -643,29 +748,83 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 36, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - " numerical zeroes random d e \\\n", - "series_numerical 0.000000 1.000000 2.000000 3.000000 4.000000 \n", - "series_random 0.783306 0.423269 0.538404 0.157979 0.431871 \n", - "series_zeros 0.000000 0.000000 0.000000 0.000000 0.000000 \n", - "\n", - " random_5 even \n", - "series_numerical 10.000000 True \n", - "series_random 2.692021 False \n", - "series_zeros 0.000000 True \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandomrandom_5even
series_numerical0.0000001.0000002.00000010.000000True
series_zeros0.0000000.0000000.0000000.000000True
series_random0.5167060.7427040.4879312.439657False
\n", + "
" + ], + "text/plain": [ + " numerical zeros random random_5 even\n", + "series_numerical 0.000000 1.000000 2.000000 10.000000 True\n", + "series_zeros 0.000000 0.000000 0.000000 0.000000 True\n", + "series_random 0.516706 0.742704 0.487931 2.439657 False" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "sorted_df[\"even\"] = sorted_df[\"numerical\"] % 2 == 0 \n", + "transposed_numeric_df[\"even\"] = transposed_numeric_df[\"numerical\"] % 2 == 0\n", "\n", - "print(sorted_df)" + "\n", + "transposed_numeric_df" ] }, { @@ -677,20 +836,85 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 40, "metadata": {}, "outputs": [ { - "ename": "SyntaxError", - "evalue": "cannot assign to literal (3413497956.py, line 1)", - "output_type": "error", - "traceback": [ - " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[77]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31msorted_df[\"even_odd\"] = \"odd\" = sorted_df[\"numerical\"] % 2 == 0\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m cannot assign to literal\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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandomrandom_5eveneven_odd
series_numerical0.0000001.0000002.00000010.000000Trueeven
series_zeros0.0000000.0000000.0000000.000000Trueeven
series_random0.5167060.7427040.4879312.439657Falseodd
\n", + "
" + ], + "text/plain": [ + " numerical zeros random random_5 even even_odd\n", + "series_numerical 0.000000 1.000000 2.000000 10.000000 True even\n", + "series_zeros 0.000000 0.000000 0.000000 0.000000 True even\n", + "series_random 0.516706 0.742704 0.487931 2.439657 False odd" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "sorted_df[\"even_odd\"] = \"odd\" = sorted_df[\"numerical\"] % 2 == 0 " + "transposed_numeric_df[\"even_odd\"] = np.where(transposed_numeric_df[\"numerical\"] % 2 != 0, \"odd\", \"even\")\n", + "transposed_numeric_df" ] }, { @@ -702,26 +926,25 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "numerical 0.783306\n", - "zeroes 1.423269\n", - "random 2.538404\n", - "d 3.157979\n", - "e 4.431871\n", - "random_5 12.692021\n", - "even 2.000000\n", - "dtype: float64\n" + "numerical 0.516706\n", + "zeros 1.742704\n", + "random 2.487931\n", + "random_5 12.439657\n", + "even 2\n", + "even_odd evenevenodd\n", + "dtype: object\n" ] } ], "source": [ - "sum_columns = sorted_df.sum()\n", + "sum_columns = transposed_numeric_df.sum()\n", "print(sum_columns)" ] }, @@ -738,29 +961,18 @@ "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'series_random'}\n", - "series_numerical\n", - "series_numerical\n", - "series_numerical\n", - "series_numerical\n" - ] + "data": { + "text/plain": [ + "'series_random'" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "max_index_numerical = sorted_df[\"numerical\"].idxmax()\n", - "max_index_zeroes = sorted_df[\"zeroes\"].idxmax()\n", - "max_index_random = sorted_df[\"random\"].idxmax()\n", - "max_index_e = sorted_df[\"e\"].idxmax()\n", - "max_index_random_5 = sorted_df[\"random_5\"].idxmax()\n", - "\n", - "print(max_index_numerical)\n", - "print(max_index_zeroes)\n", - "print(max_index_random)\n", - "print(max_index_e)\n", - "print(max_index_random_5)" + "transposed_numeric_df[\"numerical\"].idxmax()\n" ] } ],