Untitled

 avatar
unknown
python
2 years ago
647 B
5
Indexable
def calculate_purity(y_true, y_pred):
    conf_matrix=confusion_matrix(y_true, y_pred)
    Pj = conf_matrix.max(axis=0)
    Mj = conf_matrix.sum(axis=0)
    purity = np.sum(Pj)/np.sum(Mj)
    return purity

def calculate_gini_index(y_true, y_pred):
    conf_matrix=confusion_matrix(y_true, y_pred)
    print(conf_matrix)
    predicted_class_sq_sum = np.sum(conf_matrix**2, axis = 0)
    print(predicted_class_sq_sum)
    M_j = (np.sum(conf_matrix**2, axis = 0))**2
    print(M_j)
    G_i = 1-pred_sq_sum/M_j
    print(G_i)
    numerator = np.sum(G_i*M_j)
    denominator = np.sum(M_j)
    gini_wt_avg = numerator/denominator
    return gini_wt_avg
Editor is loading...