(self, y_test, y_pred, labels)
Calculate a confusion matrix and add it to the model as a data frame suitable for Qlik
Calculate a confusion matrix and add it to the model as a data frame suitable for Qlik
Calculate a confusion matrix and add it to the model as a data frame suitable for Qlik
def _prep_confusion_matrix(self, y_test, y_pred, labels):
        """
        Calculate a confusion matrix and add it to the model as a data frame suitable for Qlik
        """

        # Calculate confusion matrix and flatten it to a simple array
        if len(y_test.shape) == 1:
            confusion_array = metrics.confusion_matrix(y_test, y_pred).ravel()

            # Structure into a DataFrame suitable for Qlik
            result = []
            i = 0
            for t in labels:
                for p in labels:
                    result.append([str(t), str(p), confusion_array[i]])
                    i = i + 1
            self.model.confusion_matrix = pd.DataFrame(result, columns=["true_label", "pred_label", "count"])
            self.model.confusion_matrix.insert(0, "model_name", self.model.name)
        # Handle confusion matrix format for multi-label classification
        else:
            confusion_array = metrics.multilabel_confusion_matrix(y_test, y_pred)
            result = pd.DataFrame(confusion_array.reshape(-1, 4), columns=["true_negative", "false_positive", "false_negative", "true_positive"])
            self.model.confusion_matrix = pd.DataFrame(np.arange(len(confusion_array)), columns=["step"])
            self.model.confusion_matrix = pd.concat([self.model.confusion_matrix, result], axis=1)
            self.model.confusion_matrix.insert(0, "model_name", self.model.name)
def _prep_confusion_matrix ( self , y_test , y_pred , labels ) : # Calculate confusion matrix and flatten it to a simple array if len ( y_test . shape ) == 1 : confusion_array = metrics . confusion_matrix ( y_test , y_pred ) . ravel ( ) # Structure into a DataFrame suitable for Qlik result = [ ] i = 0 for t in labels : for p in labels : result . append ( [ str ( t ) , str ( p ) , confusion_array [ i ] ] ) i = i + 1 self . model . confusion_matrix = pd . DataFrame ( result , columns = [ "true_label" , "pred_label" , "count" ] ) self . model . confusion_matrix . insert ( 0 , "model_name" , self . model . name ) # Handle confusion matrix format for multi-label classification else : confusion_array = metrics . multilabel_confusion_matrix ( y_test , y_pred ) result = pd . DataFrame ( confusion_array . reshape ( - 1 , 4 ) , columns = [ "true_negative" , "false_positive" , "false_negative" , "true_positive" ] ) self . model . confusion_matrix = pd . DataFrame ( np . arange ( len ( confusion_array ) ) , columns = [ "step" ] ) self . model . confusion_matrix = pd . concat ( [ self . model . confusion_matrix , result ] , axis = 1 ) self . model . confusion_matrix . insert ( 0 , "model_name" , self . model . name )