spectrochempy.LSTSQ
- class LSTSQ(*, log_level='WARNING', warm_start=False, fit_intercept=True, positive=False)[source]
Ordinary least squares Linear Regression (LSTSQ).
Use
sklearn.linear_model.LinearRegressionLinearRegression 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_levelmethod or by changing thelog_levelattribute.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_startisTrue, 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 theinterceptfor this model. If set toFalse, nointerceptwill 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
NNLSNon-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
Yinput dataset.Estimated coefficients for the linear regression problem.
traitlets.config.Configobject.Whether to calculate the
interceptfor this model.Return a float or an array of shape (n_targets,).
Return
logoutput.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
paramsmethod.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.Configobject.
- description = 'Ordinary Least Squares Linear Regression'
- fit_intercept
Whether to calculate the
interceptfor this model. If set toFalse, nointerceptwill 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.0iffit_interceptisFalse. IfYhas units, thenintercepthas the same units.
- log
Return
logoutput.
- name = 'LSTSQ'
Methods Documentation
- fit(X, Y=None, sample_weight=None)[source]
Fit linear model.
- Parameters:
X (
NDDatasetor array-like of shape (n_observations,:term:n_features)) – Training data, wheren_observationsis the number of observations andn_featuresis 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.8.0") def parameters(self, default=False)[source]
Alias for
paramsmethod.Deprecated since version 0.8.0: Use
paramsinstead.
- predict(X=None)[source]
Predict features using the linear model.
- Parameters:
X (
NDDatasetor array-like matrix, shape (n_observations,:term:n_features)) – Observations. IfXis not set, the inputXforfitis 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.0and 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 (
NDDatasetor array-like of shape (n_observations, n_features)) – Test samples.Y (
NDDatasetor 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