从文件中读取方差计算
@Jerry Coffin
我明白了逻辑, while(File>>value)//当刚刚从文件中获取的输入为 true 时 ....进行计算。 然而,当我实现这个时,计数器只转到 1 & 。它的价值非常高。有时是错误的,但我不知道是什么。该文件有效
File.open(FileName, ifstream::in);
while(File>>value){
++counter;
sum += value;
sumsqr+= value * value;
}
average=sum/counter;
variance = sumsqr/counter - average*average;
File.close();
,这是我使用“text.txt”的输入文件的内容 23244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 1486415241250586205864104818638684840823244564 14864152412505862058641048186386848408
@Jerry Coffin
I get the logic,
while(File>>value)//while input just taken from file is true
.... do computation.
Yet when I implemented this the counter only went to 1 & it's value was very high. Sometime is wrong, but I have no idea what. The file is valid
File.open(FileName, ifstream::in);
while(File>>value){
++counter;
sum += value;
sumsqr+= value * value;
}
average=sum/counter;
variance = sumsqr/counter - average*average;
File.close();
here's the contents of the input file I am using "text.txt"
23244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
1486415241250586205864104818638684840823244564
14864152412505862058641048186386848408
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可悲的是,(至少)三个答案引用了您的 while (!File.eof()) ,而没有评论这完全是错误的事实。你想要的是这样的:
使用
while (!File.eof())
的错误是阴险的——你通常会得到看起来合理的结果,实际上相当< /em> 接近正确。问题是,直到您尝试从文件中读取数据之后,eof()
才会变为 true,并且尝试读取失败。当它失败时,value
仍将具有您读取的最后一个值,因此它的行为就像列表中的最后一个数字确实出现了两次(例如,如果您的文件包含21个数字,则您的循环将执行 22 次,在第 22 次迭代时,它将再次使用第 21 次数字)。这会稍微影响你的计算,但通常还不足以立即显而易见——几乎是最糟糕的错误类型。编辑:这是一个完整的测试程序:
这里有一些测试数据:
当我对该数据运行此程序时,我得到:
交叉检查,我使用两组数据在 Excel 中进行了计算:
作为 每列都是公式对前面的数据执行“VARP”的结果。请注意,我的函数与 Excel 为正确的输入数据生成的函数相匹配。使用
while (!file.eof())
的函数与 Excel 生成的结果相匹配,并重复最后一个数字。我什至无法开始猜测发生了什么使循环仅运行一次并读取错误的值。如果无法猜测或重现问题,恐怕我无法提供太多有关如何解决问题的有用建议。
Sadly, (at least) three answers have quoted your
while (!File.eof())
without commenting on the fact that this is just plain wrong. What you want is something like this:The bug from using
while (!File.eof())
is insidious -- you'll typically get results that look reasonable, and are actually fairly close to correct. The problem is thateof()
doesn't become true until after you've attempted to read from the file, and the attempted read has failed. When it fails,value
will still have the last value you read, so it'll act like the last number in the list was really there twice (e.g., if your file contained 21 numbers, your loop would execute 22 times, and on the 22nd iteration, it would use the 21st number again). This will throw your calculations off a bit, but usually not enough that it's immediately obvious -- nearly the worst possible kind of bug.Edit: Here's a complete test program:
Here's some test data to go with:
When I run this on that data, I get:
As a cross-check, I did the computation in Excel, using two sets of data:
The last line in each column is the result of a formula doing "VARP" on the preceding data. Note that my function matches with what Excel produces for the correct input data. The function using
while (!file.eof())
matches with what Excel produces with the last number duplicated.I can't even begin to guess what's happening to make the loop run only once and read an incorrect value. Without being able to either guess at or reproduce the problem, I'm afraid I can't provide much in the way of useful suggestions about how to fix it.
您对方差的计算完全错误。用统计术语来说,方差是
所以摆脱第二个循环(我什至不确定你认为它的作用)并将第一个循环更改为:
编辑:Jerry Coffin 的 答案甚至更好,因为它演示了
eof()
的问题。Your computation of variance is totally incorrect. In statistical terms, variance is
So get rid of that second loop (I'm not even sure what you think it does) and change the first loop to:
EDIT: Jerry Coffin's answer is even better as it demonstrates the issue with
eof()
.你可以这样写
you can write like that
在第二个
!File.eof()
循环中,您没有从文件中读取。方差不是数值与平均值之差的平方和吗?您的循环根本不查看文件中的值。此外,使用整数变量表示总和、平均值和方差可能会导致不准确;您可能需要使用double
来代替。In your second
!File.eof()
loop, you are not reading from the file. Isn't the variance the sum of the squares of the differences between values and the average? Your loop doesn't look at the values from the file at all. Also, using integer variables for the sum, average, and variance is likely to lead to inaccuracy; you might wantdouble
for those instead.上面几行似乎没有多大意义。您没有在该 while 块中阅读任何内容。该 while 块预计不会终止。
The above lines don't appear to make much sense. You are not reading anything in that while block. This while block isn't expected to terminate.
好吧,如果问题不限制您可以使用哪些库,我建议使用 Boost Accumulators 使此类事情变得微不足道。
您可以获得方差、均值以及您想要的任何其他基本统计值。他们在使用
long double
时遇到一些问题,但除此之外他们都很棒!Well, if the question doesn't limit what libraries you can use I would suggest using the Boost Accumulators which make this type of thing trivial.
You get variance, mean, and whatever other basic statistical value you desire. They have a few issues working with
long double
, but otherwise they are great!