我想使用numpy.polyfit
进行物理计算,因此我需要误差的大小。numpy.polyfit的错误是什么?
16
A
回答
20
如果您在您的来电polyfit
指定full=True
,将包括额外的信息:
>>> x = np.arange(100)
>>> y = x**2 + 3*x + 5 + np.random.rand(100)
>>> np.polyfit(x, y, 2)
array([ 0.99995888, 3.00221219, 5.56776641])
>>> np.polyfit(x, y, 2, full=True)
(array([ 0.99995888, 3.00221219, 5.56776641]), # coefficients
array([ 7.19260721]), # residuals
3, # rank
array([ 11.87708199, 3.5299267 , 0.52876389]), # singular values
2.2204460492503131e-14) # conditioning threshold
返回的剩余价值是配合误差的平方的总和,不知道这是你是什么后:
>>> np.sum((np.polyval(np.polyfit(x, y, 2), x) - y)**2)
7.1926072073491056
在1.7版本也有一个cov
关键字,将返回的协方差矩阵的系数,你可以用它来计算拟合系数本身的不确定性。
16
正如你可以在documentation看到:
Returns
-------
p : ndarray, shape (M,) or (M, K)
Polynomial coefficients, highest power first.
If `y` was 2-D, the coefficients for `k`-th data set are in ``p[:,k]``.
residuals, rank, singular_values, rcond : present only if `full` = True
Residuals of the least-squares fit, the effective rank of the scaled
Vandermonde coefficient matrix, its singular values, and the specified
value of `rcond`. For more details, see `linalg.lstsq`.
这意味着如果你可以做一个健康,我残差为:
import numpy as np
x = np.arange(10)
y = x**2 -3*x + np.random.random(10)
p, res, _, _, _ = numpy.polyfit(x, y, deg, full=True)
然后,p
是你适合的参数,如上所述,res
将是残差。 _
是因为您不需要保存最后三个参数,因此您可以将它们保存在您不会使用的变量_
中。这是一个惯例,不是必需的。
@海梅的答案解释了什么是残差的意思。你可以做的另一件事是将这些平方偏差看作一个函数(总和为res
)。这对于看到一个不够充分的趋势特别有用。 res
可以很大,因为统计噪声,或者可能是系统性差配件,例如:
x = np.arange(100)
y = 1000*np.sqrt(x) + x**2 - 10*x + 500*np.random.random(100) - 250
p = np.polyfit(x,y,2) # insufficient degree to include sqrt
yfit = np.polyval(p,x)
figure()
plot(x,y, label='data')
plot(x,yfit, label='fit')
plot(x,yfit-y, label='var')
所以在图中,记下显得格格不入附近x = 0
:
相关问题
- 1. numpy.polyfit和scipy.polyfit和有什么不一样?
- 2. 什么是错误
- 3. 什么是错误?
- 4. 我的错误是什么?
- 5. 什么是错误的HttpBasicAuthenticator
- 6. sfWidgetFormDoctrineChoice:错误,错误还是什么?
- 7. :什么是“execvp文件错误”错误?
- 8. MySQL错误1064 - 这是什么错误?
- 9. Java applet错误...什么是错的?
- 10. 什么是错的这个错误
- 11. 什么是错误um:loader_init_failure_status_dll_not_found_c0000135_asckc!nnknown?
- 12. 错误CS0201,这是什么?
- 13. 什么是“错误”行为?
- 14. 什么是NSXMLParserErrorDomain错误?
- 15. 什么是dwarfdump错误?
- 16. 什么是'总线错误?'
- 17. 这是什么CSS错误?
- 18. 什么是INSTALL_PARSE_FAILED_NO_CERTIFICATES错误?
- 19. 以下是什么错误?
- 20. 什么是ajax错误?
- 21. 什么是NSXMLParserErrorDomain错误1549
- 22. 什么是详细错误
- 23. 错误是什么意思?
- 24. 什么是monadic错误?
- 25. 什么是错误Java ArrayLists
- 26. 这个错误是什么?
- 27. 什么是分段错误?
- 28. 这是什么Agda错误?
- 29. 什么是错误3004?
- 30. 什么是错误Infobip
你知道如果np.polyfit使用TLS(总最小二乘法,也称为正交最小二乘法)或OLS(普通最小二乘法)来寻找最佳拟合? –