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

Notebooks from ckaufmann/akis-python-framework@c8f63b69

parent 63e66c31
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:0005-e187e36902cac495c8ae263730f678ae8bb325e6c3d005199fd6156642e tags:
# Fourier-Features
Für Polynome konnten wir den `PolynomialFeatures`-Transformer aus
Scikit-Learn verwenden um eine `Pipeline` zu erstellen. Für die
Fourier-Basis aus den Folien gibt es etwas Vergleichbares in
Scikit-Learn nicht. Aber man eigene Transformerklassen erstellen. Dazu
gibt es einen
[Developer-Guide](https://scikit-learn.org/stable/developers/develop.html).
Erstellen Sie mit Hilfe des Start-Codes einen
`FourierFeatures`-Transformer. Sie müssen eigentlich nur den Code aus
der Funktion `fourier_basis` aus den Folien unten in `transform`
einfügen und anpassen. Der Rest kann so bleiben.
Schalten Sie einen `FourierFeatures`-Transformer mit einem
`LinearRegressor`-Predictor analog zu den Folien in einer `Pipeline`
zusammen und probieren Sie es mit dem Rechtecksignal aus.
## Lösung
Hier ist der vervollständigte Start-Code
%% Cell type:code id:0006-79e7256a8f55d5953df3c0bdc09bc5caf1fbef5b2a04f29ee89920b002b tags:
```
import matplotlib.pyplot as plt
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.utils.validation import check_array
class FourierFeatures(TransformerMixin, BaseEstimator):
def __init__(self, n=5):
self.n = n
def fit(self, X, y=None):
return self
def transform(self, X):
X = check_array(X)
feat = [np.sin(2 * np.pi * k * X) for k in range(1, 2 * self.n, 2)]
return np.hstack(feat)
```
%% Cell type:markdown id:0007-9141110863bba45617d0a5a57c13c53a2ec807e0c88099a2779b2a27335 tags:
Nun erzeugen wir die Daten des Rechtecksignals:
%% Cell type:code id:0008-0c1522ce68349ee9169030037cd3565ab7140c41f653899981ac73dd14c tags:
```
rng = np.random.default_rng()
x = rng.uniform(size=(100, 1)) * 3 - 0.5
y = np.round(x % 1) - 0.5
```
%% Cell type:markdown id:0009-5adca07baaf9c7b3aa25e95c4981bce6e570a56587e01d31f5900db5b43 tags:
Dann erstellen wir die Pipeline und führen die lineare Regression durch:
%% Cell type:code id:0010-93cbe118220f4710ccefc866c59f984ae87036f5c9a8d44491822cdbef9 tags:
```
linreg = LinearRegression()
fourierfeat = FourierFeatures()
pipe = make_pipeline(fourierfeat, linreg)
_ = pipe.fit(x, y)
```
%% Cell type:markdown id:0011-ea17d081123bb36a378777ae0c49bc94fd85ecb00dc6172d74102b74922 tags:
Und zum Plotten erzeugen wir nochmals Daten:
%% Cell type:code id:0012-1db9099f621293907c5c7d53504bd88c3c47f845593adbac8d7b12c8782 tags:
```
x_plt = np.linspace(0, 4, 501).reshape(-1, 1)
y_plt = pipe.predict(x_plt)
```
%% Cell type:code id:0013-33ce4abc218dfeba33010d98000b7ab2f86a4cb03325af8527dbf737b18 tags:
```
_ = plt.plot(x_plt, y_plt, color='C9')
_ = plt.scatter(x, y, color='r')
```
%% Cell type:markdown id:0015-a80e2c053991674d11a87ee57f87cef2e6dd321fa0946b068c5b8bbbb55 tags:
Sie können den Transformer zum Testen auch manuell benutzen. Vergleichen
wir mal den Output des Transformers mit dem Output von NumPy:
%% Cell type:code id:0016-b97bda8adeb5b88dff3e0b716fed046ddf246577dd950ed510be58fe451 tags:
```
ff1 = FourierFeatures(2)
ff1.fit_transform([[0.25], [0.1]])
```
%% Output
array([[ 1. , -1. ],
[ 0.58778525, 0.95105652]])
%% Cell type:code id:0017-0c22b9db0999110db88f4a8705a9c73d5d385a046b51b792cbb2fc541ec tags:
```
np.sin(2 * np.pi * np.array([0.25, 0.1]))
```
%% Output
array([1. , 0.58778525])
%% Cell type:code id:0018-0551750465cf96a719668f732b38000766235ae86c3ad903715b34e055c tags:
```
np.sin(6 * np.pi * np.array([0.25, 0.1]))
```
%% Output
array([-1. , 0.95105652])
%% Cell type:markdown id:0002-63a44d15ba8dc22f7a855109e5eccf42652d2c20372770ee9663e1f9dfa tags:
# Fourier-Features
Für Polynome konnten wir den `PolynomialFeatures`-Transformer aus
Scikit-Learn verwenden um eine `Pipeline` zu erstellen. Für die
Fourier-Basis aus den Folien gibt es etwas Vergleichbares in
Scikit-Learn nicht. Aber man eigene Transformerklassen erstellen. Dazu
gibt es einen
[Developer-Guide](https://scikit-learn.org/stable/developers/develop.html).
Erstellen Sie mit Hilfe des Start-Codes einen
`FourierFeatures`-Transformer. Sie müssen eigentlich nur den Code aus
der Funktion `fourier_basis` aus den Folien unten in `transform`
einfügen und anpassen. Der Rest kann so bleiben.
%% Cell type:code id:0003-379c67e3d2a9e847aa1e4def11eb619207138c7704aa35dfccea18aca00 tags:
```
import matplotlib.pyplot as plt
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.utils.validation import check_array
class FourierFeatures(TransformerMixin, BaseEstimator):
def __init__(self, n=5):
self.n = n
def fit(self, X, y=None):
return self
def transform(self, X):
X = check_array(X)
# TODO: hier das transformierte X zurück geben
return
```
%% Cell type:markdown id:0005-25a84871ceee40741f89f5344d86128e3fe3a1443a727bb6d693eb4833e tags:
Schalten Sie einen `FourierFeatures`-Transformer mit einem
`LinearRegressor`-Predictor analog zu den Folien in einer `Pipeline`
zusammen und probieren Sie es mit dem Rechtecksignal aus.
Sie können den Transformer zum Testen auch manuell benutzen. Vergleichen
wir mal den Output des Transformers mit dem Output von NumPy:
%% Cell type:code id:0006-b97bda8adeb5b88dff3e0b716fed046ddf246577dd950ed510be58fe451 tags:
```
ff1 = FourierFeatures(2)
ff1.fit_transform([[0.25], [0.1]])
```
%% Output
array([[ 1. , -1. ],
[ 0.58778525, 0.95105652]])
%% Cell type:code id:0007-0c22b9db0999110db88f4a8705a9c73d5d385a046b51b792cbb2fc541ec tags:
```
np.sin(2 * np.pi * np.array([0.25, 0.1]))
```
%% Output
array([1. , 0.58778525])
%% Cell type:code id:0008-0551750465cf96a719668f732b38000766235ae86c3ad903715b34e055c tags:
```
np.sin(6 * np.pi * np.array([0.25, 0.1]))
```
%% Output
array([-1. , 0.95105652])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment