Skip to content

Commit 9d4112a

Browse files
committed
Replace all files with notebooks, update README, add requirements.
1 parent f413136 commit 9d4112a

File tree

12 files changed

+804
-139
lines changed

12 files changed

+804
-139
lines changed

numpy-tutorial/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
# Code Materials for the Numpy Tutorial
1+
# Numpy Tutorial: First Steps in Data Science
22

3-
These are code resources for the Numpy Tutorial article by @rpalo.
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 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/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/curve_grades.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

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/durer.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)