statisticscurve-fitting

Linear Regression

Fit a linear model and plot the residuals — the simplest fitting example, and what most people actually need.

MATLAB's `polyfit(x, y, 1)` and numpy's `np.polyfit(x, y, 1)` are identical. The residuals and R² calculation requires a couple of extra steps in Python since MATLAB bakes them into the `fit` object, but numpy's vectorized math makes it easy.

MATLAB source17 lines
% Fit a line to noisy data and report R^2
x = linspace(0, 10, 50)';
y = 2.5*x + 3 + randn(size(x));

p = polyfit(x, y, 1);
y_fit = polyval(p, x);
ss_res = sum((y - y_fit).^2);
ss_tot = sum((y - mean(y)).^2);
r_squared = 1 - ss_res/ss_tot;

figure
plot(x, y, 'ko'); hold on;
plot(x, y_fit, 'r-', 'LineWidth', 2);
xlabel('x'); ylabel('y');
title(sprintf('y = %.2fx + %.2f, R^2 = %.3f', p(1), p(2), r_squared));
legend('data', 'fit');
grid on;
Python output (converter-generated)23 lines · 2 flags
import numpy as np
import matplotlib.pyplot as plt

# Fit a line to noisy data and report R^2
x = np.linspace(0, 10, 50).T
y = 2.5*x + 3 + np.random.randn(x.shape)

p = np.polyfit(x, y, 1)
y_fit = np.polyval(p, x)
ss_res = np.sum((y - y_fit)**2)
ss_tot = np.sum((y - np.mean(y))**2)
r_squared = 1 - ss_res/ss_tot

plt.figure()
plt.plot(x, y, 'ko')
# hold on removed — matplotlib accumulates plots by default
plt.plot(x, y_fit, 'r-', linewidth=2)
plt.xlabel('x')
plt.ylabel('y')
plt.title('y = %.2fx + %.2f, R^2 = %.3f' % (p[0], p[1], r_squared))
plt.legend(['data', 'fit'])
plt.grid(True)
Converter flags (2)
  • TOOLBOXLine 5: polyfit → np.polyfit — Same interface: np.polyfit(x, y, deg). Returns coefficients highest-degree first, same as MATLAB.
  • TOOLBOXLine 6: polyval → np.polyval — Same interface: np.polyval(p, x).
Try it on your own MATLAB

Free for 50 lines. Same converter that produced the Python above.

More examples like this, once a week

New canonical conversions and release notes from the converter. One email, no spam.