Warning

You are reading the documentation related to the development version. Go here if you are looking for the documentation of the stable release.

spectrochempy.NNLS

class NNLS(*, log_level='WARNING', warm_start=False, fit_intercept=True, positive=True)[source]

Non-Negative least squares Linear Regression (NNLS).

Use sklearn.linear_model.LinearRegression

LinearRegression fits a linear model with coefficients w = (w1, …, wp) which can not be negative 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 the set_log_level method or by changing the log_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 is True, the existing fitted model attributes is used to initialize the new model in a subsequent call to fit.

  • fit_intercept (bool, optional, default: True) – Whether to calculate the intercept for this model. If set to False, no intercept will be used in calculations (i.e., data is expected to be centered).

  • positive (bool, optional, default: True) – When set to True , forces the coefficients to be positive.

See also

NNLS

Ordinary least squares Linear Regression.

Attributes Summary

X

Return the X input dataset (eventually modified by the model).

Y

Return the Y input dataset.

coef

Estimated coefficients for the linear regression problem.

config

traitlets.config.Config object.

description

fit_intercept

Whether to calculate the intercept for this model.

intercept

Return a float or an array of shape (n_targets,).

log

Return log output.

name

positive

When set to True , forces the coefficients to be positive.

Methods Summary

fit(X[, Y, sample_weight])

Fit linear model.

parameters([replace, removed, default])

Alias for params method.

params([default])

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).

Y

Return the Y input dataset.

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 = 'Non-Negative Least Squares Linear Regression'
fit_intercept

Whether to calculate the intercept for this model. If set to False, no intercept 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 if fit_intercept is False. If Y has units, then intercept has the same units.

log

Return log output.

name = 'NNLS'
positive

When set to True , forces the coefficients to be positive.

Methods Documentation

fit(X, Y=None, sample_weight=None)[source]

Fit linear model.

Parameters
Returns

self – Returns the instance itself.

parameters(replace="params", removed="0.7.1") def parameters(self, default=False)[source]

Alias for params method.

params(default=False)[source]

Current or default configuration values.

Parameters

default (bool, optional, default: False) – If default is True, the default parameters are returned, else the current values.

Returns

dict – Current or default configuration values.

predict(X=None)[source]

Predict features using the linear model.

Parameters

X (NDDataset or array-like matrix, shape (n_observations,:term:n_features)) – Observations. If X is not set, the input X for fit is used.

Returns

NDDataset – Predicted values (object of type of the input) using a ahape (n_observations,).

reset()[source]

Reset configuration parameters to their default values

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 is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of Y, disregarding the input features, would get a \(R^2\) score of 0.0.

Parameters
Returns

float\(R^2\) of predict (X ) wrt. Y .

to_dict()[source]

Return config value in a dict form.

Returns

dict – A regular dictionary.

Examples using spectrochempy.NNLS