Skip to content

Commit f29e6d0

Browse files
authored
Merge branch 'master' into python-class
2 parents 4eb753f + 912efcb commit f29e6d0

File tree

8 files changed

+155
-1
lines changed

8 files changed

+155
-1
lines changed

build-a-rest-api-frontend/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Build a Front End for a REST API
22

3-
This repository contains code related to the Real Python tutorial on [building a front end for a REST API](https://realpython.com/build-a-rest-api-frontend/).
3+
This repository contains code related to the Real Python tutorial on [building a front end for a REST API](https://realpython.com/flask-javascript-frontend-for-rest-api/).
44

55
## Setup
66

numpy-random-normal/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# How to Get Normally Distributed Random Numbers With NumPy
2+
3+
This repository holds the code for the Real Python [How to Get Normally Distributed Random Numbers With NumPy](https://realpython.com/numpy-random-normal/) tutorial.
4+
5+
## Dependencies
6+
7+
The scripts use NumPy, Matplotlib, and SciPy. You should first create a virtual environment:
8+
9+
```console
10+
$ python -m venv venv
11+
$ source venv/bin/activate
12+
```
13+
14+
You can then install the necessary packages with `pip`:
15+
16+
```console
17+
(venv) $ python -m pip install numpy matplotlib scipy
18+
```
19+
20+
Alternatively, you can install from [`requirements.txt`](requirements.txt) in order to get the same versions of dependencies that were used when developing and testing the code:
21+
22+
```console
23+
(venv) $ python -m pip install -r requirements.txt
24+
```
25+
26+
Use only one of the `pip install` commands.
27+
28+
## Run the Code
29+
30+
There are four scripts in this repository. The first three—[`normal_distribution.py`](normal_distribution.py), [`weights.py`](weights.py), and [`dice.py`](dice.py)—show code discussed in the tutorial. The final script, [`plot_normal_dist.py`](plot_normal_dist.py), contains the code used to generate the first figure in the tutorial.
31+
32+
Each script can be run with `python` inside your virtual environment:
33+
34+
```console
35+
(venv) $ python normal_distribution.py
36+
```
37+
38+
You can run the other scripts with a similar command.
39+
40+
## Author
41+
42+
- **Geir Arne Hjelle**, E-mail: [[email protected]]([email protected])
43+
44+
## License
45+
46+
Distributed under the MIT license. See [`LICENSE`](../LICENSE) for more information.

numpy-random-normal/dice.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
rng = np.random.default_rng(seed=2310)
5+
6+
rolls = rng.integers(low=1, high=6, endpoint=True, size=(10_000, 2))
7+
plt.hist(rolls.mean(axis=1), bins=21)
8+
plt.show()
9+
10+
rolls = rng.integers(low=1, high=6, endpoint=True, size=(10_000, 10))
11+
plt.hist(rolls.mean(axis=1), bins=41)
12+
plt.show()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
import scipy.stats
4+
5+
rng = np.random.default_rng(seed=2310)
6+
numbers = rng.normal(size=10_000)
7+
8+
print(f"Mean of numbers: {numbers.mean():.3f}")
9+
print(f"Standard deviation of numbers: {numbers.std():.3f}")
10+
11+
bins = 100
12+
bin_width = (numbers.max() - numbers.min()) / bins
13+
hist_area = len(numbers) * bin_width
14+
15+
plt.hist(numbers, bins=bins)
16+
plt.show()
17+
18+
x = np.linspace(-4, 4, 101)
19+
plt.plot(x, scipy.stats.norm.pdf(x))
20+
plt.show()
21+
22+
x = np.linspace(numbers.min(), numbers.max(), 101)
23+
plt.hist(numbers, bins=bins)
24+
plt.plot(x, scipy.stats.norm.pdf(x) * hist_area)
25+
plt.show()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
import scipy.stats
4+
5+
x = np.linspace(-4, 4, 201)
6+
y = scipy.stats.norm.pdf(x)
7+
8+
fig, ax = plt.subplots()
9+
10+
ax.fill_between(x, y, 0, alpha=0.2, color="green")
11+
ax.fill_between(x[75:126], y[75:126], 0, alpha=1, color="green")
12+
ax.plot(x, y, "k")
13+
ax.plot([-1, -1], [0, y[75]], "k--")
14+
ax.plot([1, 1], [0, y[125]], "k--")
15+
ax.annotate(
16+
"68%",
17+
xy=(0.1, 0.25),
18+
xytext=(2, 0.3),
19+
arrowprops=dict(facecolor="black", shrink=0.005),
20+
)
21+
ax.set_yticks([])
22+
23+
plt.show()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
numpy
2+
matplotlib
3+
scipy
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# This file is autogenerated by pip-compile with python 3.11
3+
# To update, run:
4+
#
5+
# pip-compile requirements.in
6+
#
7+
contourpy==1.0.7
8+
# via matplotlib
9+
cycler==0.11.0
10+
# via matplotlib
11+
fonttools==4.39.3
12+
# via matplotlib
13+
kiwisolver==1.4.4
14+
# via matplotlib
15+
matplotlib==3.7.1
16+
# via -r requirements.in
17+
numpy==1.24.2
18+
# via
19+
# -r requirements.in
20+
# contourpy
21+
# matplotlib
22+
# scipy
23+
packaging==23.0
24+
# via matplotlib
25+
pillow==9.5.0
26+
# via matplotlib
27+
pyparsing==3.0.9
28+
# via matplotlib
29+
python-dateutil==2.8.2
30+
# via matplotlib
31+
scipy==1.10.1
32+
# via -r requirements.in
33+
six==1.16.0
34+
# via python-dateutil

numpy-random-normal/weights.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
rng = np.random.default_rng(seed=2310)
5+
weights = rng.normal(1023, 19, size=5_000)
6+
7+
print(f"Mean of weights: {weights.mean():.2f}g")
8+
print(f"Standard deviation of weights: {weights.std():.2f}g")
9+
10+
plt.hist(weights, bins=50)
11+
plt.show()

0 commit comments

Comments
 (0)