将 Numpy Lstsq 残差值转换为 R^2

发布于 2024-09-05 18:02:11 字数 356 浏览 8 评论 0原文

我正在执行如下最小二乘回归(单变量)。我想用 R^2 来表达结果的显着性。 Numpy 返回一个未缩放的残差值,这将是对其进行标准化的明智方法。

field_clean,back_clean = rid_zeros(backscatter,field_data)
num_vals = len(field_clean)
x = field_clean[:,row:row+1]
y = 10*log10(back_clean)

A = hstack([x, ones((num_vals,1))])
soln = lstsq(A, y )
m, c =  soln [0]
residues = soln [1]

print residues

I am performing a least squares regression as below (univariate). I would like to express the significance of the result in terms of R^2. Numpy returns a value of unscaled residual, what would be a sensible way of normalizing this.

field_clean,back_clean = rid_zeros(backscatter,field_data)
num_vals = len(field_clean)
x = field_clean[:,row:row+1]
y = 10*log10(back_clean)

A = hstack([x, ones((num_vals,1))])
soln = lstsq(A, y )
m, c =  soln [0]
residues = soln [1]

print residues

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你穿错了嫁妆 2024-09-12 18:02:11

请参阅 http://en.wikipedia.org/wiki/Coefficient_of_metry

您的 R2 值 =

1 - residual / sum((y - y.mean())**2) 

等效值以

1 - residual / (n * y.var())

为例:

import numpy as np

# Make some data...
n = 10
x = np.arange(n)
y = 3 * x + 5 + np.random.random(n)

# Note that polyfit is an easier way to do this...
# It would just be "model, resid = np.polyfit(x,y,1,full=True)[:2]" 
A = np.vstack((x, np.ones(n))).T
model, resid = np.linalg.lstsq(A, y)[:2]

r2 = 1 - resid / (y.size * y.var())
print r2

See http://en.wikipedia.org/wiki/Coefficient_of_determination

Your R2 value =

1 - residual / sum((y - y.mean())**2) 

which is equivalent to

1 - residual / (n * y.var())

As an example:

import numpy as np

# Make some data...
n = 10
x = np.arange(n)
y = 3 * x + 5 + np.random.random(n)

# Note that polyfit is an easier way to do this...
# It would just be "model, resid = np.polyfit(x,y,1,full=True)[:2]" 
A = np.vstack((x, np.ones(n))).T
model, resid = np.linalg.lstsq(A, y)[:2]

r2 = 1 - resid / (y.size * y.var())
print r2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文