Reading preferences
Concept

Models, Inference, and pytest

Contrast predictive modelling and statistical inference, then test a small function.

python, scikit-learn, statsmodels, pytest, models, testing

Models answer different questions

Scikit-learn offers estimators and tools for prediction and model evaluation. Statsmodels provides statistical models and inference tools. Both rely on data and assumptions. pytest is a separate testing tool for checking code, not evidence that a fitted model is valid.

Python illustration

In a separate environment with scikit-learn and statsmodels installed:

import numpy as np
import statsmodels.api as sm
from sklearn.linear_model import LinearRegression

x = np.array([1.0, 2.0, 3.0])
y = np.array([3.0, 5.0, 7.0])
model = LinearRegression().fit(x.reshape(-1, 1), y)
print(round(float(model.predict([[4.0]])[0]), 1))
result = sm.OLS(y, sm.add_constant(x)).fit()
print([round(float(value), 1) for value in result.params])

Prepared output — not live execution:

9.0
[1.0, 2.0]

The training points exactly follow y = 1 + 2x. The prediction at four is an illustration, not a trustworthy evaluation of generalisation.

See the reasoning

Follow the model from data to prediction

Separate fitted parameters from an evaluation of generalisation. This is a prepared demonstration, not live code execution.

Supply training points

Illustrative modelling pipeline
  1. Training(1,3), (2,5), (3,7)Current focus
  2. Fitpending
  3. Predictpending
  4. Evaluationno held-out test

All three points lie exactly on y = 1 + 2x.

Testing and evaluating

Separate training and evaluation data before fitting preprocessing and estimators to avoid leakage. Small samples and repeated selection can make evaluation misleading. Inference also depends on assumptions; do not infer causation from a coefficient alone. sm.add_constant explicitly adds the intercept column for this example.

With pytest installed, a local test file can include:

import pytest

@pytest.mark.parametrize("values, expected", [([], 0), ([2, 3], 5)])
def test_total(values, expected):
    assert sum(values) == expected

Run python -m pytest locally. Two cases run independently; a test checks the stated examples, not every possible input. These external tools are educational examples and are not dependencies of Intuvo.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What does fit normally do for a scikit-learn estimator?
2. Why keep evaluation data separate from training?
3. Does statsmodels OLS add an intercept automatically to a supplied design matrix?
4. What does pytest parameterization help do?

References and further reading