LinearRegression#

class cuml.linear_model.LinearRegression(
*,
algorithm='auto',
fit_intercept=True,
copy_X=True,
verbose=False,
output_type=None,
)#

Ordinary least squares Linear Regression.

Parameters:
algorithm{‘auto’, ‘eig’, ‘svd’, ‘lsmr’, ‘qr’, ‘svd-qr’, ‘svd-jacobi’}, default=’auto’

The algorithm to use when fitting:

  • ‘auto’: will select ‘eig’ if supported, falling back to ‘lsmr’ if X is sparse, and ‘svd’ otherwise.

  • ‘eig’: uses an eigendecomposition of the covariance matrix. It is faster than SVD, but potentially unstable. It doesn’t support multi-target y or sparse X.

  • ‘svd’ or ‘svd-jacobi’: uses an SVD decomposition. It’s slower, but stable. It doesn’t support sparse X.

  • ‘lsmr’: uses cupyx.scipy.sparse.linalg.lsmr, an iterative algorithm. It supports all input types and is typically very fast.

  • ‘qr’: uses QR decomposition and solves Rx = Q^T y. It’s faster than SVD, but doesn’t support multi-target y or sparse X.

  • ‘svd-qr’: computes SVD decomposition using QR algorithm. It’s the slowest option. It doesn’t support multi-target y or sparse X.

fit_interceptboolean (default = True)

If True, LinearRegression tries to correct for the global mean of y. If False, the model expects that you have centered the data.

copy_Xboolean, default=True

If True, X will never be mutated. Setting to False may reduce memory usage, at the cost of potentially mutating X.

verboseint or boolean, default=False

Sets logging level. It must be one of cuml.common.logger.level_*. See Verbosity Levels for more info.

output_type{None, ‘input’, ‘cupy’, ‘numpy’, ‘cudf’, ‘pandas’}, default=None

Return results and set estimator attributes to the indicated output type. If None, the output type set at the module level (cuml.global_settings.output_type) will be used. See Output Data Type Configuration for more info.

Attributes:
coef_array, shape (n_features,) or (n_targets, n_features)

The estimated coefficients for the linear regression model.

intercept_float or array, shape (n_targets,)

The independent term. If fit_intercept is False, will be 0. Will be an array when fit on multi-target y, otherwise will be a float.

Methods

fit(self, X, y[, sample_weight])

Fit the model with X and y.

Notes

LinearRegression suffers from multicollinearity (when columns are correlated with each other), and variance explosions from outliers. Consider using Ridge to fix the multicollinearity problem, and consider maybe first DBSCAN to remove the outliers, or statistical analysis to filter possible outliers.

Applications of LinearRegression

LinearRegression is used in regression tasks where one wants to predict say sales or house prices. It is also used in extrapolation or time series tasks, dynamic systems modelling and many other machine learning tasks. This model should be first tried if the machine learning problem is a regression task (predicting a continuous variable).

For additional information, see scikit-learn’s documentation for sklearn.linear_model.LinearRegression.

For an additional example see the OLS notebook.

Examples

>>> import cupy as cp
>>> from cuml.linear_model import LinearRegression
>>> X = cp.array([[1, 1], [1, 2], [2, 2], [2, 3]], dtype=cp.float32)
>>> y = cp.array([6.0, 8.0, 9.0, 11.0], dtype=cp.float32)
>>> model = LinearRegression().fit(X, y)
>>> X_test = cp.array([[3, 5], [2, 5]], dtype=cp.float32)
>>> model.predict(X_test)
array([16.      , 14.999999], dtype=float32)
as_sklearn()[source]#

Convert this estimator into an equivalent scikit-learn (or scikit-learn extension) estimator.

Returns:
sklearn.base.BaseEstimator

A scikit-learn compatible estimator instance that mirrors the trained state of the current estimator.

fit(
self,
X,
y,
sample_weight=None,
) 'LinearRegression'[source]#

Fit the model with X and y.

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

yarray-like (device or host) shape = (n_samples, 1)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

sample_weightarray-like (device or host) shape = (n_samples,), default=None

The weights for each observation in X. If None, all observations are assigned equal weight. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

classmethod from_sklearn(model)[source]#

Create a cuml estimator from a scikit-learn estimator.

Parameters:
modelsklearn.base.BaseEstimator

A compatible scikit-learn (or scikit-learn extension) estimator.

Returns:
cls

A new instance of this cuml estimator class that mirrors the state of the input estimator.

Notes

output_type of the estimator is set to “numpy” by default, as these cannot be inferred from training arguments. If something different is required, then please use cuml’s output_type configuration utilities.

get_params(deep=True)[source]#

Returns a dict of all params owned by this class. If the child class has appropriately overridden the _get_param_names method and does not need anything other than what is there in this method, then it doesn’t have to override this method

predict(X)[source]#

Predicts y values for X.

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

Returns:
predscuDF, CuPy or NumPy object depending on cuML’s output type configuration, shape = (n_samples, 1)

Predicted values

For more information on how to configure cuML’s output type, refer to: Output Data Type Configuration.

score(X, y, sample_weight=None, **kwargs)[source]#

Scoring function for regression estimators

Returns the coefficient of determination R^2 of the prediction.

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

yarray-like (device or host) shape = (n_samples, 1)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

sample_weightarray-like (device or host) shape = (n_samples,), default=None

The weights for each observation in X. If None, all observations are assigned equal weight. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

Returns:
scorefloat

R^2 of self.predict(X) wrt. y.

set_params(**params)[source]#

Accepts a dict of params and updates the corresponding ones owned by this class. If the child class has appropriately overridden the _get_param_names method and does not need anything other than what is, there in this method, then it doesn’t have to override this method