(x, axis=None, ddof=0)
return result
Equivalent of np.var that supports sparse and dense matrices.
Equivalent of np.var that supports sparse and dense matrices.
Equivalent of np . var that supports sparse and dense matrices .
def var(x, axis=None, ddof=0):
    """ Equivalent of np.var that supports sparse and dense matrices. """
    if not sp.issparse(x):
        return np.var(x, axis, ddof=ddof)

    result = x.multiply(x).mean(axis) - np.square(x.mean(axis))
    result = np.squeeze(np.asarray(result))

    # Apply correction for degrees of freedom
    n = np.prod(x.shape) if axis is None else x.shape[axis]
    result *= n / (n - ddof)

    return result
def var ( x , axis = None , ddof = 0 ) : if not sp . issparse ( x ) : return np . var ( x , axis , ddof = ddof ) result = x . multiply ( x ) . mean ( axis ) - np . square ( x . mean ( axis ) ) result = np . squeeze ( np . asarray ( result ) ) # Apply correction for degrees of freedom n = np . prod ( x . shape ) if axis is None else x . shape [ axis ] result *= n / ( n - ddof ) return result