RBF
Bases: BaseInterpolation
Radial Basis Function (RBF) interpolation model.
Attributes: |
|
---|
Methods:
Name | Description |
---|---|
fit |
Fits the model to the data. |
predict |
Predicts the data for the provided dataset. |
fit_predict |
Fits the model to the subset and predicts the interpolated dataset. |
Notes
TODO: For the moment, this class only supports optimization for one parameter kernels. For this reason, we only have sigma as the parameter to optimize. This sigma refers to the sigma parameter in the Gaussian kernel (but is used for all kernels).
Main reference for sigma optimization: https://link.springer.com/article/10.1023/A:1018975909870
Examples:
>>> import numpy as np
>>> import pandas as pd
>>> from bluemath_tk.interpolation.rbf import RBF
>>> dataset = pd.DataFrame(
... {
... "Hs": np.random.rand(1000) * 7,
... "Tp": np.random.rand(1000) * 20,
... "Dir": np.random.rand(1000) * 360,
... }
... )
>>> subset = dataset.sample(frac=0.25)
>>> target = pd.DataFrame(
... {
... "HsPred": self.subset["Hs"] * 2 + self.subset["Tp"] * 3,
... "DirPred": - self.subset["Dir"],
... }
... )
>>> rbf = RBF()
>>> predictions = rbf.fit_predict(
... subset_data=subset,
... subset_directional_variables=["Dir"],
... target_data=target,
... target_directional_variables=["DirPred"],
... normalize_target_data=True,
... dataset=dataset,
... num_threads=4,
... iteratively_update_sigma=True,
... )
__init__(sigma_min=0.001, sigma_max=0.1, sigma_diff=0.0001, sigma_opt=None, kernel='gaussian', smooth=1e-05)
Raises: |
|
---|
fit(subset_data, target_data, subset_directional_variables=[], target_directional_variables=[], subset_custom_scale_factor={}, normalize_target_data=True, target_custom_scale_factor={}, num_threads=None, iteratively_update_sigma=False)
Fits the model to the data.
Parameters: |
|
---|
Notes
- This function fits the RBF model to the data by:
- Preprocessing the subset and target data.
- Calculating the optimal sigma for the target variables.
- Storing the RBF coefficients and optimal sigmas.
- The number of threads to use for the optimization can be specified.
fit_predict(subset_data, target_data, dataset, subset_directional_variables=[], target_directional_variables=[], subset_custom_scale_factor={}, normalize_target_data=True, target_custom_scale_factor={}, num_threads=None, iteratively_update_sigma=False)
Fits the model to the subset and predicts the interpolated dataset.
Parameters: |
|
---|
Returns: |
|
---|
Notes
- This function fits the model to the subset and predicts the interpolated dataset.
predict(dataset)
Predicts the data for the provided dataset.
Parameters: |
|
---|
Returns: |
|
---|
Raises: |
|
---|
Notes
- This function predicts the data by:
- Reconstructing the data using the fitted coefficients.
- Denormalizing the target data if normalize_target_data is True.
- Calculating the degrees for the target directional variables.
RBFError
Bases: Exception
Custom exception for RBF class.
gaussian_kernel(r, const)
This function calculates the Gaussian kernel for the given distance and constant.
Parameters: |
|
---|
Returns: |
|
---|
Notes
- The Gaussian kernel is defined as: K(r) = exp(r^2 / 2 * const^2) (https://en.wikipedia.org/wiki/Gaussian_function)
- Here, we are assuming the mean is 0.