Ejemplo n.º 1
0
    def leave_one_out_error(self):
        correct = 0
        numPoints = len(self.full_set)
        for i, x in enumerate(self.X):
            print("LOO Progress:", i + 1, "/", numPoints)
            known_label = self.labels[i]
            beta = 0
            if i == 0:
                # Begining
                beta = self.__optimize__(self.full_set[1:], self.labels[1:])
            elif i == numPoints - 1:
                # End
                beta = self.__optimize__(self.full_set[:numPoints - 1],
                                         self.labels[:numPoints - 1])
            else:
                # Middle
                beta = self.__optimize__(
                    (Util.combineSets(self.full_set[:i],
                                      self.full_set[i + 1:])),
                    Util.combineSets(self.labels[:i], self.labels[i + 1:]))

            if self.classify(x, beta) == known_label:
                correct += 1

        incorrect = numPoints - correct
        return 100 * (incorrect / numPoints)
Ejemplo n.º 2
0
    def __init__(self, classA=False, classB=False, *, C=1, plotNow=False, printReport=False, brute_loo=True, brute_mute=False):
        # Initialize Variables and Parameters
        self.optimized = False
        self.plotPrepared = False
        self.constantC = C
        self.supportVectors = []
        self.margin = 0
        self.printReport = printReport
        self.brute = brute_loo
        self.brute_mute = False

        # Generate Random Data or Accept Provided Sets
        if classA and classB:
            self.classA = classA
            self.classB = classB
            self.fullSet = Util.combineSets(self.classA, self.classB)
            self.fullSetX, self.fullSetY = self.fullSet.T
        else:
            self.classA, self.classB, self.fullSetX, self.fullSetY, self.fullSet = RandomData.random_data()
            self.labels = RandomData.linear_labels()

        # Dimension of Class Data
        self.dimensions = len(self.classA[len(self.classA)-1])

        # Optimize and Plot or Report if Needed
        if plotNow or printReport:
            self.optimize()
            if plotNow:
                print("Graphing SVM, Report Will Generate After Graph Is Closed If Requested.")
                self.plot()
            if printReport:
                self.preparePlot()
                print(self)