Tuesday, July 9, 2019

alpha evaluation for multilabel

from __future__ import division
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#reading the data



def alpha_evaluation(predicted_df,test_df,alpha=0,beta=1,gamma=1):
    y_pred=predicted_df
    y_test = test_df
    y_pred[y_pred>=0.5] = 1
    y_pred[y_pred<0.5] = 0

    Mx=0  # number of misclassified
    Fx=0  # number of false predicted 

#print type(y_pred)
#print len(y_pred.index)
#print y_pred
#print y_test
#alpha=0
#beta=1
#gamma =1
    pred_class = [0] * len(y_pred.columns)
    test_class = [0] * len(y_test.columns)
    pscore_class = [0] * len(y_test.columns)
    rscore_class = [0] * len(y_test.columns)
    score_row = [0] * len(y_pred.index)

    #print ("pred_class is ", pred_class)
    for i in range(0, len(y_pred.index)):
        Mx=0
        Fx=0
        Sx =0
        Cx=0
        score =0
        selected_class=0
    # print("Row is ",i)
        for j in range(0, len(y_pred.columns)):
            #print ("Column is ",j)
            if y_pred.values[i,j] == 1:
                pred_class[j] +=1
                selected_class = j
            if y_test.values[i,j] == 1:
                test_class[j] +=1
                    #print (y_pred.values[i,j], y_test.values[i,j])
            if y_pred.values[i,j] == 0 and y_test.values[i,j] == 1:
                Mx += 1
                #print ("mx")
            elif  y_pred.values[i,j] == 1 and y_test.values[i,j] == 0:
                Fx += 1
                #print ("fx")
            elif y_pred.values[i,j] == 1 and y_test.values[i,j] == 1:
                Sx +=1
                #print ("Sx")
            if y_pred.values[i,j] == 1 or y_test.values[i,j] == 1:
                Cx +=1
                #print ("Cx")
        print (Mx, Fx,Sx,Cx)
       
        if Sx == 0:
            score =0
        else:
            temp = 1 - (((beta * Mx) + (gamma *Fx))/Cx)
            #print("fx is ",Fx)
            #print("gama into fx  and divided by 2 is ",(gamma * Fx)/2)
            #print ("temp is ",temp)
            score = pow(temp,alpha)
        #print ("score is ",score)
       
        score_row[i] +=score
    for i in range(0, len(y_pred.index)):
        for j in range(0, len(y_pred.columns)):
            if y_pred.values[i,j] == 1:
                pscore_class[j] += score_row[i]
            if y_test.values[i,j] == 1:
                rscore_class[j] += score_row[i]
    #print ("pscore_class is ", pscore_class)
    #print ("rscore_class is ", rscore_class)
    #print ("pred_class is ", pred_class) 
    #print ("test_class is ", test_class)
   
    print(" class,  precision, recall")
    for j in range(0, len(y_pred.columns)):
        print(j, pscore_class[j]/pred_class[j], rscore_class[j]/test_class[j])
    del y_pred
    del y_test
   

y_pred1 = pd.read_csv('y_pred1.csv',header=None)
y_test1 = pd.read_csv('y_test.csv',header=None)

alpha_evaluation(y_pred1,y_test1,alpha=1,beta = 1/4)

No comments:

Post a Comment