Lasso#
- class cuml.linear_model.Lasso(alpha=1.0, *, fit_intercept=True, max_iter=1000, tol=0.001, solver='cd', selection='cyclic', output_type=None, verbose=False)[source]#
Linear Model trained with L1 prior as regularizer (aka the Lasso).
This is the same as
ElasticNet(l1_ratio=1.0)(no L2 penalty).- Parameters:
- alphafloat (default = 1.0)
Constant that multiplies the L1 term. alpha = 0 is equivalent to an ordinary least square, solved by the LinearRegression object. For numerical reasons, using alpha = 0 with the Lasso object is not advised. Given this, you should use the LinearRegression object.
- fit_interceptboolean (default = True)
If True, Lasso tries to correct for the global mean of y. If False, the model expects that you have centered the data.
- max_iterint (default = 1000)
The maximum number of iterations
- tolfloat (default = 1e-3)
The tolerance for the optimization: if the updates are smaller than tol, the optimization code checks the dual gap for optimality and continues until it is smaller than tol.
- solver{‘cd’, ‘qn’} (default=’cd’)
Choose an algorithm:
‘cd’ - coordinate descent
‘qn’ - quasi-newton
You may find the alternative ‘qn’ algorithm is faster when the number of features is sufficiently large, but the sample size is small.
- selection{‘cyclic’, ‘random’} (default=’cyclic’)
If set to ‘random’, a random coefficient is updated every iteration rather than looping over features sequentially by default. This (setting to ‘random’) often leads to significantly faster convergence especially when tol is higher than 1e-4.
- output_type{‘input’, ‘array’, ‘dataframe’, ‘series’, ‘df_obj’, ‘numba’, ‘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.- verboseint or boolean, default=False
Sets logging level. It must be one of
cuml.common.logger.level_*. See Verbosity Levels for more info.
- Attributes:
- coef_array, shape (n_features)
The estimated coefficients for the linear regression model.
- intercept_array
The independent term. If
fit_interceptis False, will be 0.- n_iter_int
The number of iterations taken by the solver.
Notes
For additional docs, see scikitlearn’s Lasso.
Examples
>>> import numpy as np >>> import cudf >>> from cuml.linear_model import Lasso >>> ls = Lasso(alpha = 0.1, solver='qn') >>> X = cudf.DataFrame() >>> X['col1'] = np.array([0, 1, 2], dtype = np.float32) >>> X['col2'] = np.array([0, 1, 2], dtype = np.float32) >>> y = cudf.Series( np.array([0.0, 1.0, 2.0], dtype = np.float32) ) >>> result_lasso = ls.fit(X, y) >>> print(result_lasso.coef_) 0 0.425 1 0.425 dtype: float32 >>> print(result_lasso.intercept_) 0.150000...
>>> X_new = cudf.DataFrame() >>> X_new['col1'] = np.array([3,2], dtype = np.float32) >>> X_new['col2'] = np.array([5,5], dtype = np.float32) >>> preds = result_lasso.predict(X_new) >>> print(preds) 0 3.549997 1 3.124997 dtype: float32