import numpy as np # Sample data x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 5, 7, 9]) # Perform linear regression and get covariance matrix p, V = np.polyfit(x, y, 1, cov=True) # p contains [slope, intercept] slope = p[0] intercept = p[1] # V is the covariance matrix. The square root of the diagonal elements # are the standard errors (uncertainties) of the coefficients. slope_uncertainty = np.sqrt(V[0, 0]) intercept_uncertainty = np.sqrt(V[1, 1]) print(f"Slope: {slope:.4f} +/- {slope_uncertainty:.4f}") print(f"Intercept: {intercept:.4f} +/- {intercept_uncertainty:.4f}")