-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/concurrent regression #531
Changes from all commits
d2d2837
69277ef
b1dc2dd
f102163
94c334b
cfc3b09
b27f107
26691b3
522d659
9409830
0ed8c80
4d46345
9cfd103
c9ff81a
ada5e40
32db8bd
371f056
14be648
0ea1664
a20ec8f
15ea681
aa630d5
7d03538
bdc69cb
100b7f4
c6bdf28
4e6bed9
925b602
d418787
0d0fc19
69aed6a
679875d
ed03263
09db85e
f897a2b
dc51e44
596a5fc
b9fd149
a946d95
a020784
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
Functional Linear Regression with multivariate covariates. | ||
========================================================== | ||
|
||
This example explores the use of the linear regression with | ||
multivariate (scalar) covariates and functional response. | ||
|
||
""" | ||
|
||
# Author: Rafael Hidalgo Alejo | ||
# License: MIT | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from sklearn.preprocessing import OneHotEncoder | ||
|
||
import skfda | ||
from skfda.ml.regression import LinearRegression | ||
from skfda.representation.basis import FDataBasis, FourierBasis | ||
|
||
# %% | ||
# In this example, we will demonstrate the use of the Linear Regression with | ||
# functional response and multivariate covariates using the | ||
# :func:`weather <skfda.datasets.fetch_weather>` dataset. | ||
# It is possible to divide the weather stations into four groups: | ||
# Atlantic, Pacific, Continental and Artic. | ||
# There are a total of 35 stations in this dataset. | ||
|
||
X_weather, y_weather = skfda.datasets.fetch_weather( | ||
return_X_y=True, as_frame=True, | ||
) | ||
fd = X_weather.iloc[:, 0].values | ||
|
||
# %% | ||
# The main goal is knowing about the effect of stations' geographic location | ||
# on the shape of the temperature curves. | ||
# So we will have a model with a functional response, the temperature curve, | ||
# and five covariates. The first one is the intercept (all entries equal to 1) | ||
# and it shows the contribution of the Canadian mean temperature. The remaining | ||
# covariates use one-hot encoding, with 1 if that weather station is in the | ||
# corresponding climate zone and 0 otherwise. | ||
|
||
# We first create the one-hot encoding of the climates. | ||
|
||
enc = OneHotEncoder(handle_unknown='ignore') | ||
enc.fit([['Atlantic'], ['Continental'], ['Pacific']]) | ||
X = np.array(y_weather).reshape(-1, 1) | ||
X = enc.transform(X).toarray() | ||
|
||
# %% | ||
# Then, we construct a dataframe with each covariate in a different column and | ||
# the temperature curves (responses). | ||
|
||
X_df = pd.DataFrame(X) | ||
|
||
y_basis = FourierBasis(n_basis=65) | ||
y_fd = fd.coordinates[0].to_basis(y_basis) | ||
|
||
# %% | ||
# An intercept term is incorporated. | ||
# All functional coefficients will have the same basis as the response. | ||
|
||
funct_reg = LinearRegression(fit_intercept=True) | ||
funct_reg.fit(X_df, y_fd) | ||
|
||
# %% | ||
# The regression coefficients are shown below. The first one is the intercept | ||
# coefficient, corresponding to Canadian mean temperature. | ||
|
||
funct_reg.intercept_.plot() | ||
funct_reg.coef_[0].plot() | ||
funct_reg.coef_[1].plot() | ||
funct_reg.coef_[2].plot() | ||
|
||
# %% | ||
# Finally, it is shown a panel with the prediction for all climate zones. | ||
|
||
predictions = [] | ||
|
||
predictions.append(funct_reg.predict([[0, 1, 0, 0]])[0]) | ||
predictions.append(funct_reg.predict([[0, 0, 1, 0]])[0]) | ||
predictions.append(funct_reg.predict([[0, 0, 0, 1]])[0]) | ||
|
||
predictions_conc = FDataBasis.concatenate(*predictions) | ||
|
||
predictions_conc.argument_names = ('day',) | ||
predictions_conc.coordinate_names = ('temperature (ºC)',) | ||
|
||
predictions_conc.plot() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,8 +157,12 @@ def coefficient_info_from_covariate( | |
def _coefficient_info_from_covariate_ndarray( # type: ignore[misc] | ||
X: NDArrayFloat, | ||
y: NDArrayFloat, | ||
basis: Basis = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [mypy] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 [mypy] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 [mypy] reported by reviewdog 🐶 |
||
**_: Any, | ||
) -> CoefficientInfo[NDArrayFloat]: | ||
if basis: | ||
return CoefficientInfoNdarray(basis=basis) | ||
|
||
return CoefficientInfoNdarray(basis=np.identity(X.shape[1], dtype=X.dtype)) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you attempting here?