Skip to content

Commit 1bcad09

Browse files
committed
Add materials for Numpy Tutorial article.
1 parent 28bc775 commit 1bcad09

File tree

10 files changed

+140
-0
lines changed

10 files changed

+140
-0
lines changed

numpy-tutorial/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Code Materials for the Numpy Tutorial
2+
3+
These are code resources for the Numpy Tutorial article by @rpalo.

numpy-tutorial/bad-gray.jpg

135 KB
Loading

numpy-tutorial/blue.jpg

64.2 KB
Loading

numpy-tutorial/curve_grades.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Introduction example: curving a series of test grades using Numpy."""
2+
3+
import numpy as np
4+
5+
6+
CURVE_CENTER = 80
7+
8+
9+
def curve(grades):
10+
"""Adjusts an array of grades so that the average is roughly shifted
11+
to the specified curve center.
12+
13+
This will never cause a student's grade to decrease, and it will
14+
never cause the final grade to go over 100%.
15+
16+
Parameters:
17+
grades (np.ndarray): The individual student grades, between 0
18+
and 100.
19+
20+
Returns:
21+
(np.ndarray): A new array of grades, adjusted upwards, but in
22+
the same order.
23+
"""
24+
average = grades.mean()
25+
change = CURVE_CENTER - average
26+
new_grades = grades + change
27+
return np.clip(new_grades, grades, 100)
28+
29+
30+
if __name__ == "__main__":
31+
grades = np.array([72, 35, 64, 88, 51, 90, 74, 12])
32+
print(curve(grades))
33+
# => array([91.25, 54.25, 83.25, 100.0, 70.25, 100.0, 93.25, 31.25])

numpy-tutorial/durer.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Validating Dürer's magic square."""
2+
3+
import numpy as np
4+
5+
square = np.array(
6+
[
7+
[16, 3, 2, 13],
8+
[5, 10, 11, 8],
9+
[9, 6, 7, 12],
10+
[4, 15, 14, 1],
11+
]
12+
)
13+
14+
for i in range(4):
15+
assert square[i, :].sum() == 34
16+
assert square[:, i].sum() == 34
17+
18+
assert square[:2, :2].sum() == 34
19+
assert square[2:, :2].sum() == 34
20+
assert square[:2, 2:].sum() == 34
21+
assert square[2:, 2:].sum() == 34
22+
23+
print("All assertions passed!")

numpy-tutorial/good-gray.jpg

133 KB
Loading

numpy-tutorial/image_mod.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
import matplotlib.image as mpimg
3+
4+
img = mpimg.imread("kitten.png")
5+
print(type(img))
6+
print(img.shape)
7+
8+
# => numpy.ndarray
9+
# => (866, 1280, 3)
10+
11+
output = img.copy() # The original image is read-only!
12+
output[:, :, :2] = 0 # Drop out Red and Green channels
13+
mpimg.imsave("blue.jpg", output)
14+
15+
averages = img.mean(axis=2) # Take the average of each R, G, and B
16+
mpimg.imsave("bad-gray.jpg", averages, cmap="gray")
17+
18+
weights = np.array([0.3, 0.59, 0.11])
19+
greyscale = np.dot(img, weights)
20+
mpimg.imsave("good-gray.jpg", greyscale, cmap="gray")

numpy-tutorial/kitty.jpg

289 KB
Loading

numpy-tutorial/maclauren.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Estimates e^x^ by using an approximated Maclauren series."""
2+
3+
from math import e, factorial
4+
5+
import numpy as np
6+
7+
fac = np.vectorize(factorial)
8+
9+
10+
def e_x(x, terms=10):
11+
"""Approximates e^x^ using 'terms' terms of the Maclauren Series"""
12+
n = np.arange(terms)
13+
return np.sum((x ** n) / fac(n))
14+
15+
16+
if __name__ == "__main__":
17+
print("Actual:", e ** 3)
18+
19+
print("N (terms)\tMaclauren\tError")
20+
21+
for n in range(1, 14):
22+
maclauren = e_x(3, terms=n)
23+
print(f"{n}\t\t{maclauren:.03f}\t\t{e**3 - maclauren:.03f}")
24+
25+
26+
# Actual: 20.085536923187664
27+
# N (terms) Maclauren Error
28+
# 1 1.000 19.086
29+
# 2 4.000 16.086
30+
# 3 8.500 11.586
31+
# 4 13.000 7.086
32+
# 5 16.375 3.711
33+
# 6 18.400 1.686
34+
# 7 19.412 0.673
35+
# 8 19.846 0.239
36+
# 9 20.009 0.076
37+
# 10 20.063 0.022
38+
# 11 20.080 0.006
39+
# 12 20.084 0.001
40+
# 13 20.085 0.000

numpy-tutorial/normal.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Validating the Normal Distribution."""
2+
3+
from numpy.random import default_rng
4+
5+
6+
rng = default_rng()
7+
values = rng.standard_normal(10000)
8+
9+
print(values[:5])
10+
# => array([.9779210858, 1.8361585253, -.3641365235, -.1311344527,
11+
# 1.286542056 ])
12+
13+
std = values.std()
14+
filtered = values[(values > -2 * std) & (values < 2 * std)]
15+
16+
print(filtered.size)
17+
# => 9565
18+
print(values.size)
19+
# => 10000
20+
print(filtered.size / values.size)
21+
# => 0.9565 (actual is .9545)

0 commit comments

Comments
 (0)