diff --git a/measure_model.py b/measure_model.py new file mode 100644 index 0000000000000000000000000000000000000000..617324f347a528f54ee2149ed5e8769227fc6d48 --- /dev/null +++ b/measure_model.py @@ -0,0 +1,51 @@ +import measures as ms + + + +class measure_model: + def __init__(self,clf_1,clf_2,x_test,y_test,x_1_test,y_1_test,x_2_test,y_2_test,single_classifier=True): + self.clf_1 = clf_1 #in case of group-specific classifiers, the classifier of group 1, otherwise the general classifier + self.clf_2 = clf_2 #in case of group-specific classifiers, the classifier of group 1, otherwise the general classifier + self.y_test = y_test + self.x_test = x_test + self.x_1_test = x_1_test + self.x_2_test = x_2_test + self.y_1_test = y_1_test + self.y_2_test=y_2_test + self.single_classifier=single_classifier + + + def get_measures(self): + labels,y_1,y_2,cm1,cm2=self.get_measures_by_sensitive_group() + IGQRs=self.get_IGQRs(y_1, y_2) + if self.single_classifier: + TN, FP, FN, TP =ms.get_confusion_matrix(self.y_test, self.clf_1.predict(self.x_test)) + labels,y,cm = ms.get_measure_values(TN, FP, FN, TP) + else: + #Situation of one classifier per group. The overall measure values are calculated by building the weighted mean of the group-specific values to support comparisons to + #single classifiers + w_1 = self.y_1_test.shape[0]/self.y_test.shape[0] + w_2 = self.y_2_test.shape[0]/self.y_test.shape[0] + y=[y_1[i]*w_1 + y_2[i]*w_2 for i in range(len(y_1))] + return labels,y,y_1,y_2,IGQRs,cm1,cm2 + + def get_measures_by_sensitive_group(self): + TN1, FP1, FN1, TP1 =ms.get_confusion_matrix(self.y_1_test, self.clf_1.predict(self.x_1_test)) + TN2, FP2, FN2, TP2 =ms.get_confusion_matrix(self.y_2_test, self.clf_2.predict(self.x_2_test)) + labels,y_1,cm1 = ms.get_measure_values(TN1, FP1, FN1, TP1) + labels,y_2,cm2 = ms.get_measure_values(TN2, FP2, FN2, TP2) + return labels,y_1,y_2,cm1,cm2 + + def get_IGQRs(self,y_1,y_2): + ''' + Calculation of the IGQR values based on the definition from the thesis. An IGQR is further invalid (encoded by -1) if one of the group-specific quality measures is invalid. + ''' + IGQRs=[] + for i in range(len(y_1)): + if y_1[i]==0 and y_2[i]==0: + IGQRs.append(1) + elif y_1[i]!=-1 and y_2[i]!=-1: + IGQRs.append(min(y_1[i],y_2[i])/max(y_1[i],y_2[i])) + else: + IGQRs.append(-1) + return IGQRs \ No newline at end of file