Skip to content

Commit

Permalink
Added solutions to notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
calvpang committed Jul 4, 2024
1 parent 364efa8 commit d37f570
Show file tree
Hide file tree
Showing 8 changed files with 941 additions and 360 deletions.
340 changes: 278 additions & 62 deletions notebooks/1_Python_Fundamentals.ipynb

Large diffs are not rendered by default.

75 changes: 49 additions & 26 deletions notebooks/2_Analysing_Patient_Data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The expression `numpy.loadtxt(...)` is a function call that asks Python to run the function `loadtxt` which belongs to the `numpy` library. The dot notation in Python is used most of all as an object attribute/property specifier or for invoking its method. \n",
"The expression `np.loadtxt(...)` is a function call that asks Python to run the function `loadtxt` which belongs to the `numpy` library. The dot notation in Python is used most of all as an object attribute/property specifier or for invoking its method. \n",
"- `object.property` will give you the object.property value.\n",
"- `object_name.method()` will invoke on object_name method.\n",
"\n",
Expand All @@ -86,7 +86,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"`numpy.loadtxt` has **two parameters**: *the name of the file* we want to read and *the delimiter that separates values on a line*. These both need to be character strings (or strings for short), so we put them in quotes.\n",
"`np.loadtxt` has **two parameters**: *the name of the file* we want to read and *the delimiter that separates values on a line*. These both need to be character strings (or strings for short), so we put them in quotes.\n",
"\n",
"Since we haven’t told it to do anything else with the function’s output, the notebook displays it. In this case, that output is the data we just loaded. \n",
"\n",
Expand All @@ -97,9 +97,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Our call to `numpy.loadtxt` read our file but didn’t save the data in memory. To do that, we need to assign the array to a variable. In a similar manner to how we assign a single value to a variable, we can also assign an array of values to a variable using the same syntax. \n",
"Our call to `np.loadtxt` read our file but didn’t save the data in memory. To do that, we need to assign the array to a variable. In a similar manner to how we assign a single value to a variable, we can also assign an array of values to a variable using the same syntax. \n",
"\n",
"Let’s re-run `numpy.loadtxt` and save the returned data:"
"Let’s re-run `np.loadtxt` and save the returned data:"
]
},
{
Expand Down Expand Up @@ -222,7 +222,7 @@
"outputs": [],
"source": [
"# Print the first value in the variable data\n",
"print(\"first value in data:\", data[0, 0])"
"print(f\"first value in data: {data[0, 0]}\")"
]
},
{
Expand All @@ -232,7 +232,7 @@
"outputs": [],
"source": [
"# Print the middle value in the variable data\n",
"print(\"middle value in data:\", data[29, 19])"
"print(f\"middle value in data: {data[29, 19]}\")"
]
},
{
Expand Down Expand Up @@ -277,7 +277,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Print the first 3 rows and 9 columns of the variable data\n",
"# Print the first 3 rows and 10 columns of the variable data\n",
"print(data[0:4, 0:10])"
]
},
Expand All @@ -287,6 +287,8 @@
"source": [
"The slice `0:4` means, \"Start at index 0 and go up to, but not including, index 4\".\n",
"\n",
"Meanwhile, the slice `0:10` means, \"Start at the index 0 and go up to, but not including, index 10\".\n",
"\n",
"Again, the up-to-but-not-including takes a bit of getting used to, but the rule is that the difference between the upper and lower bounds is the number of values in the slice.\n",
"\n",
"We also don't have to start slices at 0:"
Expand Down Expand Up @@ -319,7 +321,7 @@
"source": [
"# Slice the first 2 rows and all columns starting from the 36th column\n",
"small = data[:3, 36:]\n",
"print(\"small is:\\n\", small)"
"print(f\"small is:\\n {small}\")"
]
},
{
Expand All @@ -330,7 +332,9 @@
"\n",
"## Analysing the Data\n",
"\n",
"NumPy has several useful functions that take an array as input to perform operations on its values. If we want to find the average inflammation for all patients on all days, for example, we can ask NumPy to compute `data`'s mean value:"
"NumPy has several useful functions that take an array as input to perform operations on its values. \n",
"\n",
"If we want to find the average inflammation for all patients on all days, for example, we can ask NumPy to compute `data`'s mean value:"
]
},
{
Expand Down Expand Up @@ -444,7 +448,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Get the average inflammation per day across all patients\n",
"# Get the average inflammation per day\n",
"print(np.mean(data, axis=0))"
]
},
Expand All @@ -461,7 +465,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Get the shape of the average inflammation per day across all patients\n",
"# Get the shape of the average inflammation per day\n",
"print(np.mean(data, axis=0).shape)"
]
},
Expand All @@ -480,6 +484,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Get the average inflammation per patient\n",
"print(np.mean(data, axis=1))"
]
},
Expand Down Expand Up @@ -529,7 +534,8 @@
"metadata": {},
"outputs": [],
"source": [
"# 1a."
"# 1a.\n",
"element[:4]"
]
},
{
Expand All @@ -538,7 +544,8 @@
"metadata": {},
"outputs": [],
"source": [
"# 1b."
"# 1b.\n",
"element[4:]"
]
},
{
Expand All @@ -547,7 +554,8 @@
"metadata": {},
"outputs": [],
"source": [
"# 1c."
"# 1c.\n",
"element[:]"
]
},
{
Expand All @@ -556,7 +564,8 @@
"metadata": {},
"outputs": [],
"source": [
"# 2a."
"# 2a.\n",
"element[-1]"
]
},
{
Expand All @@ -565,7 +574,8 @@
"metadata": {},
"outputs": [],
"source": [
"# 2b."
"# 2b.\n",
"element[-2]"
]
},
{
Expand All @@ -590,7 +600,9 @@
"metadata": {},
"outputs": [],
"source": [
"# 4."
"# 4.\n",
"element = \"carpentry\"\n",
"element[-3:]"
]
},
{
Expand All @@ -606,7 +618,8 @@
"metadata": {},
"outputs": [],
"source": [
"# 5a."
"# 5a.\n",
"data[3:3, 4:4]"
]
},
{
Expand All @@ -615,7 +628,8 @@
"metadata": {},
"outputs": [],
"source": [
"# 5b.\n"
"# 5b.\n",
"data[3:3, :]\n"
]
},
{
Expand Down Expand Up @@ -659,7 +673,12 @@
"metadata": {},
"outputs": [],
"source": [
"# 1."
"# 1.\n",
"A1 = A[:, :1]\n",
"A3 = A[:, -1:]\n",
"\n",
"D = np.hstack([A1, A3])\n",
"print(D)"
]
},
{
Expand Down Expand Up @@ -726,7 +745,8 @@
"metadata": {},
"outputs": [],
"source": [
"# 1."
"# 1.\n",
"np.diff(data, axis=1)"
]
},
{
Expand All @@ -742,14 +762,15 @@
"metadata": {},
"outputs": [],
"source": [
"# 2."
"# 2.\n",
"np.diff(data, axis=1).shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How would you find the largest change in inflammatioon for each patient? Does it matter if the change in inflammation is an increase or decrease?"
"How would you find the largest change in inflammation for each patient? Does it matter if the change in inflammation is an increase or decrease?"
]
},
{
Expand All @@ -758,7 +779,8 @@
"metadata": {},
"outputs": [],
"source": [
"# 3a."
"# 3a.\n",
"np.amax(np.diff(data, axis=1), axis=1)"
]
},
{
Expand All @@ -767,7 +789,8 @@
"metadata": {},
"outputs": [],
"source": [
"# 3b."
"# 3b.\n",
"np.amax(np.absolute(np.diff(data, axis=1)), axis=1)"
]
},
{
Expand Down Expand Up @@ -805,7 +828,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.1.-1"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down
75 changes: 70 additions & 5 deletions notebooks/3_Visualising_Tabular_Data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,29 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"fig = plt.figure(figsize=(10.0, 3.0))\n",
"\n",
"axes1 = fig.add_subplot(1, 3, 1)\n",
"axes2 = fig.add_subplot(1, 3, 2)\n",
"axes3 = fig.add_subplot(1, 3, 3)\n",
"\n",
"axes1.set_ylabel(\"average\")\n",
"axes1.plot(np.mean(data, axis=0))\n",
"axes1.set_ylim(0, 15)\n",
"\n",
"axes2.set_ylabel(\"max\")\n",
"axes2.plot(np.amax(data, axis=0))\n",
"axes2.set_ylim(0, 25)\n",
"\n",
"axes3.set_ylabel(\"min\")\n",
"axes3.plot(np.amin(data, axis=0))\n",
"axes3.set_ylim(0, 6)\n",
"\n",
"fig.tight_layout()\n",
"plt.savefig(\"../output/inflammation-01.png\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
Expand All @@ -258,7 +280,26 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"fig = plt.figure(figsize=(10.0, 3.0))\n",
"\n",
"axes1 = fig.add_subplot(1, 3, 1)\n",
"axes2 = fig.add_subplot(1, 3, 2)\n",
"axes3 = fig.add_subplot(1, 3, 3)\n",
"\n",
"axes1.set_ylabel('average')\n",
"axes1.plot(np.mean(data, axis=0), drawstyle='steps-mid')\n",
"\n",
"axes2.set_ylabel('max')\n",
"axes2.plot(np.amax(data, axis=0), drawstyle='steps-mid')\n",
"\n",
"axes3.set_ylabel('min')\n",
"axes3.plot(np.amin(data, axis=0), drawstyle='steps-mid')\n",
"\n",
"fig.tight_layout()\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
Expand All @@ -273,7 +314,10 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"std_plot = plt.plot(np.std(data, axis=0))\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
Expand All @@ -288,7 +332,28 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"# change figsize (swap width and height)\n",
"fig = plt.figure(figsize=(3.0, 10.0))\n",
"\n",
"# change add_subplot (swap first two parameters)\n",
"axes1 = fig.add_subplot(3, 1, 1)\n",
"axes2 = fig.add_subplot(3, 1, 2)\n",
"axes3 = fig.add_subplot(3, 1, 3)\n",
"\n",
"axes1.set_ylabel('average')\n",
"axes1.plot(np.mean(data, axis=0))\n",
"\n",
"axes2.set_ylabel('max')\n",
"axes2.plot(np.amax(data, axis=0))\n",
"\n",
"axes3.set_ylabel('min')\n",
"axes3.plot(np.amin(data, axis=0))\n",
"\n",
"fig.tight_layout()\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -317,7 +382,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit d37f570

Please sign in to comment.