Skip to main content

Linear regression

TaskRegression
Method key (analysis_type)linear
Prediction on new dataNo
Librarysklearn.linear_model.LinearRegression

:::info Not in the tools panel

Linear regression has no button in the analysis tools panel. It exists in the backend and can only be started through the API (analysis_type: "linear"). This page documents what the backend does.

:::

When to use​

Use it to predict a number (for example, a concentration) from the colour features, when the relation is close to linear.

How it works​

Ordinary least squares: the coefficients β\beta and the intercept β0\beta_0 minimise the sum of squared residuals

min⁡β0,β∑i=1n(yi−β0−xi⊤β)2.\min_{\beta_0, \beta} \sum_{i=1}^{n} \left(y_i - \beta_0 - x_i^\top \beta\right)^2 .

The quality of the fit is measured by the coefficient of determination

R2=1−∑i(yi−y^i)2∑i(yi−yˉ)2.R^2 = 1 - \frac{\sum_i (y_i - \hat{y}_i)^2}{\sum_i (y_i - \bar{y})^2} .

Parameters​

ParameterDefaultNotes
feature_selectionoffUses the regression score functions f_regression / mutual_info_regression. See Feature selection.

average_replicates is ignored by this method.

Preprocessing​

  • The label column is converted to numbers. If that fails (text labels), the labels are encoded as integers 0, 1, 2, … in alphabetical order. Such a "regression" on class codes is rarely meaningful: use a classifier instead.
  • No scaling. Ordinary least squares does not need it for the predictions.

Results and metrics​

FieldMeaning
cv_scoresR2R^2 on every test fold
cv_mean, cv_stdMean and standard deviation of cv_scores
r2_scoreR2R^2 of the final model on the training data (optimistic)
coefficients, interceptβ\beta and β0\beta_0 of the final model
predictionsPredictions of the final model on the training data

Cross-validation differs from the classifiers: Group K-Fold uses GroupKFold; every other cv_method uses scikit-learn's default splitter for regression, which is KFold without shuffling (the folds are consecutive blocks of rows). If the rows are sorted, for example by concentration, the folds are not representative. R2R^2 on a fold can be negative when the model predicts worse than the fold mean.

Visualizations​

Source code​

analysis.py · run_linear_analysis() · lines 1310–1372
def run_linear_analysis(X, y, parameters, cv_method, cv_folds):
"""Run Linear Regression analysis with optional feature selection"""
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import cross_val_score, GroupKFold
from sklearn.metrics import r2_score

# Convert categorical y to numeric if needed
try:
y_numeric = y.astype(float)
except:
le = LabelEncoder()
y_numeric = le.fit_transform(y)

# Check for feature selection
feature_selection_params = get_feature_selection_params(parameters)
if feature_selection_params:
X_processed, selected_features, feature_scores = select_features(
X, y_numeric, feature_selection_params, 'regression'
)
else:
X_processed = X
selected_features = list(range(X.shape[1]))
feature_scores = None

lr = LinearRegression()

# Cross-validation with R²
if cv_method == 'group':
groups = parameters.get('groups')
if groups is None:
raise ValueError("Groups parameter required for group cross-validation")
cv = GroupKFold(n_splits=cv_folds)
cv_scores = cross_val_score(lr, X_processed, y_numeric, cv=cv, scoring='r2', groups=groups)
else:
cv_scores = cross_val_score(lr, X_processed, y_numeric, cv=cv_folds, scoring='r2')

# Fit full model
lr.fit(X_processed, y_numeric)
y_pred = lr.predict(X_processed)

results = {
'method': 'Linear Regression',
'cv_scores': cv_scores.tolist(),
'cv_mean': cv_scores.mean(),
'cv_std': cv_scores.std(),
'r2_score': lr.score(X_processed, y_numeric),
'coefficients': lr.coef_.tolist(),
'intercept': lr.intercept_,
'predictions': y_pred.tolist(),
'labels': y.tolist()
}

# Add feature selection info if used
if feature_selection_params:
results['feature_selection'] = {
'selected_features': selected_features,
'feature_scores': feature_scores,
'method': feature_selection_params.get('method', 'anova'),
'k': feature_selection_params.get('k', 'all')
}

return results

Limitations and common pitfalls​

  • More features than samples: the least-squares solution is not unique and the model fits the training data perfectly. Use feature selection.
  • Correlated features make the coefficients unstable.
  • Not available in the interface, no prediction on new data.

References​

  • Hastie T., Tibshirani R., Friedman J. The Elements of Statistical Learning, 2nd ed., chapter 3. Springer (2009).
  • scikit-learn user guide: Ordinary least squares.