Example #1
0
    def __test_support_vector_machine(self):
        if self.__trained_weights is not None and self.__x_test is not None and self.__y_test is not None:

            print("Testing SupportVectorMachine")

            y_test_predicted = np.array([])

            # for i in range(self.x_test.shape[0]):
            for r in self.__x_test:
                y_predict = np.sign(np.dot(r, self.__trained_weights))
                y_test_predicted = np.append(y_test_predicted, y_predict)

            accuracy_score = Utils.calculate_accuracy_score(
                self.__y_test, y_test_predicted)
            print("accuracy of the model: {}".format(accuracy_score))

            confusion_mat = Utils.calculate_confusion_matrix(
                self.__y_test, y_test_predicted)
            print("SVM confusion Matrix")
            print(confusion_mat[0])
            print(confusion_mat[1])

            # write accuracy and confusion matrix to file

            try:
                report_fl_name = "C:/Users/Brijesh Prajapati/Documents/Projects/Autism_Detection_Hons_Proj/Classifiers/" \
                                 "Classifier_Reports/svm_current_report.json"

                classifier_desc = "SVM l_r: " + str(
                    Learning_rate) + ", r_s: " + str(Reg_strength)
                dict_report = {
                    "desc": classifier_desc,
                    "accuracy_score": str(accuracy_score),
                    "tp": str(confusion_mat[0][0]),
                    "fn": str(confusion_mat[0][1]),
                    "fp": str(confusion_mat[1][0]),
                    "tn": str(confusion_mat[1][1]),
                    "specificity": str(confusion_mat[2][0]),
                    "sensitivity": str(confusion_mat[2][1])
                }

                with open(report_fl_name, 'w') as report_fl:
                    report_fl.write(json.dumps(dict_report))
            except Exception as ex:
                print("Exception occurred saving SVM report", ex)

            # print("SK Learn Metrics")
            # print(confusion_matrix(self.y_test, y_test_predicted))
            # print(classification_report(self.y_test, y_test_predicted))
        else:
            print("Train SVM before Testing.")
Example #2
0
    def __test_adaboost(self):
        print("Testing AdaBoost")

        y_predictions = np.ones(len(self.__x_test))
        for test, i in zip(self.__x_test, range(len(self.__x_test))):
            test = np.reshape(test, (-1, len(test)))
            y_predictions[i] = self.predict_sample_adaboost(test)

        accuracy_score = Utils.calculate_accuracy_score(
            self.__y_test, y_predictions)
        print("Accuracy: ", accuracy_score)

        cf = Utils.calculate_confusion_matrix(self.__y_test, y_predictions)
        print("Adaboost Matrix")
        print(cf[0])
        print(cf[1])

        # write accuracy and confusion matrix to file

        try:
            report_fl_name = "C:/Users/Brijesh Prajapati/Documents/Projects/Autism_Detection_Hons_Proj/Classifiers/" \
                             "Classifier_Reports/adaboost_current_report.json"

            classifier_desc = "Adaboost number of stumps: " + str(
                self.__num_classifiers)
            dict_report = {
                "desc": classifier_desc,
                "accuracy_score": str(accuracy_score),
                "tp": str(cf[0][0]),
                "fn": str(cf[0][1]),
                "fp": str(cf[1][0]),
                "tn": str(cf[1][1]),
                "specificity": str(cf[2][0]),
                "sensitivity": str(cf[2][1])
            }

            with open(report_fl_name, 'w') as report_fl:
                report_fl.write(json.dumps(dict_report))
        except Exception as ex:
            print("Exception occurred saving adaboost report", ex)
