Ejemplo n.º 1
0
    def Validate(self, subset):

        # Ensure that given the subset is valid.
        for feat in subset:
            if (0 > feat) or (feat > self.classifier.num_features):
                print("ERROR: Subset does not match features.")
                return 0

        # Make copy of the dataset for feature selection.
        # temp_normalized = self.classifier.normalized.copy()
        to_delete = []

        # Trim down to given subset.

        if subset != [0]:
            for col in range(self.classifier.num_features + 1)[1:]:
                if col not in subset:
                    to_delete.append(col)

        # K-fold vars.
        fold_list = []
        folded = []
        num_correct = 0

        print("\n--------------------------------------")
        print("\nPerforming k-fold cross validation.\n")

        # Perform k-fold cross validation.
        for leave_out in range(self.classifier.num_points):

            # Prepare temporary datasets for k-folding.
            fold_list = self.classifier.normalized.copy()
            folded = self.classifier.normalized[leave_out].copy()
            del fold_list[leave_out]

            # Remove the columns not specified by the subset.
            fold_list = np.delete(fold_list, to_delete, axis=1)
            folded = np.delete(folded, to_delete)

            # Create classifier for folded data.
            temp_c = Classifier()
            temp_c.Train(fold_list)
            result = temp_c.Test(folded)

            # Increment if classification accurate.
            if result == folded[0]:
                num_correct += 1

        return num_correct / self.classifier.num_points