def purity(label_pred, label_true, Ia_flag=0):
    """Calculate purity.

       input: label_pred, predicted labels
              label_true, true labels
              Ia_flag, Ia flag (optional, default is 0)

       output: purity
    """

    cc_Ia = sum([label_pred[i] == label_true[i] and label_true[i] == Ia_flag for i in range(len(label_pred))])
    wr_nIa = sum([label_pred[i] != label_true[i] and label_true[i] != Ia_flag for i in range(len(label_pred))])

    if cc_Ia + wr_nIa > 0:
        return float(cc_Ia)/(cc_Ia + wr_nIa)
    else:
        return 0