Example #3
0
    def test_main_classifier(self):
        self.__load_classifiers()

        y_pred = []
        for test_data in self.__x_test:
            svm_p = self.svm.predict_sample([test_data])
            rf_p = self.random_forest.predict([test_data])
            mlp_p = self.multi_layer_perceptron.predict(test_data)
            adb_p = self.adaboost.predict_sample_adaboost(np.array([test_data]))

            predictions = [float(svm_p), rf_p[0], mlp_p[0], adb_p[0]]

            y_pred.append(most_common_label(predictions))

        accuracy_score = Utils.calculate_accuracy_score(self.__y_test, y_pred)
        print("Accuracy of main model: ", accuracy_score)

        confusion_mat = Utils.calculate_confusion_matrix(self.__y_test, y_pred)
        print("Main Classifier Confusion Matrix")
        print(confusion_mat[0])
        print(confusion_mat[1])

        # write accuracy and confusion matrix to file

        try:
            report_fl_name = "C:/Users/Brijesh Prajapati/Documents/Projects/Autism_Detection_Hons_Proj/Classifiers/" \
                             "Classifier_Reports/mainmodel_current_report.json"

            classifier_desc = "Main Model"
            dict_report = {"desc": classifier_desc, "accuracy_score": str(accuracy_score),
                           "tp": str(confusion_mat[0][0]), "fn": str(confusion_mat[0][1]),
                           "fp": str(confusion_mat[1][0]), "tn": str(confusion_mat[1][1]),
                           "specificity": str(confusion_mat[2][0]), "sensitivity": str(confusion_mat[2][1])}

            with open(report_fl_name, 'w') as report_fl:
                report_fl.write(json.dumps(dict_report))
        except Exception as ex:
            print("Exception occurred saving Main Model report", ex)
    def __test_MultiLayerPerceptron(self):
        print("Testing Multi Layer Perceptron")
        mlp_predictions = self.__mlp_classifier.predict(self.__x_test)

        accuracy_score = Utils.calculate_accuracy_score(
            self.__y_test, mlp_predictions)
        print("Accuracy: ", accuracy_score)

        cf = Utils.calculate_confusion_matrix(self.__y_test, mlp_predictions)
        print("Multi Layer Perceptron Confusion Matrix")
        print(cf[0])
        print(cf[1])

        print(classification_report(self.__y_test, mlp_predictions))

        # write accuracy and confusion matrix to file

        try:
            report_fl_name = "C:/Users/Brijesh Prajapati/Documents/Projects/Autism_Detection_Hons_Proj/Classifiers/" \
                             "Classifier_Reports/mlp_current_report.json"

            classifier_desc = "MLP hidden_layer_size: " + str(self.__hidden_layer_size) + ", max_iteration: " + \
                              str(self.__max_iter)
            dict_report = {
                "desc": classifier_desc,
                "accuracy_score": str(accuracy_score),
                "tp": str(cf[0][0]),
                "fn": str(cf[0][1]),
                "fp": str(cf[1][0]),
                "tn": str(cf[1][1]),
                "specificity": str(cf[2][0]),
                "sensitivity": str(cf[2][1])
            }

            with open(report_fl_name, 'w') as report_fl:
                report_fl.write(json.dumps(dict_report))
        except Exception as ex:
            print("Exception occurred saving MLP report", ex)
    def __test_random_forest(self):
        print("Testing Random Forest Classifier")
        y_predictions = self.predict(self.__x_test)

        accuracy_score = Utils.calculate_accuracy_score(
            self.__y_test, y_predictions)
        print("Accuracy: ", accuracy_score)

        cf = Utils.calculate_confusion_matrix(self.__y_test, y_predictions)
        print("Random Forest Confusion Matrix")
        print(cf[0])
        print(cf[1])

        # write accuracy and confusion matrix to file

        try:
            report_fl_name = "C:/Users/Brijesh Prajapati/Documents/Projects/Autism_Detection_Hons_Proj/Classifiers/" \
                             "Classifier_Reports/rf_current_report.json"

            classifier_desc = "RF number of trees: " + str(self.__num_tree) + ", max depth: " + \
                              str(self.__max_depth)
            dict_report = {
                "desc": classifier_desc,
                "accuracy_score": str(accuracy_score),
                "tp": str(cf[0][0]),
                "fn": str(cf[0][1]),
                "fp": str(cf[1][0]),
                "tn": str(cf[1][1]),
                "specificity": str(cf[2][0]),
                "sensitivity": str(cf[2][1])
            }

            with open(report_fl_name, 'w') as report_fl:
                report_fl.write(json.dumps(dict_report))
        except Exception as ex:
            print("Exception occurred saving random forest report", ex)
Example #6
0
        #     return True
        # else:
        #     return False
        return self.value is not None


if __name__ == "__main__":
    df = pd.read_csv("D:/TrainingDataset_YEAR_PROJECT/TrainingSet.csv")

    # replace labels
    df['feature_class'].replace({'ASD': 1.0, 'TD': -1.0}, inplace=True)

    X_train, X_test, y_train, y_test = Utils.train_test_split(df, 0.2)

    X_train = X_train.values
    X_test = X_test.values
    y_train = y_train.values
    y_test = y_test.values

    decision_tree = DecisionTree(max_depth=10)
    decision_tree.train_decision_tree(X_train, y_train)

    y_pred = decision_tree.predict(X_test)

    accuracy_score = Utils.calculate_accuracy_score(y_test, y_pred)
    print("Accuracy: ", accuracy_score)

# References:
# https://machinelearningmastery.com/implement-decision-tree-algorithm-scratch-python/
# https://www.youtube.com/channel/UCbXgNpp0jedKWcQiULLbDTA