From 73cddd3963ab7db32d1afb86fc439a3437da689b Mon Sep 17 00:00:00 2001 From: "Kruglov, Oleg" Date: Tue, 3 Sep 2024 11:04:42 -0700 Subject: [PATCH] Address comments --- .../linear_model/incremental_linear_model.py | 21 +++++++++---------- .../linear_model/incremental_linear_model.py | 7 ++++--- .../tests/test_incremental_linear.py | 8 +++---- .../tests/test_incremental_linear_spmd.py | 10 +++++---- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/onedal/linear_model/incremental_linear_model.py b/onedal/linear_model/incremental_linear_model.py index a3a6461f55..f0558ad973 100644 --- a/onedal/linear_model/incremental_linear_model.py +++ b/onedal/linear_model/incremental_linear_model.py @@ -83,10 +83,11 @@ def partial_fit(self, X, y, queue=None): self._dtype = get_dtype(X) self._params = self._get_onedal_params(self._dtype) - y = np.asarray(y).astype(dtype=self._dtype) - self._y_ndim_1 = y.ndim == 1 + y = np.asarray(y, dtype=self._dtype) - X, y = _check_X_y(X, y, dtype=[np.float64, np.float32], accept_2d_y=True) + X, y = _check_X_y( + X, y, dtype=[np.float64, np.float32], accept_2d_y=True, force_all_finite=False + ) self.n_features_in_ = _num_features(X, fallback_1d=True) X_table, y_table = to_table(X, y) @@ -139,14 +140,10 @@ def finalize_fit(self, queue=None): packed_coefficients = from_table(result.model.packed_coefficients) self.coef_, self.intercept_ = ( - packed_coefficients[:, 1:], - packed_coefficients[:, 0], + packed_coefficients[:, 1:].squeeze(), + packed_coefficients[:, 0].squeeze(), ) - if self.coef_.shape[0] == 1 and self._y_ndim_1: - self.coef_ = self.coef_.ravel() - self.intercept_ = self.intercept_[0] - return self @@ -216,9 +213,11 @@ def partial_fit(self, X, y, queue=None): self._dtype = get_dtype(X) self._params = self._get_onedal_params(self._dtype) - y = np.asarray(y).astype(dtype=self._dtype) + y = np.asarray(y, dtype=self._dtype) - X, y = _check_X_y(X, y, dtype=[np.float64, np.float32], accept_2d_y=True) + X, y = _check_X_y( + X, y, dtype=[np.float64, np.float32], accept_2d_y=True, force_all_finite=False + ) self.n_features_in_ = _num_features(X, fallback_1d=True) X_table, y_table = to_table(X, y) diff --git a/onedal/spmd/linear_model/incremental_linear_model.py b/onedal/spmd/linear_model/incremental_linear_model.py index 5fc268d446..b430764101 100644 --- a/onedal/spmd/linear_model/incremental_linear_model.py +++ b/onedal/spmd/linear_model/incremental_linear_model.py @@ -67,10 +67,11 @@ def partial_fit(self, X, y, queue=None): self._dtype = get_dtype(X) self._params = self._get_onedal_params(self._dtype) - y = np.asarray(y).astype(dtype=self._dtype) - self._y_ndim_1 = y.ndim == 1 + y = np.asarray(y, dtype=self._dtype) - X, y = _check_X_y(X, y, dtype=[np.float64, np.float32], accept_2d_y=True) + X, y = _check_X_y( + X, y, dtype=[np.float64, np.float32], accept_2d_y=True, force_all_finite=False + ) self.n_features_in_ = _num_features(X, fallback_1d=True) X_table, y_table = to_table(X, y) diff --git a/sklearnex/linear_model/tests/test_incremental_linear.py b/sklearnex/linear_model/tests/test_incremental_linear.py index 296c714ead..7ef2206321 100644 --- a/sklearnex/linear_model/tests/test_incremental_linear.py +++ b/sklearnex/linear_model/tests/test_incremental_linear.py @@ -34,7 +34,7 @@ def test_sklearnex_fit_on_gold_data(dataframe, queue, fit_intercept, macro_block X = np.array([[1], [2]]) X = X.astype(dtype=dtype) X_df = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe) - y = np.array([1, 2]) + y = np.array([[1], [2]]) y = y.astype(dtype=dtype) y_df = _convert_to_dataframe(y, sycl_queue=queue, target_df=dataframe) @@ -185,16 +185,16 @@ def test_sklearnex_partial_fit_on_random_data( inclin.partial_fit(X_split_df, y_split_df) tol = 1e-4 if inclin.coef_.dtype == np.float32 else 1e-7 - assert_allclose(coef, inclin.coef_.T, atol=tol) + assert_allclose(coef.T.squeeze(), inclin.coef_, atol=tol) if fit_intercept: assert_allclose(intercept, inclin.intercept_, atol=tol) X_test = gen.random(size=(num_samples, num_features), dtype=dtype) if fit_intercept: - expected_y_pred = X_test @ coef + intercept[np.newaxis, :] + expected_y_pred = (X_test @ coef + intercept[np.newaxis, :]).squeeze() else: - expected_y_pred = X_test @ coef + expected_y_pred = (X_test @ coef).squeeze() X_test_df = _convert_to_dataframe(X_test, sycl_queue=queue, target_df=dataframe) diff --git a/sklearnex/spmd/linear_model/tests/test_incremental_linear_spmd.py b/sklearnex/spmd/linear_model/tests/test_incremental_linear_spmd.py index d36d146868..240bab2191 100644 --- a/sklearnex/spmd/linear_model/tests/test_incremental_linear_spmd.py +++ b/sklearnex/spmd/linear_model/tests/test_incremental_linear_spmd.py @@ -71,8 +71,9 @@ def test_incremental_linear_regression_fit_spmd_gold( [13.0, 32.0], [14.0, 64.0], [15.0, 128.0], - ] - ).astype(dtype=dtype) + ], + dtype=dtype, + ) dpt_X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe) local_X = _get_local_tensor(X) local_dpt_X = _convert_to_dataframe(local_X, sycl_queue=queue, target_df=dataframe) @@ -143,8 +144,9 @@ def test_incremental_linear_regression_partial_fit_spmd_gold( [13.0, 32.0], [14.0, 64.0], [15.0, 128.0], - ] - ).astype(dtype=dtype) + ], + dtype=dtype, + ) dpt_X = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe) local_X = _get_local_tensor(X) split_local_X = np.array_split(local_X, num_blocks)