Esempio n. 1
0
    def test_create_confusion_matrix(self):
        #Initialization
        truth = np.random.choice([0, 1, 2], 100, replace=True)
        pred = np.random.choice([0, 1, 2], 100, replace=True)

        ca = ClassifierAnalyzer(truth, pred)

        self.assertEqual(len(ca.confusion_matrix.shape), 2)
Esempio n. 2
0
    def test_micro_averages(self):
        #Initialization
        truth = np.array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1])
        pred = np.array([2, 2, 2, 3, 1, 3, 2, 3, 1, 1])

        ca = ClassifierAnalyzer(truth, pred)

        #Check Accuracy
        self.assertAlmostEqual(ca.calc_accuracy("micro"), 0.3, places=4)

        #check precision
        self.assertAlmostEqual(ca.calc_precision("micro"), 0.3, places=4)

        #check recall
        self.assertAlmostEqual(ca.calc_recall("micro"), 0.3, places=4)

        #check f1
        self.assertAlmostEqual(ca.calc_f1_score("micro"), 0.3, places=4)
Esempio n. 3
0
    def test_single_class_methods(self):
        #Initialization
        truth = np.array([1, 1, 0, 0, 1, 1, 1, 1, 1, 0])
        pred = np.array([0, 1, 1, 0, 0, 1, 1, 1, 0, 0])

        ca = ClassifierAnalyzer(truth, pred)

        #Check Accuracy
        self.assertAlmostEqual(ca.calc_accuracy(), 0.6, places=4)

        #check precision
        self.assertAlmostEqual(ca.calc_precision(), 0.8, places=4)

        #check recall
        self.assertAlmostEqual(ca.calc_recall(), 4 / 7, places=4)

        #check f1
        self.assertAlmostEqual(ca.calc_f1_score(), 2 / 3, places=4)
Esempio n. 4
0
    CPreds = CKNN.test()

    EKNN = EditedKNN(test1.drop(class_col, axis=1),
                     test2,
                     train,
                     k_vals,
                     contAttr,
                     discAttr,
                     unknown_col=class_col,
                     val_unknown_col=class_col,
                     predictionType="classification")
    EKNN.train()
    EPreds = CKNN.test()
    output_json[iter_num] = {}
    for i, k in enumerate(k_vals):
        KClassAnalyzer = ClassifierAnalyzer(test1[class_col], KPreds[:, i])
        CClassAnalyzer = ClassifierAnalyzer(test1[class_col], CPreds[:, i])
        EClassAnalyzer = ClassifierAnalyzer(test1[class_col], EPreds[:, i])

        output_json[iter_num][k] = {}

        #KNN
        output_json[iter_num][k]["KNN"] = {
            "F1": KClassAnalyzer.calc_f1_score("macro"),
            "P": KClassAnalyzer.calc_f1_score("macro"),
            "R": KClassAnalyzer.calc_f1_score("macro"),
            "A": KClassAnalyzer.calc_f1_score("macro"),
        }

        #CNN
        output_json[iter_num][k]["CNN"] = {
Esempio n. 5
0
        trainBin = BinDiscretizer(
            train[dataRetriever.getContinuousAttributes()], multi=True)

        trainBin.train_multi()
        train[dataRetriever.getContinuousAttributes()] = trainBin.fit_multi(
            train[dataRetriever.getContinuousAttributes()])
        test[dataRetriever.getContinuousAttributes()] = trainBin.fit_multi(
            test[dataRetriever.getContinuousAttributes()])

        naiveBayes = NaiveBayes(train, dataClass)

        answers = test[dataClass].to_numpy()[:]
        test = test.drop(columns=dataClass)
        predictions = naiveBayes.test(test)

        classifierAnalyzer = ClassifierAnalyzer(answers, predictions)

        jsonResults1[dataSet][foldNum] = {}
        jsonResults1[dataSet][foldNum][
            "Precision"] = classifierAnalyzer.calc_precision(method=method)
        jsonResults1[dataSet][foldNum][
            "Recall"] = classifierAnalyzer.calc_recall(method=method)
        jsonResults1[dataSet][foldNum][
            "Accuracy"] = classifierAnalyzer.calc_accuracy(method=method)
        jsonResults1[dataSet][foldNum][
            "F1Score"] = classifierAnalyzer.calc_f1_score(method=method)
        foldNum += 1

        calculateResults(predictions, answers)
        # print("Trained Model for Control:")
        # pprint.pprint(naiveBayes.trainedCalculation)
Esempio n. 6
0
    k_vals = list(output_json[str(iter_num)].keys())
    k_vals_int = [int(k) for k in k_vals]
    k_vals_int[-1] = 18

    print(f"Fold {iter_num}")
    #Fold 1
    centroidsKNN = KNearestNeighbor(test1.drop(class_col, axis=1), centroidsTrain, k_vals_int, contAttr, discAttr, unknown_col=class_col, predictionType="classification")
    centPreds = centroidsKNN.test()

    medoidsKNN = KNearestNeighbor(test1.drop(class_col, axis=1), medoidsTrain, k_vals_int, contAttr, discAttr, unknown_col=class_col, predictionType="classification")
    medPreds =medoidsKNN.test()

    
    # output_json[iter_num] = {}
    for i, k in enumerate(k_vals):
        CentClassAnalyzer = ClassifierAnalyzer(test1[class_col], centPreds[:,i])
        MedClassAnalyzer = ClassifierAnalyzer(test1[class_col], medPreds[:,i])


        
        # output_json[iter_num][k] = {}

        #Centroids
        output_json[str(iter_num)][str(k)]["cent"] = {
            "F1": CentClassAnalyzer.calc_f1_score("macro"),
            "P": CentClassAnalyzer.calc_f1_score("macro"),
            "R": CentClassAnalyzer.calc_f1_score("macro"),
            "A": CentClassAnalyzer.calc_f1_score("macro"),
        }

        #Medoids