예제 #1
0
def __compute_zscore(data: np.ndarray, axis: int = -1, clip: float = 3.0):
    dims = data.ndim
    data[data == np.inf] = np.nan
    data[data == -np.inf] = np.nan
    if axis is None:
        length = 1
    else:
        if axis < 0:
            axis += dims
        length = data.shape[axis]
    for i in range(length):
        _slice = [i if j == axis else slice(None) for j in range(dims)]
        sliced = data.__getitem__(_slice)
        extremes = find_extreme_values(sliced)
        score = (sliced - np.nanmedian(sliced[~extremes])) / np.nanstd(sliced[~extremes])
        score = np.clip(score, -clip, clip)
        epsilon = 1e-4
        total_err = 1.0
        avg = np.nanmean(score[~extremes])
        sd = np.nanstd(score[~extremes])
        z = (score - avg) / sd
        trial = 0
        while total_err > epsilon and trial < 3:
            if (abs(z) >= clip).any():
                z = np.clip(z, -clip, clip)
            avg = np.nanmean(z)
            std = np.nanstd(z)
            total_err = abs(avg) + abs(std - 1)
            z = (z - avg) / std
            trial += 1
        data.__setitem__(_slice, z)
    return data