def testNonExistingDataSet(self): dataSetName = "not-existing-data-set" fetcher = DataFetcher() try: fetcher.fetchDataSet(dataSetName) except IOError: pass
def testDataContainer(self): dataSetName = 'buffer_dataset' container = DataFetcher().fetchDataSet(dataSetName) numberOfExamples = 2 dataVectorLength = 1 self.assertEqual(numberOfExamples, container.getDataSetSize()) self.assertEqual(dataVectorLength, container.getDataVectorLength())
def testTrainBufferPerceptron(self): dataSetName = 'buffer_dataset' container = DataFetcher().fetchDataSet(dataSetName) perceptron = Perceptron(container, 0.1, 15) perceptron.train() weightVector = perceptron.getWeightVector() expectedLength = container.getDataVectorLength() + 1 self.assertEqual(len(weightVector), expectedLength)
def classifyDataSet(self, dataSetName, perceptron): self.log.info('Classifying dataset %s', dataSetName) container = DataFetcher().fetchDataSet(dataSetName) labels = container.getLabels() samples = container.getDataVectors() errors = 0 for i, sample in enumerate(samples): cls = perceptron.classify(sample) if cls != labels[i]: errors += 1 self.log.info('Misclassified sample %s as %s', labels[i], cls) setSize = container.getDataSetSize() errorRate = errors * 1.0 / setSize * 100 self.log.info('Error rate while classifying was %s', errorRate)
def trainPerceptron(self, dataSetName, alpha, iterations): container = DataFetcher().fetchDataSet(dataSetName) perceptron = Perceptron(container, 0.1, 15) perceptron.train() return perceptron