-
Notifications
You must be signed in to change notification settings - Fork 58
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
Implement scores for FDatairregular
objects as described in #609
#610
Changes from 4 commits
9730d51
951dea3
30c807f
a5b7617
e18bc49
5d3addc
e91419d
4bd8713
ca0a11c
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
from typing_extensions import Literal, Protocol | ||
|
||
from .._utils import nquad_vec | ||
from ..representation import FData, FDataBasis, FDataGrid | ||
from ..representation import FData, FDataBasis, FDataGrid, FDataIrregular | ||
from ..representation._functional_data import EvalPointsType | ||
from ..typing._numpy import NDArrayFloat | ||
|
||
|
@@ -125,6 +125,37 @@ | |
return float(np.mean(score.integrate()[0]) / _domain_measure(score)) | ||
|
||
|
||
def _integral_average_fdatairregular( | ||
score: FDataIrregular, | ||
squared: bool = True, | ||
weights: NDArrayFloat | None = None, | ||
) -> float: | ||
"""Calculate the weighted average of the normalized integrals of the score. | ||
|
||
The integral of the score is normalized because each integral is divided by | ||
the length of the curve's domain. | ||
|
||
If the score is vector-valued, then the mean of each codimension integral | ||
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. Is this what we want? Is what we do for the other types? 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. I understand the question is regarding whether to divide by the length of the curve's domain or by the length of the 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. No, I meant the treatment of vector-valued functions, but you also raised an interesting point that I didn't notice, and maybe we should discuss in the meeting. 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. Answering the initial question, then: yes, for other types we also take the mean of each codimension integral in the case of vector-valued functions. I think it is a reasonable design decision. |
||
is calculated for every functional observation. | ||
|
||
Args: | ||
score: Score of the functions. | ||
squared: If False, the square root is taken. | ||
vnmabus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
weights: Weights for the mean. | ||
""" | ||
if score.dim_domain != 1: | ||
raise ValueError( | ||
"Only univariate FDataIrregular objects are supported", | ||
) | ||
if not squared: | ||
score = np.sqrt(score) | ||
vnmabus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
integrals = np.mean(score.integrate(), axis=1) | ||
lebesgue_measures = np.diff(score.sample_range, axis=-1).reshape(-1) | ||
normalized_integrals = integrals / lebesgue_measures | ||
return np.average(normalized_integrals, weights=weights) | ||
|
||
|
||
@overload | ||
def explained_variance_score( | ||
y_true: DataType, | ||
|
@@ -361,8 +392,9 @@ | |
where :math:`D` is the function domain and :math:`V` the volume of that | ||
domain. | ||
|
||
For :class:`~skfda.representation.FDataBasis` only | ||
'uniform_average' is available. | ||
For :class:`~skfda.representation.FDataBasis` and | ||
:class:`~skfda.representation.FDataIrregular` only 'uniform_average' is | ||
available. | ||
|
||
If :math:`y\_true` and :math:`y\_pred` are numpy arrays, sklearn function | ||
is called. | ||
|
@@ -378,8 +410,10 @@ | |
Mean absolute error. | ||
|
||
If multioutput = 'uniform_average' or | ||
:math:`y\_pred` and :math:`y\_true` are | ||
:class:`~skfda.representation.FDataBasis` objects, float is returned. | ||
:math:`y\_pred` and :math:`y\_true` are both | ||
:class:`~skfda.representation.FDataBasis` or both | ||
:class:`~skfda.representation.FDataIrregular` objects, float is | ||
returned. | ||
|
||
If both :math:`y\_pred` and :math:`y\_true` are | ||
:class:`~skfda.representation.FDataGrid` | ||
|
@@ -412,6 +446,20 @@ | |
return _multioutput_score_grid(error, multioutput) | ||
|
||
|
||
@mean_absolute_error.register # type: ignore[attr-defined, misc] | ||
def _mean_absolute_error_fdatairregular( | ||
y_true: FDataIrregular, | ||
y_pred: FDataIrregular, | ||
*, | ||
sample_weight: Optional[NDArrayFloat] = None, | ||
vnmabus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
multioutput: MultiOutputType = 'uniform_average', | ||
) -> float: | ||
return _integral_average_fdatairregular( | ||
np.abs(y_true - y_pred), | ||
weights=sample_weight, | ||
) | ||
|
||
|
||
@mean_absolute_error.register # type: ignore[attr-defined, misc] | ||
def _mean_absolute_error_fdatabasis( | ||
y_true: FDataBasis, | ||
|
@@ -491,8 +539,9 @@ | |
where :math:`D` is the function domain and :math:`V` the volume of that | ||
domain. | ||
|
||
For :class:`~skfda.representation.FDataBasis` only | ||
'uniform_average' is available. | ||
For :class:`~skfda.representation.FDataBasis` and | ||
:class:`~skfda.representation.FDataIrregular` only 'uniform_average' is | ||
available. | ||
|
||
If :math:`y\_true` and :math:`y\_pred` are numpy arrays, sklearn function | ||
is called. | ||
|
@@ -511,8 +560,10 @@ | |
Mean absolute percentage error. | ||
|
||
If multioutput = 'uniform_average' or | ||
:math:`y\_pred` and :math:`y\_true` are | ||
:class:`~skfda.representation.FDataBasis` objects, float is returned. | ||
:math:`y\_pred` and :math:`y\_true` are both | ||
:class:`~skfda.representation.FDataBasis` or both | ||
:class:`~skfda.representation.FDataIrregular` objects, float is | ||
returned. | ||
|
||
If both :math:`y\_pred` and :math:`y\_true` are | ||
:class:`~skfda.representation.FDataGrid` | ||
|
@@ -554,6 +605,23 @@ | |
return _multioutput_score_grid(error, multioutput) | ||
|
||
|
||
@mean_absolute_percentage_error.register # type: ignore[attr-defined, misc] | ||
def _mean_absolute_percentage_error_fdatairregular( | ||
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. [pep8] reported by reviewdog 🐶 |
||
y_true: FDataIrregular, | ||
y_pred: FDataIrregular, | ||
*, | ||
sample_weight: Optional[NDArrayFloat] = None, | ||
multioutput: MultiOutputType = 'uniform_average', | ||
) -> float: | ||
epsilon = np.finfo(np.float64).eps | ||
|
||
if np.any(np.abs(y_true.values) < epsilon): | ||
warnings.warn('Zero denominator', RuntimeWarning) | ||
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. [pep8] reported by reviewdog 🐶 |
||
|
||
mape = np.abs(y_pred - y_true) / np.maximum(np.abs(y_true), epsilon) | ||
return _integral_average_fdatairregular(mape, weights=sample_weight) | ||
|
||
|
||
@mean_absolute_percentage_error.register # type: ignore[attr-defined, misc] | ||
def _mean_absolute_percentage_error_fdatabasis( | ||
y_true: FDataBasis, | ||
|
@@ -644,8 +712,9 @@ | |
where :math:`D` is the function domain and :math:`V` the volume of that | ||
domain. | ||
|
||
For :class:`~skfda.representation.FDataBasis` only | ||
'uniform_average' is available. | ||
For :class:`~skfda.representation.FDataBasis` and | ||
:class:`~skfda.representation.FDataIrregular` only 'uniform_average' is | ||
available. | ||
|
||
If :math:`y\_true` and :math:`y\_pred` are numpy arrays, sklearn function | ||
is called. | ||
|
@@ -662,8 +731,10 @@ | |
Mean squared error. | ||
|
||
If multioutput = 'uniform_average' or | ||
:math:`y\_pred` and :math:`y\_true` are | ||
:class:`~skfda.representation.FDataBasis` objects, float is returned. | ||
:math:`y\_pred` and :math:`y\_true` are both | ||
:class:`~skfda.representation.FDataBasis` or both | ||
:class:`~skfda.representation.FDataIrregular` objects, float is | ||
returned. | ||
|
||
If both :math:`y\_pred` and :math:`y\_true` are | ||
:class:`~skfda.representation.FDataGrid` | ||
|
@@ -702,6 +773,22 @@ | |
return _multioutput_score_grid(error, multioutput, squared=squared) | ||
|
||
|
||
@mean_squared_error.register # type: ignore[attr-defined, misc] | ||
def _mean_squared_error_fdatairregular( | ||
y_true: FDataIrregular, | ||
y_pred: FDataIrregular, | ||
*, | ||
sample_weight: Optional[NDArrayFloat] = None, | ||
multioutput: MultiOutputType = 'uniform_average', | ||
squared: bool = True, | ||
) -> float: | ||
return _integral_average_fdatairregular( | ||
np.power(y_true - y_pred, 2), | ||
weights=sample_weight, | ||
squared=squared, | ||
) | ||
|
||
|
||
@mean_squared_error.register # type: ignore[attr-defined, misc] | ||
def _mean_squared_error_fdatabasis( | ||
y_true: FDataBasis, | ||
|
@@ -791,8 +878,9 @@ | |
where :math:`D` is the function domain and :math:`V` the volume of that | ||
domain. | ||
|
||
For :class:`~skfda.representation.FDataBasis` only | ||
'uniform_average' is available. | ||
For :class:`~skfda.representation.FDataBasis` and | ||
:class:`~skfda.representation.FDataIrregular` only 'uniform_average' is | ||
available. | ||
|
||
If :math:`y\_true` and :math:`y\_pred` are numpy arrays, sklearn function | ||
is called. | ||
|
@@ -812,8 +900,10 @@ | |
Mean squared log error. | ||
|
||
If multioutput = 'uniform_average' or | ||
:math:`y\_pred` and :math:`y\_true` are | ||
:class:`~skfda.representation.FDataBasis` objects, float is returned. | ||
:math:`y\_pred` and :math:`y\_true` are both | ||
:class:`~skfda.representation.FDataBasis` or both | ||
:class:`~skfda.representation.FDataIrregular` objects, float is | ||
returned. | ||
|
||
If both :math:`y\_pred` and :math:`y\_true` are | ||
:class:`~skfda.representation.FDataGrid` | ||
|
@@ -860,6 +950,30 @@ | |
) | ||
|
||
|
||
@mean_squared_log_error.register # type: ignore[attr-defined, misc] | ||
def _mean_squared_log_error_fdatairregular( | ||
y_true: FDataIrregular, | ||
y_pred: FDataIrregular, | ||
*, | ||
sample_weight: Optional[NDArrayFloat] = None, | ||
multioutput: MultiOutputType = 'uniform_average', | ||
squared: bool = True, | ||
) -> float: | ||
if np.any(y_true.values < 0) or np.any(y_pred.values < 0): | ||
raise ValueError( | ||
"Mean Squared Logarithmic Error cannot be used when " | ||
"targets functions have negative values.", | ||
) | ||
|
||
return mean_squared_error( | ||
np.log1p(y_true), | ||
np.log1p(y_pred), | ||
sample_weight=sample_weight, | ||
multioutput=multioutput, | ||
squared=squared, | ||
) | ||
|
||
|
||
@mean_squared_log_error.register # type: ignore[attr-defined, misc] | ||
def _mean_squared_log_error_fdatabasis( | ||
y_true: FDataBasis, | ||
|
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.
[pep8] reported by reviewdog 🐶
DAR201 Missing "Returns" in Docstring: - return