SYDE 572 · Introduction to Pattern Recognition
Linear basis function regression
The data is the Boston Housing set: 506 towns, 13 input features, and the median house value (in $1000s) as the target. I used the housing.data and housing.names files that came with the assignment. The first 100 rows are the training set and the other 406 rows are the test set. Every error on this page is an RMS error in $1000s.
Normalization. Each input feature is z-scored with the mean and standard deviation of the 100 training rows only. The test rows are scaled with those same numbers, so nothing from the test set goes into the fit. The target is not normalized.
Fitting. The unregularized fits use numpy's lstsq (a least squares solver based on the SVD). For the regularized fits (parts d and f) I minimize the squared error plus lambda times the sum of the squared weights, which has the closed form
\[ w = (\Phi^T \Phi + \lambda I')^{-1} \Phi^T t \]
where I' is the identity matrix with a 0 in the bias position, so the bias weight is not penalized. For lambda = 0 I use lstsq again.
Cross-validation. 10 folds, made by randomly splitting the 100 training rows (numpy seed 572, picked before I looked at any results). Inside each fold, the normalization is recomputed from that fold's training part only. In part (f) the Gaussian width and centers are also taken from that fold's training part only.
For degree d, the feature matrix has a bias column plus the powers 1 to d of each of the 13 normalized features (no cross-terms), so there are 13d + 1 weights. I fit the weights on the first 100 rows, then computed the RMS error on those same 100 rows (training) and on the other 406 rows (test).
| Degree | Weights | Rank of feature matrix | Training RMS | Test RMS |
|---|---|---|---|---|
| 1 | 14 | 13 | 2.0372 | 3.64 × 101 |
| 2 | 27 | 25 | 1.5781 | 1.10 × 103 |
| 3 | 40 | 37 | 1.4491 | 4.87 × 104 |
| 4 | 53 | 48 | 1.2705 | 2.19 × 107 |
| 5 | 66 | 54 | 1.0966 | 1.84 × 1010 |
| 6 | 79 | 60 | 1.0725 | 5.02 × 1012 |
| 7 | 92 | 66 | 0.8815 | 6.88 × 1015 |
housing.data) have residuals (prediction minus true value) of -294.1, -250.7 and -227.0. Their crime rates are 88.98, 73.53 and 67.92, far above anything in the training rows (max 1.61). Raising such a value to the 7th power makes it much worse.
Comment. As the polynomial degree increases, the training error decreases (from 2.04 at degree 1 to 0.88 at degree 7) because the model becomes more flexible and can fit the training data more closely. By degree 7 there are 92 weights for only 100 training rows. The test error goes the other way and increases dramatically at every degree, so the model does not generalize well to unseen data. Part of this is overfitting, but not all of it: the test error is already about 36 at degree 1, where the training error is only 2.04. A big part of the error comes from a few test rows with extreme feature values, such as the very high crime rates, which land far outside the training range after normalizing. Raising those values to higher powers makes the predictions much larger, so the error blows up as the degree goes up. Therefore, the model with the lowest training error (degree 7) is not the best model for new data. Here degree 1 has the lowest test error.
Degree 1 fit on the first 100 rows (training RMS 2.0372, test RMS 36.4449). The first weight column is for the z-scored features, which is what the model is actually fit on. The second column converts each weight back to the original units of the feature (the weight divided by that feature's training std, with the bias adjusted to match). The script checks that the raw-unit model gives the same training predictions as the z-scored one, and it does.
| Feature # | Name | Weight (z-scored) | Weight per raw unit |
|---|---|---|---|
| bias | w0 | 22.3090 | -37.9042 |
| 1 | CRIM | -1.3380 | -3.4559 |
| 2 | ZN | 0.1230 | 0.0054 |
| 3 | INDUS | -0.5311 | -0.1688 |
| 4 | CHAS | 0.0000 | 0.0000 |
| 5 | NOX | 1.0027 | 21.5154 |
| 6 | RM | 4.1956 | 8.5910 |
| 7 | AGE | -2.1663 | -0.0792 |
| 8 | DIS | 0.0107 | 0.0076 |
| 9 | RAD | -0.1938 | -0.1399 |
| 10 | TAX | -0.3486 | -0.0076 |
| 11 | PTRATIO | 0.2613 | 0.1546 |
| 12 | B | 0.1811 | 0.0066 |
| 13 | LSTAT | 0.1488 | 0.0262 |
Comment on RM (6th feature). The weight for RM is about +8.59 in the original feature units. Since the target is in $1000s, one extra room is associated with an increase of about $8,590 in predicted median house value, with the other features held constant. It is also the largest weight on the z-scored features (4.20), so RM matters most in this fit. A positive weight seems reasonable because houses with more rooms are generally larger and more valuable. However, the model was trained on only 100 rows, so this estimate may not represent the relationship across the whole dataset.
Comment on DIS (8th feature). The weight for DIS is about +0.0076 in the original units, which works out to under $8 per unit of distance since the target is in $1000s. On the z-scored features it is 0.0107, compared with 4.20 for RM, so it is tiny next to RM (comparing the raw weights directly would not be fair, because the features have different units). This suggests that distance to employment centres has very little influence on the prediction when the other features are held constant. The positive sign is somewhat unexpected, since being closer to employment centres might be expected to increase property values, but with a weight this close to 0 the sign probably does not mean much. The features may also be related to one another (in the training rows DIS has a correlation of 0.60 with ZN and -0.48 with NOX), which makes it hard to isolate the effect of distance, especially when the model is trained on only 100 rows.
To be able to plot the result, I use only the 2nd feature, ZN (the proportion of residential land zoned for large lots), and fit polynomials of degree 1 to 7 to it. ZN is z-scored with the training mean and std before taking powers, and the curves are drawn on the original ZN axis. Every degree was fit; I show degrees 1, 3 and 7 below.
| Degree | Training RMS | Test RMS |
|---|---|---|
| 1 | 5.6951 | 9.3523 |
| 2 | 5.6696 | 9.4456 |
| 3 | 5.6649 | 9.4341 |
| 4 | 5.6506 | 9.3897 |
| 5 | 5.6475 | 9.3847 |
| 6 | 5.6473 | 9.3575 |
| 7 | 5.5006 | 10.4233 |
Comment on the degree 1 plot. The degree 1 fit is a nearly flat line that rises only slightly with ZN, which suggests that ZN alone does not have a strong linear relationship with median house value. The training RMS is 5.6951 and the test RMS is higher at 9.3523, so the fit does worse on unseen data than on the data it was trained on. Using only ZN is also much less accurate on the training data than the degree 1 fit with all 13 features (5.6951 vs 2.0372 in part a), so one feature is not enough to predict house values well. Its test error is lower than the 36.4449 from part (a), but that is probably only because ZN does not have extreme values like the crime rate does, which is what breaks the 13-feature fit.
Comment on the degree 3 plot. Increasing the degree to 3 slightly reduces the training error from 5.6951 to 5.6649, but the test error goes up slightly from 9.3523 to 9.4341. The fitted curve is still fairly similar to the degree 1 result, so the extra polynomial terms do not seem to improve the model much. This suggests that a more complex polynomial is not particularly useful for this feature.
Comment on the degree 7 plot. The degree 7 polynomial has the lowest training RMS of the seven degrees (5.5006) but also the highest test RMS (10.4233). The fitted curve swings up and down between the data clusters, which suggests it is fitting variations in the training data instead of a general relationship. This looks like overfitting, since the small improvement in training error comes with worse predictions on the test data.
Here I use the first 100 rows, only the ZN feature, and a degree 8 polynomial with L2 regularization. For each λ in {0, 0.01, 0.1, 1, 10, 100, 1000} I ran 10-fold cross-validation and averaged the validation RMS over the 10 folds.
| λ | Average validation RMS | Std over folds |
|---|---|---|
| 0 | 8.4663 | 9.3906 |
| 0.01 | 6.3230 | 4.0941 |
| 0.1 | 9.2044 | 12.2161 |
| 1 | 9.6817 | 13.1129 |
| 10 | 9.7714 | 13.0908 |
| 100 | 8.8270 | 10.3164 |
| 1000 | 6.4836 | 3.9586 |
The lowest average validation error is at λ = 0.01 (6.3230), so that is the value I would choose from this cross-validation.
| Seed | Best λ | Avg val RMS at 0.01 | Avg val RMS at 1000 |
|---|---|---|---|
| 0 | 1000 | 7.82 | 6.88 |
| 1 | 0.01 | 6.77 | 6.89 |
| 2 | 0.01 | 6.09 | 6.78 |
| 3 | 1000 | 26.51 | 6.08 |
| 4 | 0.01 | 6.77 | 6.98 |
| 572 | 0.01 | 6.32 | 6.48 |
Comment on (d). Based on the 10-fold cross-validation, λ = 0.01 gives the lowest average validation RMS (6.3230), so I would select this value. However, the standard deviation across folds is fairly high (4.0941), so the error depends a lot on which rows end up in the validation fold. Different random splits also picked different values (0.01 or 1000, and λ = 1000 is almost as good here at 6.4836), so I would not consider this choice very reliable without more testing.
Gaussian basis functions with K = 5, 15, ..., 95 centers, using all 13 normalized features. The centers are the first K points of one random permutation of the 100 training points (numpy seed 572), so the centers for a smaller K are also in the larger ones. The assignment doesn't give a width, so I used one width for all the basis functions: sigma = 4.6247, the median distance between pairs of training points. I used this rule instead of searching for the sigma with the lowest test error. There is a bias term and no regularization.
| K | Training RMS | Test RMS |
|---|---|---|
| 5 | 2.7134 | 7.7587 |
| 15 | 2.1955 | 8.3585 |
| 25 | 1.6264 | 7.3325 |
| 35 | 1.3553 | 8.3304 |
| 45 | 1.2320 | 8.2442 |
| 55 | 1.1844 | 9.1713 |
| 65 | 1.0558 | 8.3185 |
| 75 | 0.6642 | 23.0202 |
| 85 | 0.4627 | 24.4488 |
| 95 | 0.3759 | 87.0259 |
| Seed | K=5 | 15 | 25 | 35 | 45 | 55 | 65 | 75 | 85 | 95 |
|---|---|---|---|---|---|---|---|---|---|---|
| 572 | 7.76 | 8.36 | 7.33 | 8.33 | 8.24 | 9.17 | 8.32 | 23.02 | 24.45 | 87.03 |
| 0 | 7.82 | 8.06 | 7.80 | 8.66 | 13.07 | 13.38 | 19.03 | 22.13 | 44.16 | 36.54 |
| 1 | 9.63 | 7.89 | 8.31 | 7.39 | 12.31 | 8.21 | 12.28 | 27.35 | 86.79 | 239.62 |
| 2 | 14.10 | 11.20 | 7.91 | 7.68 | 16.97 | 14.06 | 8.77 | 49.92 | 187.12 | 57.88 |
| 3 | 9.30 | 8.65 | 9.86 | 11.06 | 7.43 | 8.37 | 20.55 | 16.24 | 12.08 | 32.20 |
| 4 | 11.49 | 7.58 | 7.92 | 12.17 | 10.55 | 13.15 | 21.04 | 55.40 | 42.68 | 48.81 |
| K | Weights | Condition number | Largest |weight| |
|---|---|---|---|
| 5 | 6 | 214 | 101.8 |
| 15 | 16 | 953 | 110.3 |
| 25 | 26 | 6.09e+03 | 217.7 |
| 35 | 36 | 1.27e+04 | 1126.8 |
| 45 | 46 | 4.64e+04 | 1150.2 |
| 55 | 56 | 8.42e+04 | 1518.6 |
| 65 | 66 | 2.06e+05 | 3591.5 |
| 75 | 76 | 4.51e+05 | 7819.8 |
| 85 | 86 | 1.34e+06 | 28338.5 |
| 95 | 96 | 1.86e+06 | 32499.8 |
Comment on (e). As the number of Gaussian basis functions goes from 5 to 95, the training RMS decreases from 2.7134 to 0.3759. The test error stays between about 7 and 9 up to K = 65, then jumps to 23.02, 24.45 and 87.03 at K = 75, 85 and 95. This suggests that more basis functions let the model fit the training data more closely, but past a point it overfits and generalizes poorly. At K = 95 there are 96 weights for only 100 training rows, and the largest weight grows from 101.8 at K = 5 to 32499.8, so the fit becomes very sensitive to the training data. This is also only one random choice of centers, and other permutations gave different test errors at large K.
Here I use the first 100 rows and all 13 features, with 90 Gaussian basis functions and L2 regularization. For each λ in {0, 0.01, 0.1, 1, 10, 100, 1000} I ran 10-fold cross-validation (same folds as in part d). In each fold the training part has 90 rows, so the 90 centers are all of those rows. The width sigma is the median distance between pairs of the fold's training points, the same rule as in part (e).
| λ | Average validation RMS | Std over folds |
|---|---|---|
| 0 | 8.4778 | 7.2804 |
| 0.01 | 1.7675 | 0.9194 |
| 0.1 | 2.0224 | 0.9360 |
| 1 | 2.5858 | 0.7892 |
| 10 | 3.2301 | 0.8677 |
| 100 | 4.3401 | 1.5848 |
| 1000 | 5.3862 | 1.8636 |
The lowest average validation error is at λ = 0.01 (1.7675), so that is the value I would choose from this cross-validation. Unlike in part (d), this choice was the same for all six random splits I tried (seeds 0, 1, 2, 3, 4 and 572).
Comment on (f). The unregularized model (λ = 0) has a high validation RMS of 8.4778, and adding a small amount of regularization at λ = 0.01 brings it down to 1.7675. With λ = 0 there are more weights (91) than training rows in each fold (90), which is probably why it does so badly. As λ increases further the validation error rises steadily, reaching 5.3862 at λ = 1000, which suggests that too much regularization stops the model from fitting the data. I would choose λ = 0.01 because it has the lowest validation error of the values tested, and it was also the best value for all six random splits I tried. Since 0.01 is the smallest nonzero value in the grid, testing smaller values could show whether an even better one exists.
All scripts are in this folder and need numpy and matplotlib. Put housing.data next to them and run any script with python3 from this folder. The plots are saved in media/. The assignment writes the function names with hyphens or spaces, but Python needs underscores.
| File | What it does |
|---|---|
| common.py | loads the data, z-scoring, polynomial feature matrix, least squares, RMS |
| ridge.py | ridge (L2) fit and the random 10-fold split |
| gauss.py | Gaussian feature matrix and the median-distance width |
| a_polynomial_regression.py | polynomial_regression, part (a): table and Fig 1 |
| a_check_outliers.py | the CHAS, CRIM and worst-residual numbers used in part (a) and the setup notes |
| b_weights.py | part (b): the weights table |
| b_check_dis.py | how DIS is correlated with the other features |
| c_polynomial_regression_1d_vis.py | polynomial_regression_1d_vis, part (c): the ZN plots |
| c_check_zn.py | the ZN counts used in part (c) |
| d_polynomial_regression_reg.py | polynomial_regression_reg, part (d): cross-validation and Fig 5 |
| d_check_seeds.py | repeats (d) with other random splits |
| e_gaussian_regression.py | gaussian_regression, part (e): table and plot |
| e_plot_ticks.py | the version of the part (e) plot used as Fig 6 (clearer y-axis labels) |
| e_check_seeds.py | repeats (e) with other random permutations |
| e_check_conditioning.py | condition numbers and weight sizes for (e) |
| f_gaussian_regression_reg.py | gaussian_regression_reg, part (f): cross-validation and Fig 7 |
| f_check_seeds.py | repeats (f) with other random splits |
Larissa Rudenko - Fall 2026