numpy-mean/var

numpy.mean(a, axis=None, dtype=None, out=None, skipna=False, keepdims=False)

#数组:
>>> a = array([[1, 2], [3, 4]])
>>> mean(a)#所有元素平均
2.5
>>> mean(a, axis=0)#纵轴
array([ 2.,  3.])
>>> mean(a, axis=1)#横轴
array([ 1.5,  3.5])
#矩阵:
>>> a = mat([[1, 2], [3, 4]])
>>> mean(a)
2.5
>>> mean(a,axis=0)
matrix([[ 2.,  3.]])
>>> mean(a, axis=1)
matrix([[ 1.5],
        [ 3.5]])
#应用:
>>> ylist=[1,2,3,4,5]
>>> ymat=mat(ylist).T
>>> mean(ymat,0)
matrix([[ 3.]])
>>> ymat-mean(ymat,0)
matrix([[-2.],
        [-1.],
        [ 0.],
        [ 1.],
        [ 2.]])

var:在MATLAB中,计算方差结果为总体方差。而在Python中,直接采用var计算样本的二阶中心矩;只有使用参数ddof = 1时才计算样本方差,计算结果才与MATLAB的var公式计算结果相同。

>>> a = np.arange(1,11)
>>> var(a)
8.25
>>var(a,ddof = 1)
9.1666666666666661



你可能感兴趣的:(python-numpy)