+10 XP

Fitting Lines to Data

Linear regression finds the best straight-line fit through data by minimizing the sum of squared errors.

y = β₀ + β₁x + ε Loss = Σ(y_true - y_pred)²

β₀ = intercept, β₁ = slope, ε = noise.

python
import numpy as np

# Stimulus intensity → firing rate data
x = np.array([0, 1, 2, 3, 4, 5])   # stimulus
y = np.array([5, 12, 18, 27, 35, 41])  # firing rate

# Least-squares fit (analytical solution)
X = np.column_stack([np.ones_like(x), x])
beta = np.linalg.lstsq(X, y, rcond=None)[0]
print(f'Intercept: {beta[0]:.2f}')
print(f'Slope: {beta[1]:.2f}')

np.linalg.lstsq — the NMA analytical regression solver.

The (coefficient of determination) measures how much variance in y your model explains. R² = 1 means perfect fit; R² = 0 means the model is no better than just guessing the mean.

🦌 Ilya says: NMA Tutorial W1D3 is entirely about linear regression on neural data. This IS the tutorial.