コード例 #1
0
    def test_gradient_descent(self):
        scale = Scale.scale_from_data(self.test_dataset)
        # self.logreg = LogisticRegressor(self.train_dataset, self.train_labels, scale=scale)

        self.logreg = LogisticRegressor(self.train_dataset, self.train_labels)

        gr = GradientDescentRunner(self.logreg.get_gradient(), len(self.samples[0]) + 1,
                                   self.logreg.get_error_function(), alpha=1e-8, max_iter=300)
        weights = gr.run()
        self.logreg.weights = weights

        mismatches = 0
        predictions = [self.logreg.predict(test_data) for test_data in self.test_dataset]
        print "All predictions: ", predictions

        for test_data, label in zip(self.test_dataset, self.test_labels):
            prediction = self.logreg.predict(test_data)
            if prediction != label:
                mismatches +=1
                # print "Mismatch! Predicted ", prediction, ", True ", label

        total = len(self.test_dataset)
        print "total data: ", total
        print "total mismatch: ", mismatches
        print "percentage success: ", (100 - float(mismatches) / float(total) * 100), "%"
コード例 #2
0
    def test_gradient_descent(self):
        scale = Scale.scale_from_data(self.test_dataset)
        # self.logreg = LogisticRegressor(self.train_dataset, self.train_labels, scale=scale)

        self.logreg = LogisticRegressor(self.train_dataset, self.train_labels)

        gr = GradientDescentRunner(self.logreg.get_gradient(),
                                   len(self.samples[0]) + 1,
                                   self.logreg.get_error_function(),
                                   alpha=1e-8,
                                   max_iter=300)
        weights = gr.run()
        self.logreg.weights = weights

        mismatches = 0
        predictions = [
            self.logreg.predict(test_data) for test_data in self.test_dataset
        ]
        print "All predictions: ", predictions

        for test_data, label in zip(self.test_dataset, self.test_labels):
            prediction = self.logreg.predict(test_data)
            if prediction != label:
                mismatches += 1
                # print "Mismatch! Predicted ", prediction, ", True ", label

        total = len(self.test_dataset)
        print "total data: ", total
        print "total mismatch: ", mismatches
        print "percentage success: ", (
            100 - float(mismatches) / float(total) * 100), "%"