(X, Y)
| 39 | X3only = df[['X3', 'ones']] |
| 40 | |
| 41 | def get_r2(X, Y): |
| 42 | w = np.linalg.solve( X.T.dot(X), X.T.dot(Y) ) |
| 43 | Yhat = X.dot(w) |
| 44 | |
| 45 | # determine how good the model is by computing the r-squared |
| 46 | d1 = Y - Yhat |
| 47 | d2 = Y - Y.mean() |
| 48 | r2 = 1 - d1.dot(d1) / d2.dot(d2) |
| 49 | return r2 |
| 50 | |
| 51 | print("r2 for x2 only:", get_r2(X2only, Y)) |
| 52 | print("r2 for x3 only:", get_r2(X3only, Y)) |