使用IRLS了解Scipy的最小二乘函数 [英] Understanding scipy's least square function with IRLS

查看:175
本文介绍了使用IRLS了解Scipy的最小二乘函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解此功能的工作方式时遇到了一些麻烦.

I'm having a bit of trouble understanding how this function works.

a, b = scipy.linalg.lstsq(X, w*signal)[0]

我知道signal是表示信号的数组,当前w只是[1,1,1,1,1...]

I know that signal is the array representing the signal and currently w is just [1,1,1,1,1...]

我应该如何操纵Xw模仿加权最小二乘或迭代重新加权最小二乘?

How should I manipulate X or w to imitate weighted least squares or iteratively reweighted least squared?

推荐答案

如果将x和y与sqrt(weight)乘积,则可以计算加权最小二乘. 您可以通过以下链接获取公式:

If you product X and y with sqrt(weight) you can calculate weighted least squares. You can get the formula by following link:

http://en.wikipedia.org/wiki/Linear_least_squares_%28mathematics% 29#Weighted_linear_least_squares

这是一个示例:

准备数据:

import numpy as np
np.random.seed(0)
N = 20
X = np.random.rand(N, 3)
w = np.array([1.0, 2.0, 3.0])
y = np.dot(X, w) + np.random.rand(N) * 0.1

OLS:

from scipy import linalg
w1 = linalg.lstsq(X, y)[0]
print w1

输出:

[ 0.98561405  2.0275357   3.05930664]

WLS:

weights = np.linspace(1, 2, N)
Xw = X * np.sqrt(weights)[:, None]
yw = y * np.sqrt(weights)
print linalg.lstsq(Xw, yw)[0]

输出:

[ 0.98799029  2.02599521  3.0623824 ]

通过statsmodels检查结果

Check result by statsmodels:

import statsmodels.api as sm
mod_wls = sm.WLS(y, X, weights=weights)
res = mod_wls.fit()
print res.params

输出:

[ 0.98799029  2.02599521  3.0623824 ]

这篇关于使用IRLS了解Scipy的最小二乘函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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