将Numpy Lstsq残值转换为R ^ 2 [英] Converting Numpy Lstsq residual value to R^2

查看:74
本文介绍了将Numpy Lstsq残值转换为R ^ 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行以下最小二乘回归(单变量).我想用R ^ 2表示结果的重要性. Numpy返回未缩放残差的值,这是将其标准化的明智方法.

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

推荐答案

请参见 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

这篇关于将Numpy Lstsq残值转换为R ^ 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