Coefficient of Determination(R Squared)(How to determine goodness of fit)?
来源:互联网 发布:fluent python mobi 编辑:程序博客网 时间:2024/04/28 03:37
# quote from 'introduction to computation and programming # using Python, revised, MIT press import pylabdef getTrajectoryData(fileName): dataFile = open(fileName, 'r') distances = [] heights1, heights2, heights3, heights4 = [], [], [], [] discardHeader = dataFile.readline() for line in dataFile: d, h1, h2, h3, h4 = line.split() distances.append(float(d)) heights1.append(float(h1)) heights2.append(float(h2)) heights3.append(float(h3)) heights4.append(float(h4)) dataFile.close() return (distances, [heights1, heights2, heights3, heights4]) def processTrajectories(fileName): distances, heights = getTrajectoryData(fileName) numTrials = len(heights) #heights is a 'list of lists' distances = pylab.array(distances) #Get array containing mean height at each distances totHeights = pylab.array([0]*len(distances)) for h in heights: totHeights = totHeights + pylab.array(h) meanHeights = totHeights/len(heights) pylab.title('Trajectory fo Projectile (Mean of '\ + str(numTrials) + ' Trials)') pylab.xlabel('Inches from Launch Point') pylab.ylabel('Inches Above Launch Point') pylab.plot(distances, meanHeights, 'bo') #linear fit a,b = pylab.polyfit(distances, meanHeights, 1) altitudes = a*distances + b pylab.plot(distances, altitudes, 'b', label = 'Linear Fit') print 'RSquare of linear fit =', rSquared(meanHeights, altitudes) #quadratic fit a,b,c = pylab.polyfit(distances, meanHeights, 2) altitudes = a*(distances**2) + b*distances + c pylab.plot(distances, altitudes, 'b:', label = 'Quadratic Fit') print 'RSquare of quadratic fit =', rSquared(meanHeights, altitudes) pylab.legend()def rSquared(measured, predicted): """Assumes measured a one-dimensional array of measured values predicted a one-dimensional array of predicted values Returns coefficient of determination""" #square error(cost function) estimateError = ((predicted - measured)**2).sum() meanOfMeasured = measured.sum()/float(len(measured)) variability = ((measured - meanOfMeasured)**2).sum() return 1 - estimateError/variability
%run "C:\Users\Administrator\test.py"
RSquare of linear fit = 0.0177433205441
RSquare of quadratic fit = 0.985765369287
0 0
- Coefficient of Determination(R Squared)(How to determine goodness of fit)?
- 判定系数推导 — Coefficient of Determination Derivation
- 统计学 决定系数(Coefficient of Determination) 和 相关系数 (Correlation of Coefficient )
- How to determine the size of an object of class?
- 重学 Statistics,Cha12 Tests of Goodness of Fit and Independence
- How to determine which version of .net framework are installed
- Self-Service Linux(R) : Mastering the Art of Problem Determination
- 关于卡方检验(Chi-square test/Chi-Square Goodness-of-Fit Test)的基础及实例
- How to Determine if Two Words Are Anagrams of Each Other in C# (转)
- How to determine the size of a class/struct in the C++?
- Coefficient of Variation(CV) DEMO
- State of Charge (SOC) Determination
- How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer
- CWE-469: Use of Pointer Subtraction to Determine Size
- The logical process of account assignment determination
- How to Get the Frequency Table of a Categorical Variable as a Data Frame in R
- How would a model change if we minimized absolute error instead of squared error? What about the oth
- How To Determine Granule Size
- 海量数据的排序
- short s=5;s=s+2跟s+=2的区别
- 图像处理------简单数字水印 - 文字轧花效果
- LeetCode 之 Binary Tree Preorder Traversal(树)
- NSFileManager文件操作的十个小功能
- Coefficient of Determination(R Squared)(How to determine goodness of fit)?
- CEAN.js (Couchbase + Express + AngularJS + Node.js示例)
- Android NDK开发环境配置
- javascript 小练习-tab切换
- 程序优化之循环展开
- loadrunner 相关的一些 连接
- 触发器启用
- 如何使用本文档
- JSP数据交换的形式