gpt4 book ai didi

python - 将 Numpy Lstsq 残值转换为 R^2

转载 作者:太空狗 更新时间:2023-10-29 18:21:43 25 4
gpt4 key购买 nike

我正在执行如下的最小二乘回归(单变量)。我想用 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

最佳答案

参见 http://en.wikipedia.org/wiki/Coefficient_of_determination

你的 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

关于python - 将 Numpy Lstsq 残值转换为 R^2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3054191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com