Skip to content

Commit

Permalink
Merge pull request #146 from alan-turing-institute/module03-updates
Browse files Browse the repository at this point in the history
Updates to module 03
  • Loading branch information
jemrobinson authored Nov 2, 2022
2 parents e6cfa8f + 7e00af0 commit fe5519d
Show file tree
Hide file tree
Showing 6 changed files with 843 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can plot all the data in the dataframe seperately, but that isn't always useful!"
"We can plot all the data in the dataframe separately, but that isn't always useful!"
]
},
{
Expand All @@ -948,21 +948,39 @@
}
],
"source": [
"df.plot(subplots=True);"
"df.plot(subplots=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And can produce more meaningful and useful visualisations either with our generated lists or by accessing the dataframe directly"
"Let's produce some more meaningful and useful visualisations by accessing the dataframe directly.\n",
"\n",
"We start by discarding any rows with an invalid (negative) standard deviation."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"df = df[df[\"deviation\"] > 0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we use the dataframe to construct some useful lists."
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"deviation = df[\"deviation\"].tolist() # Get the dataframe column (series) as a list\n",
"observations = df[\"observations\"].tolist()\n",
Expand All @@ -972,7 +990,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 34,
"metadata": {},
"outputs": [
{
Expand All @@ -981,7 +999,7 @@
"Text(0.5, 1.0, 'From List')"
]
},
"execution_count": 33,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
},
Expand Down
75 changes: 46 additions & 29 deletions module03_research_data_in_python/03_05_advanced_numpy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,29 @@
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[False, True, False, False],\n",
" [False, False, False, False],\n",
" [False, False, False, True]])"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_is_one = y == 1\n",
"y_is_one"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
Expand All @@ -1305,14 +1328,14 @@
" [False, False, True, False]])"
]
},
"execution_count": 50,
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iszero = x == y\n",
"iszero"
"aresame = x == y\n",
"aresame"
]
},
{
Expand All @@ -1324,7 +1347,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 52,
"metadata": {},
"outputs": [
{
Expand All @@ -1333,13 +1356,13 @@
"array([ 2, 1, -1, -2, -1, 1])"
]
},
"execution_count": 51,
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y[np.logical_not(iszero)]"
"y[np.logical_not(aresame)]"
]
},
{
Expand All @@ -1351,16 +1374,16 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"y[iszero] = 5"
"y[aresame] = 5"
]
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 54,
"metadata": {},
"outputs": [
{
Expand All @@ -1371,7 +1394,7 @@
" [-2, -1, 5, 1]])"
]
},
"execution_count": 53,
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1391,31 +1414,32 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy memory management can be tricksy:"
"NumPy manages memory differently from lists.\n",
"Changing an element in a copy of a list does not change the original list."
]
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"x = np.arange(5)\n",
"x = list(range(5))\n",
"y = x[:]"
]
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 0, 3, 4])"
"[0, 1, 2, 3, 4]"
]
},
"execution_count": 55,
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1429,31 +1453,31 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"It does **not** behave like lists!"
"But in NumPy, changing the copy **does** change the original array!"
]
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"x = list(range(5))\n",
"x = np.arange(5)\n",
"y = x[:]"
]
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4]"
"array([0, 1, 0, 3, 4])"
]
},
"execution_count": 57,
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1470,13 +1494,6 @@
"We must use `np.copy` to force separate memory.\n",
"Otherwise NumPy tries its hardest to make slices be *views* on data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, this has all been very theoretical, but let's go through a practical example, and see how powerful NumPy can be."
]
}
],
"metadata": {
Expand Down
2 changes: 2 additions & 0 deletions module03_research_data_in_python/03_06_boids.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"source": [
"⚠️ Warning: Advanced Topic! ⚠️\n",
"\n",
"Our earlier discussion of NumPy was very theoretical, but let's go through a practical example, and see how powerful NumPy can be.\n",
"\n",
"*Note this is more a showcase of what you can do with numpy than an exhaustive notebook to work through*"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
"\n",
"The first plot should show sin(x) and cos(x) for the range of x between -1 pi and +1 pi.\n",
"\n",
"**Hint:** The `range(start, stop, step)` function only works with integers. Use the `arange` function from `numpy` instead: `np.arange(start, stop, step)`.\n",
"\n",
"The second plot should show sin(x), cos(x) and the sum of sin(x) and cos(x) over the same -pi to +pi range.\n",
"Set suitable limits on the axes and pick colours, markers, or line-styles that will make it easy to differentiate between the curves.\n",
"Add legends to both axes."
Expand Down Expand Up @@ -190,7 +192,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
"version": "3.8.12"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit fe5519d

Please sign in to comment.