Data Science Linear Functions, Slope and Intercept
Interpret a straight-line calculation with its units and domain.
How it works
A linear prediction has an intercept and a slope: y = intercept + slope × x. The slope expresses a change in predicted y per unit of x. The intercept is the predicted value at x=0, which may be outside the meaningful domain. A line can be a useful approximation without being a law of nature. Keep the range over which it is used visible.
Run the code
Try in playground opens the correct numpy editor and preloads the complete source. Choose Run to execute; loading does not execute it. The isolated browser runtime does not read personal PC files.
Local review: Python 3.12.14, NumPy 2.5.4, Pandas 3.0.6 and SciPy 1.18.0. Browser review: Pyodide 0.27.7, Python 3.12.7, NumPy 2.0.2, Pandas 2.2.3 and SciPy 1.14.1 in their respective editors. Save the download as linear-functions.py and run python linear-functions.py in your separate environment. Apache and MySQL are not needed. Environment setup.
Example
import numpy as np
hours = np.array([2., 3., 4.])
intercept, slope = 5., 3.
predicted = intercept + slope*hours
assert np.allclose(predicted, [11., 14., 17.])
assert predicted[1] - predicted[0] == 3.
print("Predictions: 11.0, 14.0, 17.0 L; slope 3.0 L/hour")
Expected result
Predictions: 11.0, 14.0, 17.0 L; slope 3.0 L/hour
These coefficients are supplied, not estimated from data. Increasing planned hours by one increases the model’s prediction by three litres. That arithmetic says nothing about whether a real kiosk would respond the same way. Later regression estimates coefficients from observations and checks whether their predictions are useful on held-out data.
Technical references: NumPy, Pandas and SciPy. For evaluation design see the scikit-learn guide to leakage; these examples do not require scikit-learn.