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

Notebooks from applied-cs/data-science@37a9ef7c

parent 4825e596
Branches main
No related tags found
No related merge requests found
%% Cell type:markdown id:0002-2465e64373be0da127a2abebf7ac7f2bffb49524f9069ee09a7bf51c6fd tags:
%% Cell type:markdown id:0002-bf5eef6982876fd1fb090101d890fc7eb147adc9338aa6ed05e3b900d3d tags:
# Feature-Map
In dieser Aufgabe wollen wir die Features entsprechend der
Korrelationsmatrix auf einer Karte plotten.
1. Laden Sie die Autodaten aus `autos.csv` als DataFrame.
2. *Bonus: Werfen Sie die Ausreißer raus. Was hat das für Auswirkungen
auf das Ergebnis.*
auf das Ergebnis?*
3. Berechnen Sie die Korrelationsmatrix.
4. Wandeln Sie die Korrelationsmatrix $P$ in eine Distanzmatrix
$D = \sqrt{1 - P}$ um. *Bonus: Probieren Sie auch
$D = \sqrt{1 - |P|}$*
5. Finden Sie mit MDS die Koordinaten $X_p$ zu den Features. Sie
benötigen bei der Initialisierung den Parameter
`dissimilarity='precomputed'`, damit Sie in `fit` bzw.
`fit_transform` $D$ (anstatt $X$) reingeben können.
6. Plotten Sie das Ergebnis mittels Plotly Express’ Scatter-Plot, denn
da können Sie zusätzlich zu `data_frame=`$X_p$, `x=0` und `y=1` an
das Argument `text` die Feature-Namen übergeben.
6. Plotten Sie das Ergebnis mittels Plotly Express’ Scatter-Plot.
Übergeben Sie an `data_frame=`$X_p$, an `x=0` und an `y=1`.
Zusätzlich übergeben Sie die Feature-Namen an das Argument `text`.
%% Cell type:code id:0003-c1bb0a9ce1897e013bbc5224cd3031da808967b4ce5f467e752db79b3b6 tags:
```
import numpy as np
import pandas as pd
import plotly.express as px
from sklearn.manifold import MDS
```
......
......@@ -127,7 +127,7 @@ scatter_trace = go.Scatter3d(
y=X_train_std.iloc[:, 1],
z=X_train_std.iloc[:, 2],
mode='markers',
marker=dict(color=y, colorscale='Viridis', size=4), name='Iris Data'
marker=dict(color=y_train, colorscale='Viridis', size=4), name='Iris Data'
)
arrow_start_factor = 0.9
......
%% Cell type:markdown id:0002-2465e64373be0da127a2abebf7ac7f2bffb49524f9069ee09a7bf51c6fd tags:
%% Cell type:markdown id:0002-bf5eef6982876fd1fb090101d890fc7eb147adc9338aa6ed05e3b900d3d tags:
# Feature-Map
In dieser Aufgabe wollen wir die Features entsprechend der
Korrelationsmatrix auf einer Karte plotten.
1. Laden Sie die Autodaten aus `autos.csv` als DataFrame.
2. *Bonus: Werfen Sie die Ausreißer raus. Was hat das für Auswirkungen
auf das Ergebnis.*
auf das Ergebnis?*
3. Berechnen Sie die Korrelationsmatrix.
4. Wandeln Sie die Korrelationsmatrix $P$ in eine Distanzmatrix
$D = \sqrt{1 - P}$ um. *Bonus: Probieren Sie auch
$D = \sqrt{1 - |P|}$*
5. Finden Sie mit MDS die Koordinaten $X_p$ zu den Features. Sie
benötigen bei der Initialisierung den Parameter
`dissimilarity='precomputed'`, damit Sie in `fit` bzw.
`fit_transform` $D$ (anstatt $X$) reingeben können.
6. Plotten Sie das Ergebnis mittels Plotly Express’ Scatter-Plot, denn
da können Sie zusätzlich zu `data_frame=`$X_p$, `x=0` und `y=1` an
das Argument `text` die Feature-Namen übergeben.
6. Plotten Sie das Ergebnis mittels Plotly Express’ Scatter-Plot.
Übergeben Sie an `data_frame=`$X_p$, an `x=0` und an `y=1`.
Zusätzlich übergeben Sie die Feature-Namen an das Argument `text`.
%% Cell type:code id:0003-c1bb0a9ce1897e013bbc5224cd3031da808967b4ce5f467e752db79b3b6 tags:
```
import numpy as np
import pandas as pd
import plotly.express as px
from sklearn.manifold import MDS
```
%% Cell type:markdown id:0005-2b2e02f7c099c0b3c2e7ee38e724334b181f374b2f6da5066b33d7489c5 tags:
## Lösung
Hier der Code zur Lösung:
%% Cell type:code id:0006-472ff22b9cdec2be85fd14f451bb4cdea7db8ee3cbf28c128c994cf0453 tags:
```
df = pd.read_csv('autos.csv').drop(columns=['Marke', 'Modell'])
# df = df[df.Grundpreis < 150_000] # mit Ausreißern ist der Grundpreis weit weg von den Motordaten
corr = df.corr()
dist_corr = np.sqrt(1 - corr)
# dist_corr = np.sqrt(1 - np.abs(corr)) # mit abs rückt die Türanzahl näher an alle anderes
mds = MDS(dissimilarity='precomputed', normalized_stress='auto')
corr_map = mds.fit_transform(dist_corr)
corr_map = pd.DataFrame(corr_map)
corr_map['feature'] = corr.columns
```
%% Cell type:markdown id:0007-96c5b071fc624449a3bff00f5acf449ff13a3986090c619ea3c40dbebf7 tags:
Der Grundpreis ist ohne Ausreißer näher an den Motordaten, d.h. der
Preis verhält sich ähnlich. Mit Ausreißer ist der Preis weit weg. Das
lässt sich so interpretieren, dass der Preis für sehr teure Autos nicht
mehr im Verhältnis zum Motor steht.
%% Cell type:code id:0008-20ca1c79ef727fdf527b2a98d7d6fe563ef6fd9c2b005bb1fdfb364bbbb tags:
```
px.scatter(corr_map, x=0, y=1, text=corr.columns)
```
......
......@@ -127,7 +127,7 @@ scatter_trace = go.Scatter3d(
y=X_train_std.iloc[:, 1],
z=X_train_std.iloc[:, 2],
mode='markers',
marker=dict(color=y, colorscale='Viridis', size=4), name='Iris Data'
marker=dict(color=y_train, colorscale='Viridis', size=4), name='Iris Data'
)
arrow_start_factor = 0.9
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment