Skip to content

Commit 2da0a84

Browse files
authored
Merge pull request #158 from rpalo/numpy-tutorial
Add materials for Numpy Tutorial article.
2 parents 3ba922f + 308c93b commit 2da0a84

File tree

11 files changed

+805
-0
lines changed

11 files changed

+805
-0
lines changed

numpy-tutorial/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Numpy Tutorial: First Steps in Data Science
2+
3+
This folder contains the sample code for the [NumPy Tutorial](https://realpython.com/numpy-tutorial/) by @rpalo.
4+
5+
## Installing
6+
7+
First, you will need to have the dependencies installed:
8+
9+
```shell
10+
$ python -m pip install -r requirements.txt
11+
```
12+
13+
## Usage
14+
15+
These examples all make use of the [Jupyter Notebook](https://jupyter-notebook.readthedocs.io/en/stable/). To run the examples, make sure the correct virtual environment is active (you may have to deactivate and reactivate after installing requirements), and run:
16+
17+
```shell
18+
$ jupyter notebook
19+
```
20+
21+
You should see listings for each of the example files. You can open them up and run them cell by cell to see how they perform. Feel free to make small changes to the code to see how those affect things.

numpy-tutorial/bad-gray.jpg

274 KB
Loading

numpy-tutorial/blue.jpg

123 KB
Loading

numpy-tutorial/curve_grades.ipynb

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Curving Grades\n",
8+
"\n",
9+
"You'll be playing the part of a teacher, curving test grades to account for an unfortunately difficult test. The curving should improve the student's grade relative to where they ended up with respect to the group's average. It should also *not* hurt the student by reducing their grade, and it shouldn't give anyone anything over 100%."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 1,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import numpy as np"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"CURVE_CENTER = 80"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 3,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"def curve(grades):\n",
37+
" \"\"\"Adjusts an array of grades so that the average is roughly shifted\n",
38+
" to the specified curve center.\n",
39+
"\n",
40+
" This will never cause a student's grade to decrease, and it will\n",
41+
" never cause the final grade to go over 100%.\n",
42+
"\n",
43+
" Parameters:\n",
44+
" grades (np.ndarray): The individual student grades, between 0\n",
45+
" and 100.\n",
46+
"\n",
47+
" Returns:\n",
48+
" (np.ndarray): A new array of grades, adjusted upwards, but in\n",
49+
" the same order.\n",
50+
" \"\"\"\n",
51+
" average = grades.mean()\n",
52+
" change = CURVE_CENTER - average\n",
53+
" new_grades = grades + change\n",
54+
" return np.clip(new_grades, grades, 100)"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 4,
60+
"metadata": {},
61+
"outputs": [
62+
{
63+
"data": {
64+
"text/plain": [
65+
"array([[ 91.25, 54.25, 83.25, 100. , 70.25, 100. , 93.25, 31.25]])"
66+
]
67+
},
68+
"execution_count": 4,
69+
"metadata": {},
70+
"output_type": "execute_result"
71+
}
72+
],
73+
"source": [
74+
"grades = np.array([[72, 35, 64, 88, 51, 90, 74, 12]])\n",
75+
"curve(grades)"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"Note how none of the grades are decreased or greater than 100%!"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": []
91+
}
92+
],
93+
"metadata": {
94+
"kernelspec": {
95+
"display_name": "Python 3",
96+
"language": "python",
97+
"name": "python3"
98+
},
99+
"language_info": {
100+
"codemirror_mode": {
101+
"name": "ipython",
102+
"version": 3
103+
},
104+
"file_extension": ".py",
105+
"mimetype": "text/x-python",
106+
"name": "python",
107+
"nbconvert_exporter": "python",
108+
"pygments_lexer": "ipython3",
109+
"version": "3.8.5"
110+
}
111+
},
112+
"nbformat": 4,
113+
"nbformat_minor": 4
114+
}

numpy-tutorial/durer.ipynb

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Validating Dürer's Magic Square\n",
8+
"\n",
9+
"Albrecht Dürer was an artist and mathematician who created an engraving of a [magic square](https://mathworld.wolfram.com/DuerersMagicSquare.html), where lots of bits all add up to the same thing. Each row, column, quadrant, corners, etc. all add up to 34.\n",
10+
"\n",
11+
"At the bottom, you see 15 and 14, signifying the year it was engraved (1514) as well as 4 and 1, standing for D and A, the creator's initials.\n",
12+
"\n",
13+
"You're going to validate that everything does, in fact, add up."
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 1,
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"import numpy as np"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 2,
28+
"metadata": {},
29+
"outputs": [
30+
{
31+
"data": {
32+
"text/plain": [
33+
"array([[16, 3, 2, 13],\n",
34+
" [ 5, 10, 11, 8],\n",
35+
" [ 9, 6, 7, 12],\n",
36+
" [ 4, 15, 14, 1]])"
37+
]
38+
},
39+
"execution_count": 2,
40+
"metadata": {},
41+
"output_type": "execute_result"
42+
}
43+
],
44+
"source": [
45+
"square = np.array([\n",
46+
" [16, 3, 2, 13],\n",
47+
" [5, 10, 11, 8],\n",
48+
" [9, 6, 7, 12],\n",
49+
" [4, 15, 14, 1],\n",
50+
"])\n",
51+
"square"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 3,
57+
"metadata": {},
58+
"outputs": [
59+
{
60+
"name": "stdout",
61+
"output_type": "stream",
62+
"text": [
63+
"All assertions passed!\n"
64+
]
65+
}
66+
],
67+
"source": [
68+
"for i in range(4):\n",
69+
" assert square[i, :].sum() == 34\n",
70+
" assert square[:, i].sum() == 34\n",
71+
" \n",
72+
"assert square[:2, :2].sum() == 34\n",
73+
"assert square[2:, :2].sum() == 34\n",
74+
"assert square[:2, 2:].sum() == 34\n",
75+
"assert square[2:, 2:].sum() == 34\n",
76+
"\n",
77+
"print(\"All assertions passed!\")"
78+
]
79+
}
80+
],
81+
"metadata": {
82+
"kernelspec": {
83+
"display_name": "Python 3",
84+
"language": "python",
85+
"name": "python3"
86+
},
87+
"language_info": {
88+
"codemirror_mode": {
89+
"name": "ipython",
90+
"version": 3
91+
},
92+
"file_extension": ".py",
93+
"mimetype": "text/x-python",
94+
"name": "python",
95+
"nbconvert_exporter": "python",
96+
"pygments_lexer": "ipython3",
97+
"version": "3.8.5"
98+
}
99+
},
100+
"nbformat": 4,
101+
"nbformat_minor": 4
102+
}

numpy-tutorial/good-gray.jpg

272 KB
Loading

numpy-tutorial/image_mod.ipynb

Lines changed: 247 additions & 0 deletions
Large diffs are not rendered by default.

numpy-tutorial/kitty.jpg

641 KB
Loading

numpy-tutorial/maclaurin.ipynb

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Using a Maclaurin Series to Estimate $e$\n",
8+
"\n",
9+
"A [Maclaurin series](https://mathworld.wolfram.com/MaclaurinSeries.html) is an infinite series of terms that can be used to approximate more complex functions quickly.\n",
10+
"\n",
11+
"You're going to approximate $e^x$ by using the first few terms of the series. The series equation for $e^x$ is this:\n",
12+
"\n",
13+
"$$\\sum_{n=0}^{\\infty} \\frac{x^n}{n!} = 1 + x + \\frac{x^2}{2} + \\frac{x^3}{6} \\ldots$$"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 3,
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"import numpy as np\n",
23+
"from math import e, factorial"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 4,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"fac = np.vectorize(factorial)"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 5,
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"def e_x(x, terms=10):\n",
42+
" \"\"\"Approximates $e^x$ using a given number of terms of the Maclaurin series\"\"\"\n",
43+
" n = np.arange(terms)\n",
44+
" return np.sum((x ** n) / fac(n))"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 6,
50+
"metadata": {},
51+
"outputs": [
52+
{
53+
"data": {
54+
"text/plain": [
55+
"20.085536923187664"
56+
]
57+
},
58+
"execution_count": 6,
59+
"metadata": {},
60+
"output_type": "execute_result"
61+
}
62+
],
63+
"source": [
64+
"# Actual:\n",
65+
"e ** 3"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 7,
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"name": "stdout",
75+
"output_type": "stream",
76+
"text": [
77+
"N (terms)\tMaclaurin\tError\n",
78+
"1\t\t1.000\t\t19.086\n",
79+
"2\t\t4.000\t\t16.086\n",
80+
"3\t\t8.500\t\t11.586\n",
81+
"4\t\t13.000\t\t7.086\n",
82+
"5\t\t16.375\t\t3.711\n",
83+
"6\t\t18.400\t\t1.686\n",
84+
"7\t\t19.412\t\t0.673\n",
85+
"8\t\t19.846\t\t0.239\n",
86+
"9\t\t20.009\t\t0.076\n",
87+
"10\t\t20.063\t\t0.022\n",
88+
"11\t\t20.080\t\t0.006\n",
89+
"12\t\t20.084\t\t0.001\n",
90+
"13\t\t20.085\t\t0.000\n"
91+
]
92+
}
93+
],
94+
"source": [
95+
"print(\"N (terms)\\tMaclaurin\\tError\")\n",
96+
"\n",
97+
"for n in range(1, 14):\n",
98+
" maclaurin = e_x(3, terms=n)\n",
99+
" print(f\"{n}\\t\\t{maclaurin:.03f}\\t\\t{e**3 - maclaurin:.03f}\")"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": null,
105+
"metadata": {},
106+
"outputs": [],
107+
"source": []
108+
}
109+
],
110+
"metadata": {
111+
"kernelspec": {
112+
"display_name": "Python 3",
113+
"language": "python",
114+
"name": "python3"
115+
},
116+
"language_info": {
117+
"codemirror_mode": {
118+
"name": "ipython",
119+
"version": 3
120+
},
121+
"file_extension": ".py",
122+
"mimetype": "text/x-python",
123+
"name": "python",
124+
"nbconvert_exporter": "python",
125+
"pygments_lexer": "ipython3",
126+
"version": "3.8.5"
127+
}
128+
},
129+
"nbformat": 4,
130+
"nbformat_minor": 4
131+
}

0 commit comments

Comments
 (0)