def standardize(arr, stats=None):
    """Standardize the input.

    Parameters
    ----------
    arr : numpy.ndarray
        An array to be standardized.
    stats : tuple of float, default None
        Average and standard deviation used to standardize the arr.
        If None, those of the arr are used.

    Returns
    -------
    arr_std : numpy.ndarray
        A standardized array
    stats : tuple of float
        Average and standard deviation used to standardize the input
        array.
    """
    if arr.ndim > 2:
        raise ValueError("'arr' must be 1D or 2D.")

    if stats is None:
        if arr.ndim == 1:
            stats = (arr.mean(), arr.std())
        else:
            stats = (arr.mean(axis=1), arr.std(axis=1))

    if arr.ndim == 1:
        arr_std = (arr - stats[0]) / stats[1]
    else:
        arr_std = (arr - stats[0].reshape(-1, 1)) / stats[1].reshape(-1, 1)

    return arr_std, stats