Linear regression
| Task | Regression |
Method key (analysis_type) | linear |
| Prediction on new data | No |
| Library | sklearn.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 and the intercept minimise the sum of squared residuals
The quality of the fit is measured by the coefficient of determination
Parameters
| Parameter | Default | Notes |
|---|---|---|
feature_selection | off | Uses 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
| Field | Meaning |
|---|---|
cv_scores | on every test fold |
cv_mean, cv_std | Mean and standard deviation of cv_scores |
r2_score | of the final model on the training data (optimistic) |
coefficients, intercept | and of the final model |
predictions | Predictions 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. on a fold can be negative when the model
predicts worse than the fold mean.
Visualizations
- Time series and heatmap of the input data
Source code
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.