示例#1
0
文件: farray.py 项目: josef-pkt/la
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             
示例#2
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
示例#3
0
 def test_nanstd_all(self):
     """Check nanstd when all values are nan."""
     s = nanstd(self.Xall)
     assert np.isnan(s)
示例#4
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))
示例#5
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))
示例#6
0
 def test_nanstd_all(self):
     """Check nanstd when all values are nan."""
     s = nanstd(self.Xall)
     assert np.isnan(s)
示例#7
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))
示例#8
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))