精度,为什么Matlab和Python numpy给出如此不同的输出? [英] Precision, why do Matlab and Python numpy give so different outputs?

查看:659
本文介绍了精度,为什么Matlab和Python numpy给出如此不同的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解基本数据类型,并且浮点类型(float,double)不能完全容纳某些数字.

I know about basic data types and that float types (float,double) can not hold some numbers exactly.

但是,在将某些代码从Matlab移植到Python(Numpy)时,我发现计算存在一些显着差异,并且我认为它已经回到了精度上.

In porting some code from Matlab to Python (Numpy) I however found some significant differences in calculations, and I think it's going back to precision.

采取以下代码,仅对前两个元素具有非零值的z维归一化500维向量.

Take the following code, z-normalizing a 500 dimensional vector with only first two elements having a non-zero value.

Matlab:

Z = repmat(0,500,1); Z(1)=3;Z(2)=1;
Za = (Z-repmat(mean(Z),500,1)) ./ repmat(std(Z),500,1);
Za(1)
>>> 21.1694

Python:

from numpy import zeros,mean,std
Z = zeros((500,))
Z[0] = 3
Z[1] = 1
Za = (Z - mean(Z)) / std(Z)
print Za[0]
>>> 21.1905669677

除了格式在Python中显示的位数更多外,还有很大的差异(imho),大于0.02

Besides that the formatting shows a bit more digits in Python, there is a huge difference (imho), more than 0.02

Python和Matlab都使用64位数据类型(afaik). Python使用'numpy.float64'和Matlab'double'.

Both Python and Matlab are using a 64 bit data type (afaik). Python uses 'numpy.float64' and Matlab 'double'.

为什么差异如此之大?哪个更正确?

Why is the difference so huge? Which one is more correct?

推荐答案

也许差异来自meanstd调用.首先比较那些.

Maybe the difference comes from the mean and std calls. Compare those first.

std有几种定义,有些使用

There are several definitions for std, some use the sqaure root of

1 / n * sum((xi - mean(x)) ** 2)

其他人使用

1 / (n - 1) * sum((xi - mean(x)) ** 2)

相反.

从数学角度来看:这些公式是正态分布随机变量方差的估计量.该分布具有两个参数sigmamu.如果您知道mu恰恰是sigma ** 2的最佳估计量,则是

From a mathematical point: these formulas are estimators of the variance of a normal distributed random variable. The distribution has two parameters sigma and mu. If you know mu exactly the optimal estimator for sigma ** 2 is

1 / n * sum((xi - mu) ** 2)

如果必须使用mu = mean(xi)从数据中估算mu,则sigma**2的最佳估算器为

If you have to estimate mu from the data using mu = mean(xi), the optimal estimator for sigma**2 is

1 / (n - 1) * sum((xi- mean(x))**2)

这篇关于精度,为什么Matlab和Python numpy给出如此不同的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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