def one_vs_rest_train(X, Y, train_fun):
    """Train a multi-class classifier using the one vs. rest strategy.

    Parameters
    ----------
    X : ndarray, shape (m, n)
        training features.
    Y : ndarray, shape (m,)
        training labels in the range 0, ..., (k - 1).
    train_fun : function that, given X and a set of binary labels, trains
        a binary classifier.  The function must return a tuple, where the
        last element is the list of loss values obtained during training,
        and all the others are the parameters of the trained classifier.

    Returns
    -------
    params : dict
        collection of (class, parameters) representing the binary classifiers.
    loss : ndarray, shape (steps,)
        average loss value after each training iteration.
    """
    k = Y.max() + 1
    m, n = X.shape
    loss = 0
    classifiers = {}
    for c in range(k):
        Ybin = Y == c
        result = train_fun(X, Ybin)
        classifiers[c] = result[:-1]
        loss = loss + result[-1]
    return classifiers, loss / k
