Skip to content
Snippets Groups Projects
Commit f016e066 authored by Christof Kaufmann's avatar Christof Kaufmann
Browse files

Notebooks from applied-cs/data-science@9c21c74b

parent 2e17c716
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id:0005-c6178e60b944258ad3f127adea03eb4ee0da7c74b1a39b54c9400760909 tags:
# Matrix als 2D-Array
Wir erzeugen in dieser Übung eine Matrix, einen Vektor und führen eine
Matrix-Vektor-Multiplikation durch.
## a) Matrix erzeugen
Erstellen Sie ein 2D-NumPy-Array `a` mit `arange` und `reshape` wie
dargestellt:
$\begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \end{bmatrix}$
Hier Ihr Code:
%% Cell type:code id:0006-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 tags:
```
```
%% Cell type:markdown id:0008-3ecf6e6b5ab63e42f08e072a90d315657776722faefa171a4c5a4bf408b tags:
### Lösung zu a)
Wir erstellen einen Vektor von 1, …, 7 (ohne 7) und führen ein `reshape`
auf $2 \times 3$ durch um das geforderte Array zu erhalten.
%% Cell type:code id:0009-e4b6c21541fce42f2f2254a5e852efd31e50df101e190790c5cd7b090ec tags:
```
import numpy as np
a = np.arange(1, 7).reshape((2, 3))
```
%% Cell type:markdown id:0011-1cc6e5f1e34374c41c7c0866c7bc97c9953348cde0fc8ef578593078396 tags:
### Tests zu a)
Wir testen, ob die Elemente wie erwartet sind:
%% Cell type:code id:0012-112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb tags:
```
a
```
%% Output
array([[1, 2, 3],
[4, 5, 6]])
%% Cell type:markdown id:0017-0328546f1dffddc990c09fb5d601b0a937e5de407867597480a75115e3f tags:
## b) Matrix-Vektor-Multiplikation
Erstellen Sie ein 1D-NumPy-Array `v` mit `arange` wie dargestellt:
$\begin{bmatrix} 10\\ 8\\ 6 \end{bmatrix}$
Führen Sie die Matrix-Vektor-Multiplikation $a \, v$ durch und speichern
das Ergebnis in `w`!
Hier Ihr Code:
%% Cell type:code id:0018-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 tags:
```
```
%% Cell type:markdown id:0020-babf4709a23502cf9119e5bb5cce3c40af6d20decb15606ae4514709347 tags:
### Lösung zu b)
Wir erstellen den Vektor von 10, …, 5 mit negativer Schrittweite -2.
%% Cell type:code id:0021-4e8d763959efa25db7e48a520463917283a0207d03032bb1fdc4fc21dcc tags:
```
v = np.arange(10, 5, -2)
```
%% Cell type:markdown id:0022-38706663a0a925df41ae4d5761e58c8cc60171cb95935c0e2f0995441f2 tags:
Nun führen wir die Matrix-Vektor-Multiplikation durch.
%% Cell type:code id:0023-d45b03c5a5db93eaafa612535b471de9d8e9ba8a32f5b2761a5aa2f51d6 tags:
```
w = a @ v
```
%% Cell type:markdown id:0025-992f177dd16a0d1b483ed05e06783dc1801f9c0eacc242e35500af5a238 tags:
### Tests zu b)
Wir testen, ob die Elemente von `v` wie erwartet sind:
%% Cell type:code id:0026-85e0c21ae6c41ce1dfe7b6bfaceea5ab68e40a2476f50208e526f506080 tags:
```
v
```
%% Output
array([10, 8, 6])
%% Cell type:markdown id:0027-1ace5586569a4ff843fe20890c8f950f459c0c0377b3c0578ec86be031d tags:
Und das Ergebnis der Matrix-Vektor-Multiplikation:
%% Cell type:code id:0028-1e49c013f00c62cf59f2163542a9d8df02464efeb615d31051b0fddc326 tags:
```
w
```
%% Output
array([ 44, 116])
%% Cell type:markdown id:0031-95db6bdf5836af7e18d40db1d82afda3c54771e55900ff5af4368f80a6a tags:
## c) Sortierung
Sortieren Sie den Vektor `v` mit `np.sort` und speichern das Ergebnis in
`v_sorted`! Schauen Sie sich im Test an, ob es aufsteigend oder
absteigend sortiert ist! Können Sie die Sortierreihenfolge über Slicing
umkehren? Versuchen Sie es!
Hier Ihr Code:
%% Cell type:code id:0032-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 tags:
```
```
%% Cell type:markdown id:0036-eaceadf3fc5eb1dc3b7e431fc79499ba646e7d6c2ad4506f0bb5dfee218 tags:
Nun verwenden Sie np.argsort um die Indizes der sortierten Elemente zu
erhalten und speichern diese in `v_sorted_indices`! Schauen Sie sich im
Test unten an, was passiert, wenn Sie `v_sorted_indices` auf `v`
anwenden!
*Bonus: Schaffen Sie es, die Spalten von `a` nach der Sortierung von `v`
zu sortieren?*
### Lösung zu c)
Zunächst sortieren wir den Vektor `v` aufsteigend und absteigend. Wir
können die Sortierreihenfolge umkehren, indem wir mit negativer
Schrittweite slicen.
%% Cell type:code id:0037-4a6d98a7ec96685ce5f477cc14db5ba7f2da27c9b82e2695e8a3d523b29 tags:
```
v_sorted = np.sort(v) # ascending (aufsteigend)
v_sorted_reversed = np.sort(v)[::-1] # descending (absteigend)
```
%% Cell type:markdown id:0038-27837dafe02bffae4f451b45bf240087053d24ec642144cc73ea2a37fe0 tags:
Die Absteigende Sortierung ist natürlich langweilig, weil es die
ursprüngliche Reihenfolge ist. Nun führen wir die
Matrix-Vektor-Multiplikation durch.
%% Cell type:code id:0039-9ff970c568dd84250d1699058e82096ed5be1a0f584a46884def166f8ec tags:
```
v_sorted_reversed
```
%% Output
array([10, 8, 6])
%% Cell type:markdown id:0040-f30aad78b36f2c5f31a3c61a51c55c9e43b54b9aa9b4399b0750a26e220 tags:
Nun verwenden wir `argsort` um die Indizes der sortierten Elemente zu
erhalten und speichern diese in `v_sorted_indices`. Man sieht im Test,
dass die Anwendung der Sortierindizes auf `v` das gleiche Ergebnis wie
`v_sorted` liefert. Für die Bonusaufgabe wenden wir die Sortierindizes
auf die Spalten von `a` an. Allgemein lassen sich so zwei Arrays
gleichartig sortieren, z. B. $y$-Werte nach den entsprechenden
$x$-Werten.
%% Cell type:code id:0041-2443864e0527e20381f777a9b3c01dd0380b4046bcd7b457ed89fedbf1f tags:
```
v_sorted_indices = np.argsort(v)
a_sorted = a[:, v_sorted_indices]
```
%% Cell type:code id:0042-00c0156accd3cd62646f7445303d5958f50202e08b93250d21117e0e0bf tags:
```
a_sorted
```
%% Output
array([[3, 2, 1],
[6, 5, 4]])
%% Cell type:markdown id:0044-23fffe701d76dc191fa3759ad4ad76eb59b720f4c3ef33d11e45e75011f tags:
### Tests zu c)
Der Vektor `v` ist:
%% Cell type:code id:0045-85e0c21ae6c41ce1dfe7b6bfaceea5ab68e40a2476f50208e526f506080 tags:
```
v
```
%% Output
array([10, 8, 6])
%% Cell type:markdown id:0046-fa52a44b66f51668e263ae189ef6422d0aa84c1d63c15b993716154ed71 tags:
Sortiert:
%% Cell type:code id:0047-fe5508108e9636d2ad10f6c6a611a6fa460ca33e04dda3400c76d4bbe12 tags:
```
v_sorted
```
%% Output
array([ 6, 8, 10])
%% Cell type:markdown id:0048-11a98d7fa4c96482460ec065f0faa4197563223281eb451dc5034ccfced tags:
Sortierindizes:
%% Cell type:code id:0049-fa89420e96785c287fad6b270873c081d43db4779e9a2503ae62bea9003 tags:
```
v_sorted_indices
```
%% Output
array([2, 1, 0])
%% Cell type:markdown id:0050-c1108c165d8111c761de5a684ff69c2058d4d52b6c89ba1c589070f56a7 tags:
Und die Anwendung der Sortierindizes auf `v`:
%% Cell type:code id:0051-f0e4f4e230e723cda122e72eb4cc32c10ac8c30139419ccd2df6a8f01c6 tags:
```
v[v_sorted_indices]
```
%% Output
array([ 6, 8, 10])
%% Cell type:markdown id:0005-c6178e60b944258ad3f127adea03eb4ee0da7c74b1a39b54c9400760909 tags:
# Matrix als 2D-Array
Wir erzeugen in dieser Übung eine Matrix, einen Vektor und führen eine
Matrix-Vektor-Multiplikation durch.
## a) Matrix erzeugen
Erstellen Sie ein 2D-NumPy-Array `a` mit `arange` und `reshape` wie
dargestellt:
$\begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \end{bmatrix}$
Hier Ihr Code:
%% Cell type:code id:0006-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 tags:
```
```
%% Cell type:markdown id:0008-1cc6e5f1e34374c41c7c0866c7bc97c9953348cde0fc8ef578593078396 tags:
### Tests zu a)
Wir testen, ob die Elemente wie erwartet sind:
%% Cell type:code id:0009-112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb tags:
```
a
```
%% Output
array([[1, 2, 3],
[4, 5, 6]])
%% Cell type:markdown id:0014-0328546f1dffddc990c09fb5d601b0a937e5de407867597480a75115e3f tags:
## b) Matrix-Vektor-Multiplikation
Erstellen Sie ein 1D-NumPy-Array `v` mit `arange` wie dargestellt:
$\begin{bmatrix} 10\\ 8\\ 6 \end{bmatrix}$
Führen Sie die Matrix-Vektor-Multiplikation $a \, v$ durch und speichern
das Ergebnis in `w`!
Hier Ihr Code:
%% Cell type:code id:0015-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 tags:
```
```
%% Cell type:markdown id:0017-992f177dd16a0d1b483ed05e06783dc1801f9c0eacc242e35500af5a238 tags:
### Tests zu b)
Wir testen, ob die Elemente von `v` wie erwartet sind:
%% Cell type:code id:0018-85e0c21ae6c41ce1dfe7b6bfaceea5ab68e40a2476f50208e526f506080 tags:
```
v
```
%% Output
array([10, 8, 6])
%% Cell type:markdown id:0019-1ace5586569a4ff843fe20890c8f950f459c0c0377b3c0578ec86be031d tags:
Und das Ergebnis der Matrix-Vektor-Multiplikation:
%% Cell type:code id:0020-1e49c013f00c62cf59f2163542a9d8df02464efeb615d31051b0fddc326 tags:
```
w
```
%% Output
array([ 44, 116])
%% Cell type:markdown id:0023-95db6bdf5836af7e18d40db1d82afda3c54771e55900ff5af4368f80a6a tags:
## c) Sortierung
Sortieren Sie den Vektor `v` mit `np.sort` und speichern das Ergebnis in
`v_sorted`! Schauen Sie sich im Test an, ob es aufsteigend oder
absteigend sortiert ist! Können Sie die Sortierreihenfolge über Slicing
umkehren? Versuchen Sie es!
Hier Ihr Code:
%% Cell type:code id:0024-44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 tags:
```
```
%% Cell type:markdown id:0028-0df12fdea3eb22d455be855d3463643a0adb3e858fe1e64242c52d75b38 tags:
Nun verwenden Sie np.argsort um die Indizes der sortierten Elemente zu
erhalten und speichern diese in `v_sorted_indices`! Schauen Sie sich im
Test unten an, was passiert, wenn Sie `v_sorted_indices` auf `v`
anwenden!
*Bonus: Schaffen Sie es, die Spalten von `a` nach der Sortierung von `v`
zu sortieren?*
### Tests zu c)
Der Vektor `v` ist:
%% Cell type:code id:0029-85e0c21ae6c41ce1dfe7b6bfaceea5ab68e40a2476f50208e526f506080 tags:
```
v
```
%% Output
array([10, 8, 6])
%% Cell type:markdown id:0030-fa52a44b66f51668e263ae189ef6422d0aa84c1d63c15b993716154ed71 tags:
Sortiert:
%% Cell type:code id:0031-fe5508108e9636d2ad10f6c6a611a6fa460ca33e04dda3400c76d4bbe12 tags:
```
v_sorted
```
%% Output
array([ 6, 8, 10])
%% Cell type:markdown id:0032-11a98d7fa4c96482460ec065f0faa4197563223281eb451dc5034ccfced tags:
Sortierindizes:
%% Cell type:code id:0033-fa89420e96785c287fad6b270873c081d43db4779e9a2503ae62bea9003 tags:
```
v_sorted_indices
```
%% Output
array([2, 1, 0])
%% Cell type:markdown id:0034-c1108c165d8111c761de5a684ff69c2058d4d52b6c89ba1c589070f56a7 tags:
Und die Anwendung der Sortierindizes auf `v`:
%% Cell type:code id:0035-f0e4f4e230e723cda122e72eb4cc32c10ac8c30139419ccd2df6a8f01c6 tags:
```
v[v_sorted_indices]
```
%% Output
array([ 6, 8, 10])
%% Cell type:markdown id:0005-c49301c38b440d5f680bae1298919020ffdd23be9734d4417a0f951950c tags:
%% Cell type:markdown id:0005-6c4225b83583e52ce194438956dca32831559082e0d85e600bd6eedf084 tags:
# Ziehung
Erstellen Sie eine Matrix `A` wie dargestellt (Tipp: `arange` und
`reshape`).
| Zeile \\ Spalte | 0 | 1 | 2 |
|-----------------|:---:|:---:|:---:|
| 0 | 0 | 1 | 2 |
| 1 | 3 | 4 | 5 |
| 2 | 6 | 7 | 8 |
| 3 | 9 | 10 | 11 |
| 4 | 12 | 13 | 14 |
| 5 | 15 | 16 | 17 |
| 6 | 18 | 19 | 20 |
Nun ist das Ziel aus jeder Zeile ein Element zufällig auszuwählen.
Erstellen Sie zwei Indizes `i`, `j` für die Zeilen bzw. Spalten, sodass
`A[i, j]` der gewünschte Vektor ist.
## Lösung
Wir erstellen einen Vektor von 0, …, 20 und führen ein `reshape` auf
$7 imes 3$ durch um das geforderte Array zu erhalten.
$7 \times 3$ durch um das geforderte Array zu erhalten.
%% Cell type:code id:0006-3c437238c279b4d5d83cea255b3777e70d4207076b8a3637e9a32a715e2 tags:
```
import numpy as np
rng = np.random.default_rng()
A = np.arange(21).reshape((7, 3))
```
%% Cell type:markdown id:0007-327a66afac1eab2432a0480fcd7b10ef52d81e7143622cb4066439769e6 tags:
Anschließend wählen wir für `i` einfach einen Vektor 0, …, 6, weil wir
ja aus *jeder* Zeile ein Element ziehen wollen und für die Spalten
erzeugen wir einen Vektor mit 7 zufälligen Elementen zwischen 0 und 2,
entsprechend den Spaltenindizes. Dann kombiniert die komplexe
Indizierung `A[i, j]` die Indizes paarweise und wir erhalten die
gewünschten Elemente:
%% Cell type:code id:0008-4b94664435e9c237fcbbad2b5e67b9c4d05ebc8584b76d3fed083906ae7 tags:
```
i = np.arange(A.shape[0]) # hier: [0 1 2 3 4 5 6]
j = rng.choice(A.shape[1], size=A.shape[0])
```
%% Cell type:markdown id:0010-6f9f7a194530ee6f13ab985ccd222beb82ad15bea8eb7d72b7614d2ad2a tags:
## Tests
Wir geben die zufällig gewählten Elemente aus:
%% Cell type:code id:0011-0178a0acabe8bd93bb9cf9c7c95b8ec1cf03ba7595d0b51deb5ab06a987 tags:
```
print(A[i, j]) # Ausgabe zufällig
```
%% Output
[ 0 4 8 9 12 17 18]
%% Cell type:markdown id:0012-186463924299dea0ec08c78350e54d4a94a4906f8f80aab3b60eeee4efb tags:
Die Elemente müssen aufsteigend sein und die Differenz kann nicht größer
als 5 sein:
%% Cell type:code id:0013-4ddd9b6771d97bf4d423827f9fee4c05bd072fb576877c0595a533d9564 tags:
```
sel = A[i, j]
diff = sel[1:] - sel[:-1]
(0 < diff) & (diff < 6)
```
%% Output
array([ True, True, True, True, True, True])
......
%% Cell type:markdown id:0002-bc8683364aac1c6f5ed7b278d3583b1b347acfeb6f1bc5b8c2917727d2f tags:
# Kuddelmuddel
Führen Sie zunächst den Startcode aus und schauen sich den Plot im
Abschnitt [Test](#tests) an!
Hier ist die Reihenfolge durcheinander geraten. Sortieren Sie $x$ und
zwar so, dass Sie $y$ entsprechend sortieren können, damit die
$(x, y)$-Paare noch zusammen passen. Plotten Sie anschließend die Punkte
mit einem Linienplot.
%% Cell type:code id:0003-fcca641da37b0e2c663882c5be52b12dcfa29763fdbe639041f2f8705db tags:
```
import matplotlib.pyplot as plt
import numpy as np
x = np.array([ 0., 1.79, 1.58, 0.11, 0.84, 0.53, 1.16, 0.32, 1.89, 1.68, 1.37, 0.21, 0.95, 2., 0.42, 1.26, 0.74, 1.05, 1.47, 0.63])
y = np.array([ 0., 0.46, 1., 0.5, -0.88, 0.49, -0.47, 1., -0.05, 0.84, 0.53, 0.87, -1., -0.54, 0.86, 0.03, -0.52, -0.85, 0.88, -0.02,])
```
%% Cell type:markdown id:0005-d8d78488067bdbcd104303976ff8c0e94d4a14e6b94cf1aabe757aa1daf tags:
## Lösung
Damit $y$ noch zu den richtigen $x$ zugeordnet werden kann, sortieren
wir $x$ nicht direkt, sondern bilden erst den Sortierindex und ordnen
dann $x$ und $y$ gleichermaßen, indem wir sie mit dem Sortierindex
indizieren.
%% Cell type:code id:0006-9951e655ea7e2d7f082cbaaf415a6abc2b9ed7c989b519cdce62b4df1ba tags:
```
sort_ind = np.argsort(x)
x = x[sort_ind]
y = y[sort_ind]
```
%% Cell type:markdown id:0008-72436094b4e7ff41609abded3de75f7cb16c0f07561e2dcf90a1554132e tags:
## Tests
Hier die Abfrage, ob $x$ sortiert ist:
%% Cell type:code id:0009-fdb44a5fb823638e8b19890b97b50cc9b208afe518f1f4ffcc95510ac27 tags:
```
np.all(np.diff(x) > 0) # differenz zum nächsten Wert jeweils positiv?
```
%% Output
False
%% Cell type:markdown id:0010-35ab204c652219970b5613a958ba252c89954e983eb890539b9f924553f tags:
Und der Plot:
%% Cell type:code id:0011-3eb6515085572ba2b78abc2ce9f3ebfb4d04c5661dcbd5a964ff73f2fe9 tags:
```
plt.plot(x, y)
```
%% Cell type:markdown id:0002-bc8683364aac1c6f5ed7b278d3583b1b347acfeb6f1bc5b8c2917727d2f tags:
# Kuddelmuddel
Führen Sie zunächst den Startcode aus und schauen sich den Plot im
Abschnitt [Test](#tests) an!
Hier ist die Reihenfolge durcheinander geraten. Sortieren Sie $x$ und
zwar so, dass Sie $y$ entsprechend sortieren können, damit die
$(x, y)$-Paare noch zusammen passen. Plotten Sie anschließend die Punkte
mit einem Linienplot.
%% Cell type:code id:0003-fcca641da37b0e2c663882c5be52b12dcfa29763fdbe639041f2f8705db tags:
```
import matplotlib.pyplot as plt
import numpy as np
x = np.array([ 0., 1.79, 1.58, 0.11, 0.84, 0.53, 1.16, 0.32, 1.89, 1.68, 1.37, 0.21, 0.95, 2., 0.42, 1.26, 0.74, 1.05, 1.47, 0.63])
y = np.array([ 0., 0.46, 1., 0.5, -0.88, 0.49, -0.47, 1., -0.05, 0.84, 0.53, 0.87, -1., -0.54, 0.86, 0.03, -0.52, -0.85, 0.88, -0.02,])
```
%% Cell type:markdown id:0005-72436094b4e7ff41609abded3de75f7cb16c0f07561e2dcf90a1554132e tags:
## Tests
Hier die Abfrage, ob $x$ sortiert ist:
%% Cell type:code id:0006-fdb44a5fb823638e8b19890b97b50cc9b208afe518f1f4ffcc95510ac27 tags:
```
np.all(np.diff(x) > 0) # differenz zum nächsten Wert jeweils positiv?
```
%% Output
False
%% Cell type:markdown id:0007-35ab204c652219970b5613a958ba252c89954e983eb890539b9f924553f tags:
Und der Plot:
%% Cell type:code id:0008-3eb6515085572ba2b78abc2ce9f3ebfb4d04c5661dcbd5a964ff73f2fe9 tags:
```
plt.plot(x, y)
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment