Python numpy.polyfit没有关键字';cov';

Python numpy.polyfit没有关键字';cov';,python,numpy,covariance,Python,Numpy,Covariance,我试图用多元拟合来找到一组数据的最佳拟合直线,但我还需要知道参数的不确定性,所以我也需要协方差矩阵。在线文档建议我写: 多边形拟合(x,y,2,cov=真) 但这就产生了错误: TypeError:polyfit()获得意外的关键字参数“cov” 当然,帮助(polyfit)没有显示关键字参数“cov” 那么在线文档是否引用了numpy的早期版本?(我有最新的1.6.1)。我可以编写自己的多边形拟合函数,但有人对为什么我的多边形拟合上没有协方差选项有什么建议吗 感谢来自库的解决方案,我发现使用是

我试图用多元拟合来找到一组数据的最佳拟合直线,但我还需要知道参数的不确定性,所以我也需要协方差矩阵。在线文档建议我写:

多边形拟合(x,y,2,cov=真)

但这就产生了错误:

TypeError:polyfit()获得意外的关键字参数“cov”

当然,帮助(polyfit)没有显示关键字参数“cov”

那么在线文档是否引用了numpy的早期版本?(我有最新的1.6.1)。我可以编写自己的多边形拟合函数,但有人对为什么我的多边形拟合上没有协方差选项有什么建议吗


感谢来自库的解决方案,我发现使用是一个方便的选择。在statsmodels中,回归对象具有返回参数和标准错误的可调用属性。我在下面举了一个例子,说明这将如何对您起作用:

# Imports, I assume NumPy for forming your data.
import numpy as np
import scikits.statsmodels.api as sm

# Form the data here
(X, Y) = ....

reg_x_data =  np.ones(X.shape);                      # 0th degree term. 
for ii in range(1,deg+1):
    reg_x_data = np.hstack(( reg_x_data, X**(ii) )); # Append the ii^th degree term.


# Store OLS regression results into `result`
result = sm.OLS(Y,reg_x_data).fit()


# Print the estimated coefficients
print result.params

# Print the basic OLS standard error in the coefficients
print result.bse

# Print the estimated basic OLS covariance matrix
print result.cov_params()   # <-- Notice, this one is a function by convention.

# Print the heteroskedasticity-consistent standard error
print result.HC0_se

# Print the heteroskedasticity-consistent covariance matrix
print result.cov_HC0
对于您的问题,您可以这样使用它:

import numpy as np

# Define or load your data:
(Y, X) = ....

# Desired polynomial degree
deg = 2;

reg_x_data =  np.ones(X.shape); # 0th degree term. 
for ii in range(1,deg+1):
    reg_x_data = np.hstack(( reg_x_data, X**(ii) )); # Append the ii^th degree term.


# Get all of the regression data.
beta, residuals, sig_hat, conv_st_error, sig_hat_asymptotic_variance, ols_cov, v_robust, v_modified_robust = ols_linreg(reg_x_data,Y)

# Print the covariance matrix:
print ols_cov

如果您在我的计算中发现任何错误(特别是异方差一致性估计),请告诉我,我会尽快修复。

这里是1.6中对
polyfit
的参考:。请注意,没有
cov
参数。您可能正在查看numpy 1.7()的文档,谢谢您的帮助,不过有几个bug。我想hastack应该是hstack。我还必须首先将X和Y转换为2D数组(即,shape=(2000,1),而不是(2000),然后我得到问题't2=(np.transpose(X)).dot(Y),ValueError:矩阵未对齐',因为reg_x_数据现在与Y的大小不同。是的,我刚才更改了
hastack
打字错误,我不确定您的屏幕是否没有刷新或其他内容。是的,我假设
x
Y
是二维数组。您可以在
ols\re的开头添加一些简单的代码g
根据需要将一维阵列重塑为二维阵列。我添加了一个使用scikits.statsmodels库的解决方案,以防您真的需要库解决方案。我还修改了代码,使其更加高效和矢量化。我已对照scikits.statsmodels回归结果检查了此处发布的函数,并且我的函数与在他们提供的几个测试数据集上,ir结果精确到小数点后6位。
import numpy as np

# Define or load your data:
(Y, X) = ....

# Desired polynomial degree
deg = 2;

reg_x_data =  np.ones(X.shape); # 0th degree term. 
for ii in range(1,deg+1):
    reg_x_data = np.hstack(( reg_x_data, X**(ii) )); # Append the ii^th degree term.


# Get all of the regression data.
beta, residuals, sig_hat, conv_st_error, sig_hat_asymptotic_variance, ols_cov, v_robust, v_modified_robust = ols_linreg(reg_x_data,Y)

# Print the covariance matrix:
print ols_cov