spectrochempy.LSTSQ
- class LSTSQ(*, log_level='WARNING', warm_start=False, fit_intercept=True, positive=False)[source][source]
Ordinary least squares Linear Regression (LSTSQ).
Use
sklearn.linear_model.LinearRegression
LinearRegression fits a linear model with coefficients
w = (w1, ..., wp)
to minimize the residual sum of squares between the observed targets in the dataset, and the targets predicted by the linear approximation.- Parameters:
log_level (any of [
"INFO"
,"DEBUG"
,"WARNING"
,"ERROR"
], optional, default:"WARNING"
) – The log level at startup. It can be changed later on using theset_log_level
method or by changing thelog_level
attribute.warm_start (
bool
, optional, default:False
) – When fitting repeatedly on the same dataset, but for multiple parameter values (such as to find the value maximizing performance), it may be possible to reuse previous model learned from the previous parameter value, saving time.When
warm_start
isTrue
, the existing fitted model attributes is used to initialize the new model in a subsequent call tofit
.fit_intercept (
bool
, optional, default: True) – Whether to calculate theintercept
for this model. If set toFalse
, nointercept
will be used in calculations (i.e., data is expected to be centered).positive (
bool
, optional, default: False) – When set toTrue
, forces the coefficients (coef
) to be positive.
See also
NNLS
Non-Negative least squares Linear Regression.
Initialize the BaseConfigurable class.
- Parameters:
log_level (int, optional) – The log level at startup. Default is logging.WARNING.
**kwargs (dict) – Additional keyword arguments for configuration.
Attributes Summary
Return the X input dataset (eventually modified by the model).
Return the
Y
input dataset.Estimated coefficients for the linear regression problem.
traitlets.config.Config
object.Whether to calculate the
intercept
for this model.Return a float or an array of shape (n_targets,).
Return
log
output.When set to
True
, forces the coefficients (coef
) to be positive.Methods Summary
fit
(X[, Y, sample_weight])Fit linear model.
parameters
([replace, removed, default])Alias for
params
method.params
([default])Return current or default configuration values.
predict
([X])Predict features using the linear model.
reset
()Reset configuration parameters to their default values.
score
([X, Y, sample_weight])Return the coefficient of determination of the prediction.
to_dict
()Return config value in a dict form.
Attributes Documentation
- X
Return the X input dataset (eventually modified by the model).
- coef
Estimated coefficients for the linear regression problem.
If multiple targets are passed during the fit (Y 2D), this is a 2D array of shape (n_targets, n_features), while if only one target is passed, this is a 1D array of length n_features.
- config
traitlets.config.Config
object.
- description = 'Ordinary Least Squares Linear Regression'
- fit_intercept
Whether to calculate the
intercept
for this model. If set toFalse
, nointercept
will be used in calculations (i.e., data is expected to be centered).
- intercept
Return a float or an array of shape (n_targets,).
Independent term in the linear model. Set to
0.0
iffit_intercept
isFalse
. IfY
has units, thenintercept
has the same units.
- log
Return
log
output.
- name = 'LSTSQ'
Methods Documentation
- fit(X, Y=None, sample_weight=None)[source]
Fit linear model.
- Parameters:
X (
NDDataset
or array-like of shape (n_observations,:term:n_features
)) – Training data, wheren_observations
is the number of observations andn_features
is the number of features.Y (array-like of shape (n_observations,) or (n_observations,:term:
n_targets
)) – Target values. Will be cast toX
’s dtype if necessary.sample_weight (array-like of shape (n_observations,), default:
None
) – Individual weights for each observation.
- Returns:
self – Returns the instance itself.
- parameters(replace="params", removed="0.7.1") def parameters(self, default=False)[source]
Alias for
params
method.
- predict(X=None)[source]
Predict features using the linear model.
- Parameters:
X (
NDDataset
or array-like matrix, shape (n_observations,:term:n_features
)) – Observations. IfX
is not set, the inputX
forfit
is used.- Returns:
NDDataset
– Predicted values (object of type of the input) using a ahape (n_observations,).
- score(X=None, Y=None, sample_weight=None)[source]
Return the coefficient of determination of the prediction.
The coefficient of determination \(R^2\) is defined as \((1 - \frac{u}{v})\) , where \(u\) is the residual sum of squares
((y_true - y_pred)** 2).sum()
and \(v\) is the total sum of squares((y_true - y_true.mean()) ** 2).sum()
. The best possible score is1.0
and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value ofY
, disregarding the input features, would get a \(R^2\) score of 0.0.- Parameters:
X (
NDDataset
or array-like of shape (n_observations, n_features)) – Test samples.Y (
NDDataset
or array-like of shape (n_observations,)) – True values forX
.sample_weight (array-like of shape (n_observations,), default:
None
) – Sample weights.
- Returns:
Examples using spectrochempy.LSTSQ