Skip to content
Snippets Groups Projects
Commit 57387511 authored by Ulrich Kerzel's avatar Ulrich Kerzel
Browse files

check more notebooks for new versions

parent 11a0f6e3
Branches
No related tags found
No related merge requests found
......@@ -76,7 +76,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
......@@ -106,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 3,
"metadata": {},
"outputs": [
{
......@@ -136,7 +136,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.6 64-bit",
"display_name": "pythonintro-zVi-LRuq-py3.11",
"language": "python",
"name": "python3"
},
......@@ -150,14 +150,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.11.11"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
}
}
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
......
......@@ -192,7 +192,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
......@@ -493,7 +493,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "pythonintro-zVi-LRuq-py3.11",
"language": "python",
"name": "python3"
},
......@@ -507,14 +507,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.11.11"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
}
}
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
......
......@@ -149,7 +149,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 4,
"metadata": {},
"outputs": [
{
......@@ -196,7 +196,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 5,
"metadata": {},
"outputs": [
{
......@@ -305,7 +305,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
......@@ -323,7 +323,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 7,
"metadata": {},
"outputs": [
{
......@@ -358,7 +358,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 8,
"metadata": {},
"outputs": [
{
......@@ -386,7 +386,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 9,
"metadata": {},
"outputs": [
{
......@@ -435,7 +435,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.6 ('pythonintro-A08DqnGu-py3.10')",
"display_name": "pythonintro-zVi-LRuq-py3.11",
"language": "python",
"name": "python3"
},
......@@ -449,14 +449,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.11.11"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "eb3a944512c48027e49906d9e47d21cfb9b2c1ddd33f9b79c72df4b5e3a553dc"
}
}
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
......
......@@ -612,7 +612,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.6 ('pythonintro-A08DqnGu-py3.10')",
"display_name": "pythonintro-zVi-LRuq-py3.10",
"language": "python",
"name": "python3"
},
......@@ -628,12 +628,7 @@
"pygments_lexer": "ipython3",
"version": "3.10.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "eb3a944512c48027e49906d9e47d21cfb9b2c1ddd33f9b79c72df4b5e3a553dc"
}
}
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
......
This diff is collapsed.
pythonintro/solutions/SinCos.png

33.9 KiB

%% Cell type:markdown id: tags:
# Fast Python with Numba
%% Cell type:code id: tags:
``` python
# all imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
import numba
from numba import jit
```
%% Cell type:code id: tags:
``` python
print(numba.__version__)
```
%% Output
0.56.4
0.61.0
%% Cell type:code id: tags:
``` python
@jit(nopython=True)
def random_walk(n_steps = 5000, step_size = 1):
# we always start at (0,0)
x_points = [0]
y_points = [0]
# do the random walk:
for i in range(0, n_steps):
# choose direction:
# the following is the same as np.random.choice([-1,1]) but this cannot be optimized with Numba
x_dir = np.round(2*(np.random.randint(0,2)-0.5))
y_dir = np.round(2*(np.random.randint(0,2)-0.5))
# calculate new positions: last position + step_size * direction
new_x = x_points[-1] + step_size * x_dir
new_y = y_points[-1] + step_size * y_dir
# append to arrays
x_points.append(new_x)
y_points.append(new_y)
# calculate distance between start and end as Eucledian distance
# bit explicit as numba does not work with the one line we have used before
x_start = x_points[0]
y_start = y_points[0]
x_stop = x_points[-1]
y_stop = y_points[-1]
distance2 = (x_stop - x_start )**2 + ( y_stop - y_start )**2
distance = np.sqrt( distance2)
return x_points, y_points, distance
```
%% Cell type:markdown id: tags:
Now we can compare the timings with and without the ```@jit``` decorator. \
Remember that decorators change the behaviour of the function - but we do not have to change the function itself.
In this case, Numba is a specialised package that optimises a function "behind the scenes".
Note that the first call includes the optimisation / compile time. If we want to measure the time the optimised function takes, we need to discard the timing from the first call.
%% Cell type:code id: tags:
``` python
%%time
distances = []
for i in range(0,200):
_, _, distance = random_walk()
distances.append(distance)
```
%% Output
CPU times: user 34.5 ms, sys: 0 ns, total: 34.5 ms
Wall time: 33.9 ms
CPU times: user 30.9 ms, sys: 9.53 ms, total: 40.4 ms
Wall time: 39.5 ms
......
This diff is collapsed.
......@@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [
{
......@@ -48,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [
{
......@@ -103,7 +103,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [
{
......@@ -151,7 +151,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.6 ('pythonintro-hKmMjISb-py3.10')",
"display_name": "pythonintro-zVi-LRuq-py3.11",
"language": "python",
"name": "python3"
},
......@@ -165,14 +165,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.11.11"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "8eb59338b0c14423ef4b72291999b63587c3ee4bd1b8d960807ae48fbf6679f5"
}
}
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
......
This diff is collapsed.
,sepal_length,sepal_width,petal_length,petal_width,species
0,5.1,3.5,1.4,0.2,setosa
1,4.9,3.0,1.4,0.2,setosa
2,4.7,3.2,1.3,0.2,setosa
3,4.6,3.1,1.5,0.2,setosa
4,5.0,3.6,1.4,0.2,setosa
5,5.4,3.9,1.7,0.4,setosa
6,4.6,3.4,1.4,0.3,setosa
7,5.0,3.4,1.5,0.2,setosa
8,4.4,2.9,1.4,0.2,setosa
9,4.9,3.1,1.5,0.1,setosa
10,5.4,3.7,1.5,0.2,setosa
11,4.8,3.4,1.6,0.2,setosa
12,4.8,3.0,1.4,0.1,setosa
13,4.3,3.0,1.1,0.1,setosa
14,5.8,4.0,1.2,0.2,setosa
15,5.7,4.4,1.5,0.4,setosa
16,5.4,3.9,1.3,0.4,setosa
17,5.1,3.5,1.4,0.3,setosa
18,5.7,3.8,1.7,0.3,setosa
19,5.1,3.8,1.5,0.3,setosa
20,5.4,3.4,1.7,0.2,setosa
21,5.1,3.7,1.5,0.4,setosa
22,4.6,3.6,1.0,0.2,setosa
23,5.1,3.3,1.7,0.5,setosa
24,4.8,3.4,1.9,0.2,setosa
25,5.0,3.0,1.6,0.2,setosa
26,5.0,3.4,1.6,0.4,setosa
27,5.2,3.5,1.5,0.2,setosa
28,5.2,3.4,1.4,0.2,setosa
29,4.7,3.2,1.6,0.2,setosa
30,4.8,3.1,1.6,0.2,setosa
31,5.4,3.4,1.5,0.4,setosa
32,5.2,4.1,1.5,0.1,setosa
33,5.5,4.2,1.4,0.2,setosa
34,4.9,3.1,1.5,0.2,setosa
35,5.0,3.2,1.2,0.2,setosa
36,5.5,3.5,1.3,0.2,setosa
37,4.9,3.6,1.4,0.1,setosa
38,4.4,3.0,1.3,0.2,setosa
39,5.1,3.4,1.5,0.2,setosa
40,5.0,3.5,1.3,0.3,setosa
41,4.5,2.3,1.3,0.3,setosa
42,4.4,3.2,1.3,0.2,setosa
43,5.0,3.5,1.6,0.6,setosa
44,5.1,3.8,1.9,0.4,setosa
45,4.8,3.0,1.4,0.3,setosa
46,5.1,3.8,1.6,0.2,setosa
47,4.6,3.2,1.4,0.2,setosa
48,5.3,3.7,1.5,0.2,setosa
49,5.0,3.3,1.4,0.2,setosa
50,7.0,3.2,4.7,1.4,versicolor
51,6.4,3.2,4.5,1.5,versicolor
52,6.9,3.1,4.9,1.5,versicolor
53,5.5,2.3,4.0,1.3,versicolor
54,6.5,2.8,4.6,1.5,versicolor
55,5.7,2.8,4.5,1.3,versicolor
56,6.3,3.3,4.7,1.6,versicolor
57,4.9,2.4,3.3,1.0,versicolor
58,6.6,2.9,4.6,1.3,versicolor
59,5.2,2.7,3.9,1.4,versicolor
60,5.0,2.0,3.5,1.0,versicolor
61,5.9,3.0,4.2,1.5,versicolor
62,6.0,2.2,4.0,1.0,versicolor
63,6.1,2.9,4.7,1.4,versicolor
64,5.6,2.9,3.6,1.3,versicolor
65,6.7,3.1,4.4,1.4,versicolor
66,5.6,3.0,4.5,1.5,versicolor
67,5.8,2.7,4.1,1.0,versicolor
68,6.2,2.2,4.5,1.5,versicolor
69,5.6,2.5,3.9,1.1,versicolor
70,5.9,3.2,4.8,1.8,versicolor
71,6.1,2.8,4.0,1.3,versicolor
72,6.3,2.5,4.9,1.5,versicolor
73,6.1,2.8,4.7,1.2,versicolor
74,6.4,2.9,4.3,1.3,versicolor
75,6.6,3.0,4.4,1.4,versicolor
76,6.8,2.8,4.8,1.4,versicolor
77,6.7,3.0,5.0,1.7,versicolor
78,6.0,2.9,4.5,1.5,versicolor
79,5.7,2.6,3.5,1.0,versicolor
80,5.5,2.4,3.8,1.1,versicolor
81,5.5,2.4,3.7,1.0,versicolor
82,5.8,2.7,3.9,1.2,versicolor
83,6.0,2.7,5.1,1.6,versicolor
84,5.4,3.0,4.5,1.5,versicolor
85,6.0,3.4,4.5,1.6,versicolor
86,6.7,3.1,4.7,1.5,versicolor
87,6.3,2.3,4.4,1.3,versicolor
88,5.6,3.0,4.1,1.3,versicolor
89,5.5,2.5,4.0,1.3,versicolor
90,5.5,2.6,4.4,1.2,versicolor
91,6.1,3.0,4.6,1.4,versicolor
92,5.8,2.6,4.0,1.2,versicolor
93,5.0,2.3,3.3,1.0,versicolor
94,5.6,2.7,4.2,1.3,versicolor
95,5.7,3.0,4.2,1.2,versicolor
96,5.7,2.9,4.2,1.3,versicolor
97,6.2,2.9,4.3,1.3,versicolor
98,5.1,2.5,3.0,1.1,versicolor
99,5.7,2.8,4.1,1.3,versicolor
100,6.3,3.3,6.0,2.5,virginica
101,5.8,2.7,5.1,1.9,virginica
102,7.1,3.0,5.9,2.1,virginica
103,6.3,2.9,5.6,1.8,virginica
104,6.5,3.0,5.8,2.2,virginica
105,7.6,3.0,6.6,2.1,virginica
106,4.9,2.5,4.5,1.7,virginica
107,7.3,2.9,6.3,1.8,virginica
108,6.7,2.5,5.8,1.8,virginica
109,7.2,3.6,6.1,2.5,virginica
110,6.5,3.2,5.1,2.0,virginica
111,6.4,2.7,5.3,1.9,virginica
112,6.8,3.0,5.5,2.1,virginica
113,5.7,2.5,5.0,2.0,virginica
114,5.8,2.8,5.1,2.4,virginica
115,6.4,3.2,5.3,2.3,virginica
116,6.5,3.0,5.5,1.8,virginica
117,7.7,3.8,6.7,2.2,virginica
118,7.7,2.6,6.9,2.3,virginica
119,6.0,2.2,5.0,1.5,virginica
120,6.9,3.2,5.7,2.3,virginica
121,5.6,2.8,4.9,2.0,virginica
122,7.7,2.8,6.7,2.0,virginica
123,6.3,2.7,4.9,1.8,virginica
124,6.7,3.3,5.7,2.1,virginica
125,7.2,3.2,6.0,1.8,virginica
126,6.2,2.8,4.8,1.8,virginica
127,6.1,3.0,4.9,1.8,virginica
128,6.4,2.8,5.6,2.1,virginica
129,7.2,3.0,5.8,1.6,virginica
130,7.4,2.8,6.1,1.9,virginica
131,7.9,3.8,6.4,2.0,virginica
132,6.4,2.8,5.6,2.2,virginica
133,6.3,2.8,5.1,1.5,virginica
134,6.1,2.6,5.6,1.4,virginica
135,7.7,3.0,6.1,2.3,virginica
136,6.3,3.4,5.6,2.4,virginica
137,6.4,3.1,5.5,1.8,virginica
138,6.0,3.0,4.8,1.8,virginica
139,6.9,3.1,5.4,2.1,virginica
140,6.7,3.1,5.6,2.4,virginica
141,6.9,3.1,5.1,2.3,virginica
142,5.8,2.7,5.1,1.9,virginica
143,6.8,3.2,5.9,2.3,virginica
144,6.7,3.3,5.7,2.5,virginica
145,6.7,3.0,5.2,2.3,virginica
146,6.3,2.5,5.0,1.9,virginica
147,6.5,3.0,5.2,2.0,virginica
148,6.2,3.4,5.4,2.3,virginica
149,5.9,3.0,5.1,1.8,virginica
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment