From 835a3f65eb57dccc9bd4a4c1e1b8d22ab2b97c8c Mon Sep 17 00:00:00 2001 From: lennybronner Date: Tue, 24 Sep 2024 21:12:03 -0400 Subject: [PATCH] ran linter --- src/elexsolver/LinearSolver.py | 32 ++++++++++++++++++---- src/elexsolver/OLSRegressionSolver.py | 12 ++++++-- src/elexsolver/QuantileRegressionSolver.py | 15 ++++------ tests/test_ols.py | 4 +-- tests/test_quantile.py | 6 ++-- 5 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/elexsolver/LinearSolver.py b/src/elexsolver/LinearSolver.py index 4ae29b9..fe39321 100644 --- a/src/elexsolver/LinearSolver.py +++ b/src/elexsolver/LinearSolver.py @@ -32,7 +32,15 @@ def __init__(self): self.rng = np.random.default_rng(seed=0) @classmethod - def fit(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = None, lambda_: float = 0.0, cache: bool = True, **kwargs): + def fit( + self, + x: np.ndarray, + y: np.ndarray, + weights: np.ndarray | None = None, + lambda_: float = 0.0, + cache: bool = True, + **kwargs, + ): """ Fits model """ @@ -81,7 +89,15 @@ def _check_intercept(self, x): if ~np.all(x[:, 0] == 1): warnings.warn("Warning: fit_intercept=True and not all elements of the first columns are 1s") - def residuals(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = None, K: int | None = None, center: bool = True, **kwargs) -> np.ndarray: + def residuals( + self, + x: np.ndarray, + y: np.ndarray, + weights: np.ndarray | None = None, + K: int | None = None, + center: bool = True, + **kwargs, + ) -> np.ndarray: """ Computes residuals for the model """ @@ -98,7 +114,7 @@ def residuals(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = N # shuffle for random order of datapoints self.rng.shuffle(indices) x_shuffled, y_shuffled, weights_shuffled = x[indices], y[indices], weights[indices] - + # create folds x_folds = np.array_split(x_shuffled, K) y_folds = np.array_split(y_shuffled, K) @@ -107,8 +123,12 @@ def residuals(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = N residuals = [] for k in range(K): # extract test points - x_test, y_test, weights_test, = x_folds[k], y_folds[k], weights_folds[k] - + x_test, y_test, _, = ( + x_folds[k], + y_folds[k], + weights_folds[k], + ) + # extract training points x_train = np.concatenate([x_folds[j] for j in range(K) if j != k]) y_train = np.concatenate([y_folds[j] for j in range(K) if j != k]) @@ -121,7 +141,7 @@ def residuals(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = N # k-th residuals residuals_k = y_test - y_hat_k residuals.append(residuals_k) - + residuals = np.concatenate(residuals) # undo shuffling of residuals, to put them in the original dataset order residuals = residuals[np.argsort(indices)] diff --git a/src/elexsolver/OLSRegressionSolver.py b/src/elexsolver/OLSRegressionSolver.py index de06649..68d6820 100644 --- a/src/elexsolver/OLSRegressionSolver.py +++ b/src/elexsolver/OLSRegressionSolver.py @@ -92,7 +92,7 @@ def fit( fit_intercept: bool = True, regularize_intercept: bool = False, n_feat_ignore_reg: int = 0, - cache: bool = True + cache: bool = True, ): self._check_any_element_nan_or_inf(x) self._check_any_element_nan_or_inf(y) @@ -128,7 +128,15 @@ def fit( return coefficients - def residuals(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray | None = None, K: int | None = None, center: bool = True, **kwargs) -> np.ndarray: + def residuals( + self, + x: np.ndarray, + y: np.ndarray, + weights: np.ndarray | None = None, + K: int | None = None, + center: bool = True, + **kwargs + ) -> np.ndarray: if K == x.shape[0]: # compute standard residuals y_hat = self.predict(x) diff --git a/src/elexsolver/QuantileRegressionSolver.py b/src/elexsolver/QuantileRegressionSolver.py index 91665ee..ce4476e 100644 --- a/src/elexsolver/QuantileRegressionSolver.py +++ b/src/elexsolver/QuantileRegressionSolver.py @@ -30,10 +30,7 @@ def _fit(self, x: np.ndarray, y: np.ndarray, weights: np.ndarray, tau: float) -> # A_eq are the equality constraint matrix # b_eq is the equality constraint vector (ie. A_eq @ x = b_eq) # bounds are the (min, max) possible values of every element of x - try: - res = linprog(-1 * S, A_eq=Phi.T, b_eq=zeros, bounds=bounds, method="highs", options={"presolve": False}) - except: - import pdb; pdb.set_trace() + res = linprog(-1 * S, A_eq=Phi.T, b_eq=zeros, bounds=bounds, method="highs", options={"presolve": False}) # marginal are the dual values, since we are solving the dual this is equivalent to the primal return -1 * res.eqlin.marginals @@ -88,7 +85,7 @@ def fit( regularize_intercept: bool = False, n_feat_ignore_reg: int = 0, normalize_weights: bool = True, - cache: bool = True + cache: bool = True, ): """ Fits quantile regression @@ -110,14 +107,14 @@ def fit( raise ZeroDivisionError weights = weights / weights_sum - if y.ndim == 1: # code expects 2-dim array - y = y.reshape(-1,1) + if y.ndim == 1: # code expects 2-dim array + y = y.reshape(-1, 1) # _fit assumes that taus is list, so if we want to do one value of tau then turn into a list if isinstance(taus, float): taus = [taus] else: - assert y.shape[1] == 1 # you can either have multiple taus or multiple ys + assert y.shape[1] == 1 # you can either have multiple taus or multiple ys coefficients_array = [] for tau in taus: for y_arr in y.T: @@ -146,4 +143,4 @@ def predict(self, x: np.ndarray, coefficients: np.ndarray | None = None) -> np.n else: preds = x @ coefficients - return preds \ No newline at end of file + return preds diff --git a/tests/test_ols.py b/tests/test_ols.py index e399991..bea6876 100644 --- a/tests/test_ols.py +++ b/tests/test_ols.py @@ -209,7 +209,7 @@ def test_residuals_no_weights(random_data_no_weights): x = random_data_no_weights[["x0", "x1", "x2", "x3", "x4"]].values y = random_data_no_weights["y"].values.reshape(-1, 1) lm.fit(x, y, fit_intercept=False) - predictions = lm.predict(x) + lm.predict(x) residuals = lm.residuals(x, y, K=None, center=False) assert residuals[0] == pytest.approx(0.885973530) @@ -232,7 +232,7 @@ def test_residuals_weights(random_data_weights): weights = random_data_weights["weights"].values lm.fit(x, y, weights=weights, fit_intercept=False) - predictions = lm.predict(x) + lm.predict(x) residuals = lm.residuals(x, y, weights=weights, K=None, center=False) diff --git a/tests/test_quantile.py b/tests/test_quantile.py index 515bdc0..a938615 100644 --- a/tests/test_quantile.py +++ b/tests/test_quantile.py @@ -104,9 +104,9 @@ def test_multiple(random_data_no_weights): quantreg.fit(x, y, taus, fit_intercept=False) quantreg.predict(x) assert quantreg.coefficients.shape == (5, 3) - np.testing.assert_allclose(quantreg.coefficients[:,0], [0.17759, 6.99588, 4.18896, 4.83906, 3.22546], rtol=TOL) - np.testing.assert_allclose(quantreg.coefficients[:,1], [1.57699, 6.74906, 4.40175, 4.85346, 4.51814], rtol=TOL) - np.testing.assert_allclose(quantreg.coefficients[:,2], [1.85617, 6.81286, 6.05586, 5.51965, 4.19864], rtol=TOL) + np.testing.assert_allclose(quantreg.coefficients[:, 0], [0.17759, 6.99588, 4.18896, 4.83906, 3.22546], rtol=TOL) + np.testing.assert_allclose(quantreg.coefficients[:, 1], [1.57699, 6.74906, 4.40175, 4.85346, 4.51814], rtol=TOL) + np.testing.assert_allclose(quantreg.coefficients[:, 2], [1.85617, 6.81286, 6.05586, 5.51965, 4.19864], rtol=TOL) ######################