Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
AI308 Machine Learning - Lesson 6
From closed-form equations to scikit-learn
β₀ is the intercept (bias), β₁ is the slope (weight)MSE) measures the average squared prediction errorβ₀, β₁ that minimize this cost functionx and yŷ = β₀ + β₁xR², MSE, and RMSEWorked Example
5 students: hours studied (x) versus exam score (y)6 hoursx = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 5, 4, 5])
x_mean, y_mean = np.mean(x), np.mean(y)x = 3, mean of y = 46, sum of squared x-deviations = 10Worked Example
β₀ and β₁0.6 points to the predicted scoreŷ = 2.2 + 0.6xWorked Example
6 hours| Metric | Value |
|---|---|
| MSE | 0.48 |
| RMSE | ≈ 0.69 |
| R² | 0.6 |
R² = 0.6 means the model explains 60% of the variance in exam scoresWorked Example
sq ft) predicts price for 5 sample houses2000 sq ft house$205 to the predicted priceR² = 0.9966, RMSE = 5.81, a much tighter fit than Example 12000 sq ft house: `$382.59k`LinearRegression reproduces the manual math exactlyfrom sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X.reshape(-1, 1), y)
slope, intercept = model.coef_[0], model.intercept_
predicted = model.predict([[6]])MSE; same units as the target, easier to interpretR²)0 to 1; higher is betterRidge, Lasso)MSE)R², MSE, and RMSE together, never just one metricscikit-learn should always agree: a great trust-building exerciseMahmoud Abas|Linear Regression Lab