简体   繁体   English

为什么numpy.std()使用abs()?

[英]Why does numpy.std() use abs()?

I checked the numpy library and found the following definition for the standard deviation in numpy : 我检查了numpy库并找到了numpy标准偏差的以下定义:

std = sqrt(mean(abs(x - x.mean())**2))

Why is the abs() function used? 为什么使用abs()函数? - Because mathematically the square of a number will be positive per definition. - 因为数学上每个定义的数字的平方是正的。

So I thought: 所以我认为:

abs(x - x.mean())**2 == (x - x.mean())**2

The square of a real number is always positive, but this is not true for complex numbers. 实数的平方总是正数,但对于复数而言则不然。

A very simple example: j**2=-1 一个非常简单的例子: j**2=-1

A more complex (pun intended) example: (3-2j)**2=(5-12j) 更复杂(双关语)的例子: (3-2j)**2=(5-12j)

From documentation: 来自文档:

Note that, for complex numbers, std takes the absolute value before squaring, so that the result is always real and nonnegative. 请注意,对于复数, std在求平方之前取绝对值,因此结果始终是实数且非负数。

Note: Python uses j for the imaginary unit, while mathematicians uses i . 注意:Python使用j作为虚构单位,而数学家使用i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM