コード例 #1
0
    def avgPerceptron(self):
        ### initialize variables
        ### initialize prediction (yHat) and actual (yStar) to (0)
        ### use numpy for vectors
        ### create a numpy array of zeroes the size of m for weight, w
        #m = len(self.vocabulary)

        yHat = 0
        yStar = 0

        count = 1

        for iteration in range(self.maxIteration):

            for fortune in self.featureVectorList:
                ### yHat and yStar are in the set {+1, -1}

                yStar = sign(int(fortune[0]))
                yHat = sign(self.w.dot(fortune[1]))
                ### if the prediction doesn't match the label,
                ### update the weight
                if yHat != yStar:
                    self.w = self.w + (self.eta * yStar * np.array(fortune[1]))
                    self.wCache = self.wCache + (self.eta * yHat *
                                                 np.array(fortune[1]))

                count += 1

            numTrain = len(self.featureVectorList)
            numTest = len(self.testVectorList)

            trainMistakes = self.checkAccuracy(self.featureVectorList)
            testMistakes = self.checkAccuracy(self.testVectorList)

            self.averagedTrainingAccuracy.append(
                100 - (100 * (float(trainMistakes) / numTrain)))
            self.averagedTestingAccuracy.append(100 -
                                                (100 * (float(testMistakes)) /
                                                 numTest))
            '''
            print 'iteration: ', iteration + 1
            print 'Training mistakes: ', trainMistakes, '   Testing mistakes: ', testMistakes
            print 'Training percentage: ', 100 - (100 * (float(trainMistakes)/numTrain)), '%', \
            '   Testing percentage: ', 100 - (100 * (float(testMistakes))/numTest), '%'
            '''
        '''
        for vector in range(len(self.wCache)):
            for element in self.wCache[vector][1]:
                newWeight = float(element) / count
                self.wCache[vector][1][element] = newWeight
            print 'weight: ', self.wCache[vector][1]
        '''
        self.wCache = self.wCache / count
コード例 #2
0
    def checkAccuracy(self):
        error = 0
        for fortune in self.featureVectorList:
            ### yHat and yStar are in the set {+1, -1}

            yStar = sign(int(fortune[0]))
            yHat = sign(self.w.dot(fortune[1]))
            ### if the prediction doesn't match the label,
            ### update the weight
            if yHat != yStar:
                error += 1

        return error
コード例 #3
0
    def perceptron(self):
        ### initialize variables
        ### initialize prediction (yHat) and actual (yStar) to (0)
        ### use numpy for vectors
        ### create a numpy array of zeroes the size of m for weight, w
        #m = len(self.vocabulary)

        yHat = 0
        yStar = 0

        for fortune in self.featureVectorList:
            ### yHat and yStar are in the set {+1, -1}

            yStar = sign(int(fortune[0]))
            yHat = sign(self.w.dot(fortune[1]))
            ### if the prediction doesn't match the label,
            ### update the weight
            if yHat != yStar:
                self.w = self.w + (self.eta * yStar * np.array(fortune[1]))
コード例 #4
0
    def perceptron(self):
        ### initialize variables
        ### initialize prediction (yHat) and actual (yStar) to (0)
        ### use numpy for vectors
        ### create a numpy array of zeroes the size of m for weight, w
        #m = len(self.vocabulary)

        for iteration in range(self.maxIteration):
            mistakes = 0
            yHat = 0
            yStar = 0

            for fortune in self.featureVectorList:
                ### yHat and yStar are in the set {+1, -1}

                yStar = sign(int(fortune[0]))
                yHat = sign(self.w.dot(fortune[1]))
                ### if the prediction doesn't match the label,
                ### update the weight
                if yHat != yStar:
                    self.w = self.w + (self.eta * yStar * np.array(fortune[1]))
                    mistakes += 1

            numTrain = len(self.featureVectorList)
            numTest = len(self.testVectorList)

            trainMistakes = self.checkAccuracy(self.featureVectorList)
            testMistakes = self.checkAccuracy(self.testVectorList)

            self.numberOfMistakes.append(mistakes)
            self.standardTrainingAccuracy.append(
                100 - (100 * (float(trainMistakes) / numTrain)))
            self.standardTestingAccuracy.append(100 -
                                                (100 * (float(testMistakes)) /
                                                 numTest))
            '''