|
943 | 943 | "source": [ |
944 | 944 | "def element_wise_sum_1(nums1, nums2):\n", |
945 | 945 | " # A: init the output list\n", |
946 | | - " summed_elems = [0 for _ in range(len(nums1))]\n", |
| 946 | + " summed_elems = [0 for _ in nums1]\n", |
947 | 947 | " # B: iterate over both input lists\n", |
948 | | - " for index in range(len(nums1)):\n", |
| 948 | + " for index in range(len(summed_elems)):\n", |
949 | 949 | " # C: place the next sum needed in the output list\n", |
950 | 950 | " elem_sum = nums1[index] + nums2[index]\n", |
951 | 951 | " summed_elems[index] = elem_sum\n", |
952 | 952 | " # D: return the output\n", |
953 | 953 | " return summed_elems" |
954 | 954 | ], |
955 | 955 | "id": "FLsum4Ac-EuZ", |
956 | | - "execution_count": null, |
| 956 | + "execution_count": 32, |
957 | 957 | "outputs": [] |
958 | 958 | }, |
959 | 959 | { |
|
979 | 979 | "print(element_wise_sum_1(nums1, nums2))" |
980 | 980 | ], |
981 | 981 | "id": "yrUOA73E_nCr", |
982 | | - "execution_count": null, |
| 982 | + "execution_count": 33, |
983 | 983 | "outputs": [ |
984 | 984 | { |
985 | 985 | "output_type": "stream", |
| 986 | + "name": "stdout", |
986 | 987 | "text": [ |
987 | 988 | "[11, 17, 23]\n" |
988 | | - ], |
989 | | - "name": "stdout" |
| 989 | + ] |
990 | 990 | } |
991 | 991 | ] |
992 | 992 | }, |
|
1013 | 1013 | "This is where Python's built-in `zip()` function can become immensely useful. For our purposes, it will basically combine our arrays into a single matrix - forming an array that is the same length as `nums1` or `nums2` before; and which contains an array for all the elements found at that index in the aforementioned `nums1` and `nums2`. Observe how this works in the diagram below:\n", |
1014 | 1014 | "```\n", |
1015 | 1015 | "How zip(nums1, nums2) Works:\n", |
1016 | | - "index | nums1 | num2 | out \n", |
1017 | | - " 0 | 1 -------> 10 --------> (1, 10)\n", |
1018 | | - " 1 | 2 -------> 15 --------> (2, 15)\n", |
1019 | | - " 2 | 3 -------> 20 --------> (3, 20)\n", |
1020 | | - " | | | \n", |
| 1016 | + "index | nums1 | num2 | out \n", |
| 1017 | + " 0 | 1 -------> 10 -------> (1, 10)\n", |
| 1018 | + " 1 | 2 -------> 15 -------> (2, 15)\n", |
| 1019 | + " 2 | 3 -------> 20 -------> (3, 20)\n", |
| 1020 | + " | | |\n", |
1021 | 1021 | "```\n", |
1022 | 1022 | "\n", |
1023 | | - "Essentially, by using `zip()` we take out the need for tracking the `index` ourselves, and can thereby compute the element-wise sum using a more straightforward `for` loop, nested in a *list comprehension*." |
| 1023 | + "Essentially, by using `zip()` we take out the need for tracking the `index` ourselves, and can thereby compute the element-wise sum using a more straightforward `for` loop, nested in a *list comprehension*." |
1024 | 1024 | ], |
1025 | 1025 | "id": "tcQe2UmO9VCS" |
1026 | 1026 | }, |
|
1039 | 1039 | " return summed_elems" |
1040 | 1040 | ], |
1041 | 1041 | "id": "prompt-rescue", |
1042 | | - "execution_count": null, |
| 1042 | + "execution_count": 35, |
1043 | 1043 | "outputs": [] |
1044 | 1044 | }, |
1045 | 1045 | { |
|
1075 | 1075 | "print(element_wise_sum_2(nums1, nums2))" |
1076 | 1076 | ], |
1077 | 1077 | "id": "civil-accordance", |
1078 | | - "execution_count": null, |
| 1078 | + "execution_count": 36, |
1079 | 1079 | "outputs": [ |
1080 | 1080 | { |
1081 | 1081 | "output_type": "stream", |
| 1082 | + "name": "stdout", |
1082 | 1083 | "text": [ |
1083 | 1084 | "[11, 17, 23]\n" |
1084 | | - ], |
1085 | | - "name": "stdout" |
| 1085 | + ] |
1086 | 1086 | } |
1087 | 1087 | ] |
1088 | 1088 | }, |
|
1117 | 1117 | "id": "QC7SMlmn8huV" |
1118 | 1118 | }, |
1119 | 1119 | "source": [ |
1120 | | - "# A: first, import the function we can use to add 2 elems\n", |
| 1120 | + "# A: first, import (or define) a function we can use to add 2 elems\n", |
1121 | 1121 | "from operator import add\n", |
1122 | 1122 | "\n", |
1123 | 1123 | "# B: iterate over the two arrays in parallel, to compute the element-wise sum\n", |
1124 | 1124 | "element_wise_sum_3 = list(map(add, nums1, nums2))" |
1125 | 1125 | ], |
1126 | 1126 | "id": "QC7SMlmn8huV", |
1127 | | - "execution_count": null, |
| 1127 | + "execution_count": 37, |
1128 | 1128 | "outputs": [] |
1129 | 1129 | }, |
1130 | 1130 | { |
|
1150 | 1150 | "print(element_wise_sum_3)" |
1151 | 1151 | ], |
1152 | 1152 | "id": "PDvfPT1m9NDL", |
1153 | | - "execution_count": null, |
| 1153 | + "execution_count": 38, |
1154 | 1154 | "outputs": [ |
1155 | 1155 | { |
1156 | 1156 | "output_type": "stream", |
| 1157 | + "name": "stdout", |
1157 | 1158 | "text": [ |
1158 | 1159 | "[11, 17, 23]\n" |
1159 | | - ], |
1160 | | - "name": "stdout" |
| 1160 | + ] |
1161 | 1161 | } |
1162 | 1162 | ] |
1163 | 1163 | }, |
|
1171 | 1171 | "\n", |
1172 | 1172 | "You are given an array of numbers, `nums`. How would implement a function in Python to compute the square of each number in `nums`?\n", |
1173 | 1173 | "\n", |
1174 | | - "Please return the ouput in a new array." |
| 1174 | + "Please return the output in a new array." |
1175 | 1175 | ], |
1176 | 1176 | "id": "military-magnet" |
1177 | 1177 | }, |
|
1194 | 1194 | "nums = [1, 2, 3]" |
1195 | 1195 | ], |
1196 | 1196 | "id": "dJQkJkR7BmE8", |
1197 | | - "execution_count": null, |
| 1197 | + "execution_count": 39, |
1198 | 1198 | "outputs": [] |
1199 | 1199 | }, |
1200 | 1200 | { |
|
1217 | 1217 | " return [num**2 for num in nums]" |
1218 | 1218 | ], |
1219 | 1219 | "id": "potential-fields", |
1220 | | - "execution_count": null, |
| 1220 | + "execution_count": 40, |
1221 | 1221 | "outputs": [] |
1222 | 1222 | }, |
1223 | 1223 | { |
|
1243 | 1243 | "print(square_elements(nums))" |
1244 | 1244 | ], |
1245 | 1245 | "id": "theoretical-houston", |
1246 | | - "execution_count": null, |
| 1246 | + "execution_count": 41, |
1247 | 1247 | "outputs": [ |
1248 | 1248 | { |
1249 | 1249 | "output_type": "stream", |
| 1250 | + "name": "stdout", |
1250 | 1251 | "text": [ |
1251 | 1252 | "[1, 4, 9]\n" |
1252 | | - ], |
1253 | | - "name": "stdout" |
| 1253 | + ] |
1254 | 1254 | } |
1255 | 1255 | ] |
1256 | 1256 | }, |
| 1257 | + { |
| 1258 | + "source": [ |
| 1259 | + "**Note**: Alternative to the `**` operator, Python also has a few other options for calculating the power of a number:\n", |
| 1260 | + "\n", |
| 1261 | + "1. `pow(a, b)`: this is a built-in function that return the result of `a` raised to the exponent `b`.\n", |
| 1262 | + "2. `math.pow(a, b)`: similar to the `pow()` function, except that it returns a floating-point value rather than an integer.\n", |
| 1263 | + "\n", |
| 1264 | + "Regardless of which option you choose to go with, we still advise using a list comprehension to solve this problem." |
| 1265 | + ], |
| 1266 | + "cell_type": "markdown", |
| 1267 | + "metadata": {} |
| 1268 | + }, |
1257 | 1269 | { |
1258 | 1270 | "cell_type": "markdown", |
1259 | 1271 | "metadata": { |
|
1272 | 1284 | "\n", |
1273 | 1285 | "$E(X) = \\sum_{i=1}^{6}i * P(dice = i)$\n", |
1274 | 1286 | "\n", |
1275 | | - "Where:\n", |
1276 | | - "- $X$ = our random event (aka the *random variable*)\n", |
| 1287 | + "Here, the expected value is a *probability-weighted average*, which accounts for all the different values the die can land on. Here's what the variables mean:\n", |
| 1288 | + "- $X$ = our random event (aka the *random variable*), which is the dice roll\n", |
1277 | 1289 | "- $i$ = the value of the dice on a given roll\n", |
1278 | 1290 | "- $P(dice = i)$ = the probability of the die landing with that value on top\n", |
1279 | 1291 | "\n", |
|
1285 | 1297 | "2. Compute the probability that when the die is rolled, its face value is 2 $(P(dice = 2))$?\n", |
1286 | 1298 | "3. And so and so forth, for all the faces of the die?\n", |
1287 | 1299 | "\n", |
1288 | | - "7.2: Now, how would you compute $E(X)$ of the die, using the answer from the questions above?" |
| 1300 | + "7.2: Now, how would you compute $E(X)$ of the die, using your answers from 7.1?" |
1289 | 1301 | ], |
1290 | 1302 | "id": "descending-orbit" |
1291 | 1303 | }, |
|
1319 | 1331 | "source": [ |
1320 | 1332 | "# We can show that E(X) is the mean of the following dice random variable\n", |
1321 | 1333 | "import numpy as np\n", |
1322 | | - "# lets roll the dice 1000 times\n", |
| 1334 | + "# lets roll the dice 1,000 times\n", |
1323 | 1335 | "dice = np.random.randint(low=1.0, high=7.0, size=1000)\n", |
1324 | | - "print(dice)\n", |
1325 | | - "# Compute the mean of dice list\n", |
| 1336 | + "print(dice) # prints a list of 1,000 randomly generated integers\n", |
| 1337 | + "# Compute the mean of the dice values\n", |
1326 | 1338 | "print(np.mean(dice))\n", |
1327 | 1339 | "print(sum(dice)/len(dice))" |
1328 | 1340 | ], |
1329 | 1341 | "id": "headed-michael", |
1330 | 1342 | "execution_count": null, |
1331 | | - "outputs": [ |
1332 | | - { |
1333 | | - "output_type": "stream", |
1334 | | - "text": [ |
1335 | | - "[4 6 1 4 5 2 3 2 4 4 5 3 6 4 2 6 2 3 3 4 6 1 5 2 5 4 5 1 3 4 1 5 5 4 6 4 2\n", |
1336 | | - " 3 1 3 3 2 6 3 1 5 4 1 2 1 6 1 6 1 5 3 4 5 4 5 5 2 1 5 2 6 4 5 6 4 1 2 4 1\n", |
1337 | | - " 2 2 6 5 5 2 3 5 5 4 1 5 6 4 4 5 5 6 2 6 5 4 4 3 6 6 3 1 6 5 4 3 5 3 3 2 1\n", |
1338 | | - " 3 6 5 3 1 1 5 5 1 3 4 4 5 2 3 1 5 2 4 2 6 6 3 6 2 1 5 2 5 5 3 5 1 6 6 5 2\n", |
1339 | | - " 2 3 5 6 3 4 5 6 2 1 5 5 1 3 6 2 4 1 1 1 6 5 4 2 2 2 5 5 2 1 5 1 1 5 5 5 6\n", |
1340 | | - " 3 3 5 5 3 4 3 6 4 5 1 5 4 3 5 2 5 5 3 4 1 2 2 6 1 3 4 3 6 1 6 3 5 4 5 3 3\n", |
1341 | | - " 1 5 6 2 4 6 1 3 1 4 4 4 6 6 4 6 6 3 3 1 1 4 3 2 6 2 5 4 5 5 1 5 3 2 1 4 3\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3 4 1 2\n", |
1354 | | - " 2 4 6 2 3 5 5 6 2 2 3 6 1 3 3 1 5 2 3 1 5 3 4 2 6 2 2 3 6 6 4 3 6 1 4 4 1\n", |
1355 | | - " 5 4 2 2 6 4 4 1 3 6 5 5 4 5 4 1 6 5 6 6 4 1 6 5 6 2 5 5 6 5 3 6 2 6 3 3 4\n", |
1356 | | - " 5 3 2 4 6 4 2 2 2 4 3 2 2 1 1 3 5 5 2 5 5 5 2 2 2 5 2 6 6 1 6 6 5 3 3 5 2\n", |
1357 | | - " 1 1 3 3 4 1 1 3 4 6 4 3 4 4 4 3 4 4 6 2 2 5 6 6 1 5 5 5 3 1 2 2 4 3 6 4 4\n", |
1358 | | - " 1 1 1 6 2 3 5 6 3 1 6 4 2 5 6 2 2 1 2 3 2 6 1 3 1 3 4 5 6 6 1 3 1 6 4 1 5\n", |
1359 | | - " 5 4 6 4 3 4 1 2 5 6 1 2 4 4 4 1 2 4 1 2 2 6 4 2 2 2 5 6 3 1 1 2 4 4 6 6 1\n", |
1360 | | - " 2 4 2 4 3 5 5 5 1 5 4 3 1 3 1 3 1 4 6 2 4 4 3 1 6 4 3 1 4 3 2 4 6 2 4 3 4\n", |
1361 | | - " 5 6 6 3 5 4 6 3 6 2 2 2 5 2 3 2 4 1 1 1 3 3 3 3 5 2 4 6 1 3 5 1 6 4 6 3 1\n", |
1362 | | - " 4]\n", |
1363 | | - "3.536\n", |
1364 | | - "3.536\n" |
1365 | | - ], |
1366 | | - "name": "stdout" |
1367 | | - } |
1368 | | - ] |
| 1343 | + "outputs": [] |
1369 | 1344 | }, |
1370 | 1345 | { |
1371 | 1346 | "cell_type": "markdown", |
|
1456 | 1431 | "If everything is working correctly, then the `histogram` should show the following key-value pairs:\n", |
1457 | 1432 | "```\n", |
1458 | 1433 | "{\n", |
1459 | | - " 1: 2, \n", |
1460 | | - " 3: 5, # <--- 3 is the mode!\n", |
1461 | | - " 5: 2, \n", |
1462 | | - " 2: 1, \n", |
1463 | | - " 7: 3, \n", |
1464 | | - " 8: 1, \n", |
1465 | | - " 4: 2, \n", |
1466 | | - " 10: 2, \n", |
1467 | | - " 0: 3, \n", |
1468 | | - " 6: 1, \n", |
1469 | | - " 9: 1\n", |
| 1434 | + " 1 : 2, \n", |
| 1435 | + " 3 : 5, # <--- 3 is the mode!\n", |
| 1436 | + " 5 : 2, \n", |
| 1437 | + " 2 : 1, \n", |
| 1438 | + " 7 : 3, \n", |
| 1439 | + " 8 : 1, \n", |
| 1440 | + " 4 : 2, \n", |
| 1441 | + " 10 : 2, \n", |
| 1442 | + " 0 : 3, \n", |
| 1443 | + " 6 : 1, \n", |
| 1444 | + " 9 : 1\n", |
1470 | 1445 | "}\n", |
1471 | 1446 | "```" |
1472 | 1447 | ], |
|
1529 | 1504 | "source": [ |
1530 | 1505 | "from collections import Counter\n", |
1531 | 1506 | "\n", |
1532 | | - "\n", |
1533 | 1507 | "def compute_mode_2(dataset):\n", |
1534 | 1508 | " # A: compute the histogram\n", |
1535 | 1509 | " histogram = Counter(dataset)\n", |
|
0 commit comments