Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions python/cuml/cuml/linear_model/linear_regression.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import cupyx.scipy.sparse as sp
import numpy as np

from cuml.common.doc_utils import generate_docstring
from cuml.common.sparse import is_sparse
from cuml.internals.base import Base, get_handle
from cuml.internals.interop import InteropMixin
from cuml.internals.mixins import (
Expand Down Expand Up @@ -91,8 +90,7 @@ class LinearRegression(InteropMixin,
The algorithm to use when fitting:

- 'auto': will select 'nnls' if ``positive=True``. Otherwise it
selects 'eig' if supported, falling back to 'lsmr' if X is sparse,
and 'svd' otherwise.
selects 'cholesky' for dense ``X`` and 'lsmr' for sparse ``X``.

- 'cholesky': uses a cholesky decomposition of the covariance matrix,
falling back to an eigendecomposition if that's singular. This is
Expand Down Expand Up @@ -258,9 +256,7 @@ class LinearRegression(InteropMixin,
may_mutate_sample_weight=False,
):
"""Fit a LinearRegression using a libcuml solver"""
cdef int algo = (
Algo.EIG if self.algorithm == "auto" else Algo.parse(self.algorithm)
)
cdef int algo = Algo.parse(self.algorithm)

# All libcuml solvers require F-ordered X, and mutate the inputs.
X = cp.array(X, order="F", copy=None if may_mutate_X else True)
Expand Down Expand Up @@ -337,21 +333,15 @@ class LinearRegression(InteropMixin,
Fit the model with X and y.

"""
X_orig, y_orig, sample_weight_orig = X, y, sample_weight
if self.positive and self.algorithm not in ("auto", "nnls"):
raise ValueError(
"`positive=True` requires `algorithm='auto'` or "
f"`algorithm='nnls'`, got `algorithm={self.algorithm!r}`."
)
if not self.positive and self.algorithm == "nnls":
elif self.algorithm == "nnls" and not self.positive:
raise ValueError("`algorithm='nnls'` requires `positive=True`.")
if self.positive and is_sparse(X):
raise TypeError(
"`positive=True` requires dense `X`, but sparse data was "
"passed. Use '.toarray()' to explicitly convert it to a "
"dense array."
)

X_orig, y_orig, sample_weight_orig = X, y, sample_weight
X, y, sample_weight = check_inputs(
self,
X,
Expand All @@ -367,6 +357,7 @@ class LinearRegression(InteropMixin,
X_is_copy = cuda_ptr(X) != cuda_ptr(X_orig)
y_is_copy = cuda_ptr(y) != cuda_ptr(y_orig)
sample_weight_is_copy = cuda_ptr(sample_weight) != cuda_ptr(sample_weight_orig)
X_is_sparse = sp.issparse(X)

if self.algorithm not in (
"auto", "cholesky", "eig", "svd", "lsmr", "nnls", "qr", "svd-qr", "svd-jacobi"
Expand All @@ -377,12 +368,14 @@ class LinearRegression(InteropMixin,
fallback_reason = None
if self.positive:
solver = "nnls"
elif self.algorithm == "cholesky" and sp.issparse(X) and self.fit_intercept:
elif self.algorithm == "auto":
solver = "lsmr" if X_is_sparse else "cholesky"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
elif self.algorithm == "cholesky" and X_is_sparse and self.fit_intercept:
solver = "lsmr"
fallback_reason = "sparse X with `fit_intercept=True`"
elif self.algorithm in ("cholesky", "lsmr"):
solver = self.algorithm
elif sp.issparse(X):
elif X_is_sparse:
solver = "lsmr"
fallback_reason = "sparse X"
elif X.shape[1] == 1:
Expand Down
14 changes: 5 additions & 9 deletions python/cuml/cuml/linear_model/ridge.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class Ridge(InteropMixin,
solver : {'auto', 'cholesky', 'eig', 'svd', 'lsmr'}, default='auto'
The solver to use when fitting:

- 'auto': will select 'eig' if supported, falling back to 'lsmr' if X
is sparse, and 'svd' otherwise.
- 'auto': will select 'cholesky' for dense ``X``, and 'lsmr' for
sparse ``X``.

- 'cholesky': uses a cholesky decomposition of the covariance matrix,
falling back to an eigendecomposition if that's singular. This is
Expand Down Expand Up @@ -394,6 +394,7 @@ class Ridge(InteropMixin,
X_is_copy = cuda_ptr(X) != cuda_ptr(X_orig)
y_is_copy = cuda_ptr(y) != cuda_ptr(y_orig)
sample_weight_is_copy = cuda_ptr(sample_weight) != cuda_ptr(sample_weight_orig)
X_is_sparse = sp.issparse(X)

n_targets = 1 if y.ndim == 1 else y.shape[1]

Expand Down Expand Up @@ -433,14 +434,9 @@ class Ridge(InteropMixin,
"solver='svd' or solver='auto' instead"
)
elif solver == "auto":
if sp.issparse(X):
solver = "lsmr"
elif X.shape[1] == 1 or n_targets != 1:
solver = "svd"
else:
solver = "eig"
solver = "lsmr" if X_is_sparse else "cholesky"
Comment thread
jcrist marked this conversation as resolved.
Resolved

if sp.issparse(X):
if X_is_sparse:
if solver not in ("lsmr", "cholesky"):
raise ValueError(
f"{solver=!r} doesn't support sparse X, please select "
Expand Down
18 changes: 3 additions & 15 deletions python/cuml/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,27 +369,15 @@ def test_cholesky_sparse_invalid_fit_intercept():


def test_solver_auto():
# 1 feature defaults to svd
X, y = make_regression(n_features=1, random_state=42)
model = cuml.Ridge().fit(X, y)
assert model.solver_ == "svd"

# multi-target defaults to svd
X, y = make_regression(n_targets=3, random_state=42)
# dense defaults to cholesky
X, y = make_regression(random_state=42)
model = cuml.Ridge().fit(X, y)
assert model.solver_ == "svd"
assert model.solver_ == "cholesky"

# sparse defaults to lsmr
model = cuml.Ridge().fit(sp.csr_matrix(X), y)
assert model.solver_ == "lsmr"
model = cuml.Ridge().fit(cp_sp.csr_matrix(cp.asarray(X)), y)
assert model.solver_ == "lsmr"

# otherwise defaults to eig
X, y = make_regression(random_state=42)
model = cuml.Ridge().fit(X, y)
assert model.solver_ == "eig"


@pytest.mark.parametrize("solver", ["eig", "svd", "cholesky", "lsmr"])
def test_more_features_than_samples(solver):
Expand Down
Loading