Ejemplo n.º 1
0
def zscore(arr, axis=None):
    """
    Z-score along the specified axis.
    
    Parameters
    ----------
    arr : ndarray
        Input array.
    axis : {int, None}, optional
        The axis along which to take the z-score. The default (None) is
        to find the z-score of the flattened array.
    
    Returns
    -------
    y : ndarray
        A copy normalized with the Z-score along the specified axis.
    
    Examples
    --------
    >>> arr = np.array([1, np.nan, 2, 3])
    >>> zscore(arr)
    array([-1.22474487,         NaN,  0.        ,  1.22474487])
        
    """
    arr = demean(arr, axis)
    if axis != 0 and not axis is None:
        ind = [slice(None)] * arr.ndim
        ind[axis] = np.newaxis
        arr /= nanstd(arr, axis)[ind]
    else:
        arr /= nanstd(arr, axis)   
    return arr             
Ejemplo n.º 2
0
 def test_nanstd_all(self):
     """Check nanstd when all values are nan."""
     s = nanstd(self.Xall)
     assert np.isnan(s)
Ejemplo n.º 3
0
 def test_nanstd_some(self):
     """Check nanstd when some values only are nan."""
     s = nanstd(self.Xsome)
     assert_approx_equal(s, np.std(self.Xsomet, ddof=0))
Ejemplo n.º 4
0
 def test_nanstd_none(self):
     """Check nanstd when no values are nan."""
     s = nanstd(self.X)
     assert_approx_equal(s, np.std(self.X, ddof=0))