def score(cm):
    """
    Compute the precision, the recall and the f1-score
    for the given confusion matrix

    Parameters
    ----------
        cm : (n, n) ndarray
            The input confusion matrix

    Returns
    -------
        score : (n 3) ndarray
            The array contains the tripletes [purity, recall, f1-score]
            for each class of objects represented in the confusion matrix

    Examples
    --------
    >>> print(cm)
    [[31633.,     0.,     0.]
     [ 3494.,  8902.,     0.]
     [    0.,   907.,  5064.]]

    >>> score(cm)
    [[0, 0.9005323540296638, 1.0, 0.9476632714200119],
     [1, 0.9075338974411254, 0.7181348822200709, 0.8018013960819635],
     [2, 1.0, 0.8480991458717133, 0.9178069777979158]]
    """
    scores = []
    for i in range(len(cm)):
        tp = cm[i][i]
        fp = 0
        fn = 0
        for j in range(len(cm)):
            if j != i:
                fp += cm[j][i]
                fn += cm[i][j]
        purity = tp / (tp + fp)
        compl = tp / (tp + fn)
        f1 = 2 * purity*compl / (purity+compl)
        scores.append([i, purity, compl, f1])
    return scores