首页 > 解决方案 > 计算 OLS 回归的 yhat

问题描述

我已经实现了一种在 python 中计算 OLS 回归的 beta 的方法。现在,我想使用 R^2 对我的模型进行评分。对于我的任务,我不允许使用 Python 包来执行此操作,因此必须从头开始实现一个方法。

#load the data
import numpy as np
import pandas as pd
from numpy.linalg import inv
from sklearn.datasets import load_boston
boston = load_boston()

# Set the X and y variables. 
X = boston.data
y = boston.target

#append ones to my X matrix. 
int = np.ones(shape=y.shape)[..., None]
X = np.concatenate((int, X), 1)

#compute betas. 
betas = inv(X.transpose().dot(X)).dot(X.transpose()).dot(y)

# extract the feature names of the boston data set and prepend the 
#intercept
names = np.insert(boston.feature_names, 0, 'INT')

# collect results into a DataFrame for pretty printing
results = pd.DataFrame({'coeffs':betas}, index=names)

#print the results
print(results)

            coeffs
INT      36.491103
CRIM     -0.107171
ZN        0.046395
INDUS     0.020860
CHAS      2.688561
NOX     -17.795759
RM        3.804752
AGE       0.000751
DIS      -1.475759
RAD       0.305655
TAX      -0.012329
PTRATIO  -0.953464
B         0.009393
LSTAT    -0.525467

现在,我想实现一个 R^2 来根据这个数据(或任何其他数据)对我的模型进行评分。(见这里: https ://en.wikipedia.org/wiki/Coefficient_of_determination )

我的问题是我不完全确定如何计算分子 SSE。在代码中它看起来像这样:

#numerator
sse = sum((Y - yhat ** 2)

其中 Y 是波士顿房价,yhat 是这些房屋的预测价格。但是,我如何计算术语,yhat

标签: pythonpython-3.x

解决方案


yhat是您对给定观察的估计。您可以通过 使用点积同时获得所有估计值X.dot(betas)

您的误差平方和如下(注意对您给出的版本的更正:您需要对差异进行平方,即对错误进行平方):

y_hat = X.dot(betas)
errors = y - y_hat 
sse = (errors ** 2).sum()

你的总平方和:

tss = ((y - y.mean()) ** 2).sum()

以及由此产生的 R 平方(确定系数):

r2 = 1 - sse / tss

此外,我不会将int其用作变量名以避免破坏内置int函数(只需调用它onesconst改为)。


推荐阅读