Пример #1
0
    def test_knn(self):
        from classifiers import KNNClassifier

        if type(self.k) == int:
            k = "%s" % self.k
        else:
            k = "-".join([str(i) for i in self.k])

        print("KNNClassifier")
        print("---" * 45)
        print("Train num = %s" % self.train_num)
        print("Test num = %s" % self.test_num)
        print("K = %s" % k)

        knn = KNNClassifier(self.train_data, self.train_labels, k=self.k, best_words=self.best_words)
        classify_labels = []

        print("KNNClassifiers is testing ...")
        for data in self.test_data:
            classify_labels.append(knn.classify(data))
        print("KNNClassifiers tests over.")

        filepath = "f_runout/KNN-%s-train-%d-test-%d-f-%d-k-%s-%s.xls" % \
                   (self.type,
                    self.train_num, self.test_num,
                    self.feature_num, k,
                    datetime.datetime.now().strftime(
                        "%Y-%m-%d-%H-%M-%S"))

        self.write(filepath, classify_labels)
Пример #2
0
def classify(data, classifier, num_classes, train_labels, train_features,
             test_labels, test_features):
    """
    Function used by FoldRunner to execute classification based on the current classifier
    :param data: the configuration dictionary
    :param classifier: current classifier (from the classifiers list in the configuration file)
    :param num_classes: the number of distinct labels (binary or multiclass classification)
    :param train_labels: the labels of all train instances
    :param train_features: the features of all train instances
    :param test_labels: the labels of all test instances
    :param test_features: the features of all test instances
    :return: the confusion matrix of the classification
    """
    if classifier == "NN_keras":
        return nnk.classify(data, num_classes, train_labels, train_features,
                            test_labels, test_features)
    elif classifier == "NN_scikit-learn":
        return nns.classify(train_labels, train_features, test_labels,
                            test_features)
    elif classifier == "KNN":
        return knn.classify(data, train_labels, train_features, test_labels,
                            test_features)
    elif classifier == "NaiveBayes":
        return nb.classify(train_labels, train_features, test_labels,
                           test_features)
    elif classifier == "RandomForest":
        return rf.classify(train_labels, train_features, test_labels,
                           test_features)
    elif classifier == "LogisticRegression":
        return lr.classify(train_labels, train_features, test_labels,
                           test_features)
Пример #3
0
    def test_knn(self):
        from classifiers import KNNClassifier

        if type(self.k) == int:
            k = "%s" % self.k
        else:
            k = "-".join([str(i) for i in self.k])

        print("KNNClassifier")
        print("---" * 45)
        print("Train num = %s" % self.train_num)
        print("Test num = %s" % self.test_num)
        print("K = %s" % k)

        # print self.train_data
        print (self.train_labels)
        print (len(self.train_data))
        print (self.train_data[0])

        knn = KNNClassifier(self.train_data, self.train_labels, k=self.k, best_words=self.best_words)
        classify_labels = []

        print("KNNClassifiers is testing ...")

        for data in self.test_data:
            classify_labels.append(knn.classify(data))
        print("KNNClassifiers tests over.")

        filepath = "f_runout/KNN-%s-train-%d-test-%d-f-%d-k-%s-%s.xls" % \
                   (self.type,
                    self.train_num, self.test_num,
                    self.feature_num, k,
                    datetime.datetime.now().strftime(
                        "%Y-%m-%d-%H-%M-%S"))

        self.write(filepath, classify_labels)