From 1582445aedfd190bb3f0bc8efb688b914d74b295 Mon Sep 17 00:00:00 2001 From: VNMabus Date: Fri, 21 Jun 2024 22:21:13 +0200 Subject: [PATCH 1/3] Change np.float_ for np.float64. --- .../dim_reduction/variable_selection/_rkvs.py | 2 +- .../variable_selection/maxima_hunting.py | 5 ++-- .../dim_reduction/variable_selection/mrmr.py | 28 +++++++++---------- skfda/tests/test_interpolation.py | 2 +- skfda/tests/test_kernel_regression.py | 18 ++++++------ skfda/tests/test_math.py | 26 ++++++++--------- skfda/tests/test_neighbors.py | 18 ++++++------ skfda/tests/test_pandas_fdatabasis.py | 2 +- skfda/tests/test_pandas_fdatagrid.py | 2 +- skfda/tests/test_per_class_transformer.py | 4 +-- skfda/tests/test_recursive_maxima_hunting.py | 8 +++--- skfda/tests/test_regression.py | 2 +- skfda/tests/test_regularization.py | 6 ++-- skfda/tests/test_smoothing.py | 2 +- skfda/typing/_numpy.py | 4 +-- 15 files changed, 64 insertions(+), 65 deletions(-) diff --git a/skfda/preprocessing/dim_reduction/variable_selection/_rkvs.py b/skfda/preprocessing/dim_reduction/variable_selection/_rkvs.py index 298a68fea..925780015 100644 --- a/skfda/preprocessing/dim_reduction/variable_selection/_rkvs.py +++ b/skfda/preprocessing/dim_reduction/variable_selection/_rkvs.py @@ -75,7 +75,7 @@ def _rkhs_vs( indexes = np.delete(indexes, selected_features[0]) for i in range(1, n_features_to_select): - aux = np.zeros_like(indexes, dtype=np.float_) + aux = np.zeros_like(indexes, dtype=np.float64) for j in range(0, indexes.shape[0]): new_selection = np.concatenate([ diff --git a/skfda/preprocessing/dim_reduction/variable_selection/maxima_hunting.py b/skfda/preprocessing/dim_reduction/variable_selection/maxima_hunting.py index bbac46510..989723908 100644 --- a/skfda/preprocessing/dim_reduction/variable_selection/maxima_hunting.py +++ b/skfda/preprocessing/dim_reduction/variable_selection/maxima_hunting.py @@ -6,9 +6,8 @@ import numpy as np import scipy.signal import sklearn.utils -from sklearn.base import clone - from dcor import u_distance_correlation_sqr +from sklearn.base import clone from ...._utils._sklearn_adapter import ( BaseEstimator, @@ -232,7 +231,7 @@ def fit( # type: ignore[override] # noqa: D102 self.features_shape_ = X.data_matrix.shape[1:] self.dependence_ = _compute_dependence( X, - y.astype(np.float_), + y.astype(np.float64), dependence_measure=self.dependence_measure, ) diff --git a/skfda/preprocessing/dim_reduction/variable_selection/mrmr.py b/skfda/preprocessing/dim_reduction/variable_selection/mrmr.py index 036663ccf..55ad2670a 100644 --- a/skfda/preprocessing/dim_reduction/variable_selection/mrmr.py +++ b/skfda/preprocessing/dim_reduction/variable_selection/mrmr.py @@ -41,10 +41,10 @@ bound="MinimumRedundancyMaximumRelevance[Any]", ) -dtype_X_T = TypeVar("dtype_X_T", bound=np.float_, covariant=True) +dtype_X_T = TypeVar("dtype_X_T", bound=np.float64, covariant=True) dtype_y_T = TypeVar( "dtype_y_T", - bound=Union[np.int_, np.float_], + bound=Union[np.int_, np.float64], covariant=True, ) @@ -108,7 +108,7 @@ def mutual_information( MethodName = Literal["MID", "MIQ"] -def _parse_method(name: MethodName) -> Method[Union[np.int_, np.float_]]: +def _parse_method(name: MethodName) -> Method[Union[np.int_, np.float64]]: if name == "MID": return MID elif name == "MIQ": @@ -336,8 +336,8 @@ def __init__( *, n_features_to_select: int = 1, dependence_measure: _DependenceMeasure[ - np.typing.NDArray[np.float_], - np.typing.NDArray[np.float_ | dtype_y_T], + np.typing.NDArray[np.float64], + np.typing.NDArray[np.float64 | dtype_y_T], ], criterion: _CriterionLike, ) -> None: @@ -349,12 +349,12 @@ def __init__( *, n_features_to_select: int = 1, relevance_dependence_measure: _DependenceMeasure[ - np.typing.NDArray[np.float_], + np.typing.NDArray[np.float64], np.typing.NDArray[dtype_y_T], ], redundancy_dependence_measure: _DependenceMeasure[ - np.typing.NDArray[np.float_], - np.typing.NDArray[np.float_], + np.typing.NDArray[np.float64], + np.typing.NDArray[np.float64], ], criterion: _CriterionLike, ) -> None: @@ -366,16 +366,16 @@ def __init__( n_features_to_select: int = 1, method: Method[dtype_y_T] | MethodName | None = None, dependence_measure: _DependenceMeasure[ - np.typing.NDArray[np.float_], - np.typing.NDArray[np.float_ | dtype_y_T], + np.typing.NDArray[np.float64], + np.typing.NDArray[np.float64 | dtype_y_T], ] | None = None, relevance_dependence_measure: _DependenceMeasure[ - np.typing.NDArray[np.float_], + np.typing.NDArray[np.float64], np.typing.NDArray[dtype_y_T], ] | None = None, redundancy_dependence_measure: _DependenceMeasure[ - np.typing.NDArray[np.float_], - np.typing.NDArray[np.float_], + np.typing.NDArray[np.float64], + np.typing.NDArray[np.float64], ] | None = None, criterion: _CriterionLike | None = None, ) -> None: @@ -417,7 +417,7 @@ def _validate_parameters(self) -> None: ) self.relevance_dependence_measure_: _DependenceMeasure[ - np.typing.NDArray[np.float_], + np.typing.NDArray[np.float64], np.typing.NDArray[dtype_y_T], ] = ( method.relevance_dependence_measure diff --git a/skfda/tests/test_interpolation.py b/skfda/tests/test_interpolation.py index 5bac5d1a3..ad2829617 100644 --- a/skfda/tests/test_interpolation.py +++ b/skfda/tests/test_interpolation.py @@ -401,7 +401,7 @@ def test_evaluation_nodes(self) -> None: @np.vectorize def _coordinate_function( *args: float, -) -> np.typing.NDArray[np.float_]: +) -> np.typing.NDArray[np.float64]: _, *domain_indexes, _ = args return np.sum(domain_indexes) # type: ignore[no-any-return] diff --git a/skfda/tests/test_kernel_regression.py b/skfda/tests/test_kernel_regression.py index dd3fbee2b..d2c8f44b7 100644 --- a/skfda/tests/test_kernel_regression.py +++ b/skfda/tests/test_kernel_regression.py @@ -18,7 +18,7 @@ from skfda.representation.basis import FDataBasis, FourierBasis, MonomialBasis from skfda.representation.grid import FDataGrid -FloatArray = np.typing.NDArray[np.float_] +FloatArray = np.typing.NDArray[np.float64] def _nw_alt( @@ -151,7 +151,7 @@ def test_nadaraya_watson(self) -> None: fd_train_grid, fd_test_grid, y_train_grid = _create_data_grid() # Test NW method with basis representation and bandwidth=1 - nw_basis = KernelRegression[FDataBasis, np.typing.NDArray[np.float_]]( + nw_basis = KernelRegression[FDataBasis, np.typing.NDArray[np.float64]]( kernel_estimator=NadarayaWatsonHatMatrix(bandwidth=1), ) nw_basis.fit(fd_train_basis, y_train_basis) @@ -168,7 +168,7 @@ def test_nadaraya_watson(self) -> None: ) # Test NW method with grid representation and bandwidth=1 - nw_grid = KernelRegression[FDataGrid, np.typing.NDArray[np.float_]]( + nw_grid = KernelRegression[FDataGrid, np.typing.NDArray[np.float64]]( kernel_estimator=NadarayaWatsonHatMatrix(bandwidth=1), ) nw_grid.fit(fd_train_grid, y_train_grid) @@ -192,7 +192,7 @@ def test_knn(self) -> None: # Test KNN method with basis representation, n_neighbours=3 and # uniform kernel - knn_basis = KernelRegression[FDataBasis, np.typing.NDArray[np.float_]]( + knn_basis = KernelRegression[FDataBasis, np.typing.NDArray[np.float64]]( kernel_estimator=KNeighborsHatMatrix(n_neighbors=3), ) knn_basis.fit(fd_train_basis, y_train_basis) @@ -210,7 +210,7 @@ def test_knn(self) -> None: # Test KNN method with grid representation, n_neighbours=3 and # uniform kernel - knn_grid = KernelRegression[FDataGrid, np.typing.NDArray[np.float_]]( + knn_grid = KernelRegression[FDataGrid, np.typing.NDArray[np.float64]]( kernel_estimator=KNeighborsHatMatrix(n_neighbors=3), ) knn_grid.fit(fd_train_grid, y_train_grid) @@ -253,7 +253,7 @@ def test_llr(self) -> None: # Creating data fd_train_basis, fd_test_basis, y_train_basis = _create_data_basis() - llr_basis = KernelRegression[FDataBasis, np.typing.NDArray[np.float_]]( + llr_basis = KernelRegression[FDataBasis, np.typing.NDArray[np.float64]]( kernel_estimator=LocalLinearRegressionHatMatrix(bandwidth=1), ) llr_basis.fit(fd_train_basis, y_train_basis) @@ -273,7 +273,7 @@ def test_nw_r(self) -> None: """Comparison of NW's results with results from fda.usc.""" X_train, X_test, y_train = _create_data_r() - nw = KernelRegression[FDataGrid, np.typing.NDArray[np.float_]]( + nw = KernelRegression[FDataGrid, np.typing.NDArray[np.float64]]( kernel_estimator=NadarayaWatsonHatMatrix(bandwidth=1), ) nw.fit(X_train, y_train) @@ -298,7 +298,7 @@ def test_knn_r(self) -> None: """Comparison of NW's results with results from fda.usc.""" X_train, X_test, y_train = _create_data_r() - knn = KernelRegression[FDataGrid, np.typing.NDArray[np.float_]]( + knn = KernelRegression[FDataGrid, np.typing.NDArray[np.float64]]( kernel_estimator=KNeighborsHatMatrix(n_neighbors=3), ) knn.fit(X_train, y_train) @@ -337,7 +337,7 @@ def test_llr_non_orthonormal(self) -> None: bandwidth=100, kernel=uniform, ) - kr = KernelRegression[FDataBasis, np.typing.NDArray[np.float_]]( + kr = KernelRegression[FDataBasis, np.typing.NDArray[np.float64]]( kernel_estimator=llr, ) kr.fit(X_train, y_train) diff --git a/skfda/tests/test_math.py b/skfda/tests/test_math.py index e9e465e64..9b43a2022 100644 --- a/skfda/tests/test_math.py +++ b/skfda/tests/test_math.py @@ -16,8 +16,8 @@ def _ndm( - *args: np.typing.NDArray[np.float_], -) -> Sequence[np.typing.NDArray[np.float_]]: + *args: np.typing.NDArray[np.float64], +) -> Sequence[np.typing.NDArray[np.float64]]: return [ x[(None,) * i + (slice(None),) + (None,) * (len(args) - i - 1)] for i, x in enumerate(args) @@ -30,10 +30,10 @@ class InnerProductTest(unittest.TestCase): def test_several_variables(self) -> None: """Test inner_product with functions of several variables.""" def f( # noqa: WPS430 - x: np.typing.NDArray[np.float_], - y: np.typing.NDArray[np.float_], - z: np.typing.NDArray[np.float_], - ) -> np.typing.NDArray[np.float_]: + x: np.typing.NDArray[np.float64], + y: np.typing.NDArray[np.float64], + z: np.typing.NDArray[np.float64], + ) -> np.typing.NDArray[np.float64]: return x * y * z t = np.linspace(0, 1, 30) @@ -73,9 +73,9 @@ def f( # noqa: WPS430 def test_mixed_several_variables(self) -> None: """Test inner_product with basis and grid multivariable functions.""" def f( # noqa: WPS430 - x: np.typing.NDArray[np.float_], - y: np.typing.NDArray[np.float_], - ) -> np.typing.NDArray[np.float_]: + x: np.typing.NDArray[np.float64], + y: np.typing.NDArray[np.float64], + ) -> np.typing.NDArray[np.float64]: return x + y t = np.linspace(0, 1, 50) @@ -103,13 +103,13 @@ def f( # noqa: WPS430 def test_vector_valued(self) -> None: """Test inner_product with vector valued functions.""" def f( # noqa: WPS430 - x: np.typing.NDArray[np.float_], - ) -> np.typing.NDArray[np.float_]: + x: np.typing.NDArray[np.float64], + ) -> np.typing.NDArray[np.float64]: return x**2 def g( # noqa: WPS430 - y: np.typing.NDArray[np.float_], - ) -> np.typing.NDArray[np.float_]: + y: np.typing.NDArray[np.float64], + ) -> np.typing.NDArray[np.float64]: return 3 * y t = np.linspace(0, 1, 100) diff --git a/skfda/tests/test_neighbors.py b/skfda/tests/test_neighbors.py index 705bd3d9b..a98697117 100644 --- a/skfda/tests/test_neighbors.py +++ b/skfda/tests/test_neighbors.py @@ -134,12 +134,12 @@ def test_predict_regressor(self) -> None: """Test scalar regression, predicts mode location.""" # Dummy test, with weight = distance, only the sample with distance 0 # will be returned, obtaining the exact location - knnr = KNeighborsRegressor[FDataGrid, np.typing.NDArray[np.float_]]( + knnr = KNeighborsRegressor[FDataGrid, np.typing.NDArray[np.float64]]( weights='distance', ) rnnr = RadiusNeighborsRegressor[ FDataGrid, - np.typing.NDArray[np.float_], + np.typing.NDArray[np.float64], ]( weights='distance', radius=0.1, @@ -168,7 +168,7 @@ def test_kneighbors(self) -> None: knn = KNeighborsClassifier() knn.fit(self.X, self.y) - knnr = KNeighborsRegressor[FDataGrid, np.typing.NDArray[np.float_]]() + knnr = KNeighborsRegressor[FDataGrid, np.typing.NDArray[np.float64]]() knnr.fit(self.X, self.modes_location) neigh: KNeighborsMixin[FDataGrid, Any] @@ -201,7 +201,7 @@ def test_radius_neighbors(self) -> None: knnr = RadiusNeighborsRegressor[ FDataGrid, - np.typing.NDArray[np.float_], + np.typing.NDArray[np.float64], ](radius=0.1) knnr.fit(self.X, self.modes_location) @@ -241,7 +241,7 @@ def test_knn_functional_response_precomputed(self) -> None: """Test that precomputed distances work for functional response.""" # Non-precomputed knnr = KNeighborsRegressor[ - np.typing.NDArray[np.float_], + np.typing.NDArray[np.float64], FDataGrid, ]( weights='distance', @@ -252,7 +252,7 @@ def test_knn_functional_response_precomputed(self) -> None: # Precomputed knnr_pre = KNeighborsRegressor[ - np.typing.NDArray[np.float_], + np.typing.NDArray[np.float64], FDataGrid, ]( weights='distance', @@ -395,7 +395,7 @@ def test_score_scalar_response(self) -> None: """Test regression with scalar response.""" neigh = KNeighborsRegressor[ FDataGrid, - np.typing.NDArray[np.float_], + np.typing.NDArray[np.float64], ]() neigh.fit(self.X, self.modes_location) @@ -522,8 +522,8 @@ def test_lof_exceptions(self) -> None: def _weights( self, - weights: np.typing.NDArray[np.float_], - ) -> np.typing.NDArray[np.float_]: + weights: np.typing.NDArray[np.float64], + ) -> np.typing.NDArray[np.float64]: return np.array([w == np.min(weights) for w in weights], dtype=float) diff --git a/skfda/tests/test_pandas_fdatabasis.py b/skfda/tests/test_pandas_fdatabasis.py index 96be90119..32c735dbb 100644 --- a/skfda/tests/test_pandas_fdatabasis.py +++ b/skfda/tests/test_pandas_fdatabasis.py @@ -60,7 +60,7 @@ def data_missing(basis: Basis) -> FDataBasis: """Length-2 array with [NA, Valid].""" coef_matrix = np.arange( 2 * basis.n_basis, - dtype=np.float_, + dtype=np.float64, ).reshape(2, basis.n_basis) coef_matrix[0, :] = np.NaN diff --git a/skfda/tests/test_pandas_fdatagrid.py b/skfda/tests/test_pandas_fdatagrid.py index 80818a1b3..00dde5c6a 100644 --- a/skfda/tests/test_pandas_fdatagrid.py +++ b/skfda/tests/test_pandas_fdatagrid.py @@ -66,7 +66,7 @@ def data_missing() -> ExtensionArray: """Return a length-2 array with [NA, Valid].""" data_matrix = np.arange( 2 * 10 * 10 * 3, - dtype=np.float_, + dtype=np.float64, ).reshape(2, 10, 10, 3) data_matrix[0, ...] = np.NaN grid_points = [ diff --git a/skfda/tests/test_per_class_transformer.py b/skfda/tests/test_per_class_transformer.py index 4e3cd73d0..f5dab101d 100644 --- a/skfda/tests/test_per_class_transformer.py +++ b/skfda/tests/test_per_class_transformer.py @@ -25,7 +25,7 @@ def setUp(self) -> None: def test_transform(self) -> None: """Check the data transformation is done correctly.""" - t = PerClassTransformer[FDataGrid, np.typing.NDArray[np.float_]]( + t = PerClassTransformer[FDataGrid, np.typing.NDArray[np.float64]]( RecursiveMaximaHunting(), array_output=True, ) @@ -46,7 +46,7 @@ def test_transform(self) -> None: def test_not_transformer_argument(self) -> None: """Check that invalid arguments in fit raise exception.""" - t = PerClassTransformer[FDataGrid, np.typing.NDArray[np.float_]]( + t = PerClassTransformer[FDataGrid, np.typing.NDArray[np.float64]]( KNeighborsClassifier(), ) self.assertRaises( diff --git a/skfda/tests/test_recursive_maxima_hunting.py b/skfda/tests/test_recursive_maxima_hunting.py index d781a69b9..0475f796a 100644 --- a/skfda/tests/test_recursive_maxima_hunting.py +++ b/skfda/tests/test_recursive_maxima_hunting.py @@ -29,8 +29,8 @@ def test_gaussian_homoscedastic(self) -> None: n_features = 101 def mean_1( # noqa: WPS430 - t: np.typing.NDArray[np.float_], - ) -> np.typing.NDArray[np.float_]: + t: np.typing.NDArray[np.float64], + ) -> np.typing.NDArray[np.float64]: return ( # type: ignore[no-any-return] np.abs(t - 0.25) @@ -86,8 +86,8 @@ def test_fit_exponential(self) -> None: n_features = 101 def mean_1( # noqa: WPS430 - t: np.typing.NDArray[np.float_], - ) -> np.typing.NDArray[np.float_]: + t: np.typing.NDArray[np.float64], + ) -> np.typing.NDArray[np.float64]: return ( # type: ignore[no-any-return] np.abs(t - 0.25) diff --git a/skfda/tests/test_regression.py b/skfda/tests/test_regression.py index a8270fd8c..3e1ff98a8 100644 --- a/skfda/tests/test_regression.py +++ b/skfda/tests/test_regression.py @@ -318,7 +318,7 @@ def test_mixed_regularization(self) -> None: ]) X: Sequence[ - np.typing.NDArray[np.float_] | FDataBasis, + np.typing.NDArray[np.float64] | FDataBasis, ] = [multivariate, x_fd] # y = 2 + sum([3, 1] * array) + int(3 * function) # noqa: E800 diff --git a/skfda/tests/test_regularization.py b/skfda/tests/test_regularization.py index fa3d4d717..ecbf99dd4 100644 --- a/skfda/tests/test_regularization.py +++ b/skfda/tests/test_regularization.py @@ -36,8 +36,8 @@ Union[ float, Callable[ - [np.typing.NDArray[np.float_]], - np.typing.NDArray[np.float_], + [np.typing.NDArray[np.float64]], + np.typing.NDArray[np.float64], ], ], ], @@ -53,7 +53,7 @@ def _test_penalty( basis: Basis, linear_diff_op: LinearDifferentialOperatorInput, atol: float = 0, - result: Optional[np.typing.NDArray[np.float_]] = None, + result: Optional[np.typing.NDArray[np.float64]] = None, ) -> None: operator = LinearDifferentialOperator(linear_diff_op) diff --git a/skfda/tests/test_smoothing.py b/skfda/tests/test_smoothing.py index 315d2b3a5..d44f7e988 100644 --- a/skfda/tests/test_smoothing.py +++ b/skfda/tests/test_smoothing.py @@ -122,7 +122,7 @@ class TestKernelSmoother(unittest.TestCase): def _test_hat_matrix( self, kernel_estimator: HatMatrix, - ) -> np.typing.NDArray[np.float_]: + ) -> np.typing.NDArray[np.float64]: return KernelSmoother( # noqa: WPS437 kernel_estimator=kernel_estimator, )._hat_matrix( diff --git a/skfda/typing/_numpy.py b/skfda/typing/_numpy.py index b870c2bc4..82c4dc678 100644 --- a/skfda/typing/_numpy.py +++ b/skfda/typing/_numpy.py @@ -14,8 +14,8 @@ from numpy.typing import NDArray NDArrayAny = NDArray[Any] NDArrayInt = NDArray[np.int_] - NDArrayFloat = NDArray[np.float_] - NDArrayReal = NDArray[Union[np.float_, np.int_]] + NDArrayFloat = NDArray[np.float64] + NDArrayReal = NDArray[Union[np.float64, np.int_]] NDArrayBool = NDArray[np.bool_] NDArrayStr = NDArray[np.str_] NDArrayObject = NDArray[np.object_] From 5a7b53d19d83acef625b9e8075d9fdd11bfe77db Mon Sep 17 00:00:00 2001 From: VNMabus Date: Sat, 22 Jun 2024 00:44:03 +0200 Subject: [PATCH 2/3] Restrict to NumPy version less than 2 for now. --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index c7fa0ed5a..c7aac9a10 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,7 +37,7 @@ dependencies = [ "lazy_loader", "matplotlib", "multimethod>=1.5, !=1.11, != 1.11.1", - "numpy>=1.16", + "numpy>=1.16,<2", "pandas>=1.0", "rdata", "scikit-datasets[cran]>=0.2.2", From 6d2ce186e4047a9e178a4b0cff63c1a9325d96f0 Mon Sep 17 00:00:00 2001 From: VNMabus Date: Sat, 22 Jun 2024 01:22:54 +0200 Subject: [PATCH 3/3] Enable animations in examples. --- docs/conf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/conf.py b/docs/conf.py index e25650181..a1ffd43f9 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -412,6 +412,7 @@ def __repr__(self) -> str: "dependencies": ["../binder/requirements.txt"], "notebooks_dir": "../examples", }, + "matplotlib_animations": True, } warnings.filterwarnings(