def train(): dirname = os.path.dirname(__file__) output_dirname = os.path.join(dirname, 'results') try: os.stat(output_dirname) except: os.mkdir(output_dirname) file_name = 'resources/dataset_train.csv' dirname = os.path.dirname(__file__) file_name = os.path.join(dirname, file_name) d = DataSet(file_name) d.loadDataSet() to_remove = [ d.data_set[0].index('Index'), d.data_set[0].index('First Name'), d.data_set[0].index('Last Name'), d.data_set[0].index('Birthday'), d.data_set[0].index('Best Hand'), d.data_set[0].index('Hogwarts House'), # Tests 7/10/18 d.data_set[0].index('Arithmancy'), d.data_set[0].index('Defense Against the Dark Arts'), d.data_set[0].index('Divination'), d.data_set[0].index('Muggle Studies'), d.data_set[0].index('History of Magic'), d.data_set[0].index('Transfiguration'), d.data_set[0].index('Potions'), d.data_set[0].index('Care of Magical Creatures'), d.data_set[0].index('Charms'), d.data_set[0].index('Flying'), ] X = np.array([[ d.data_set[i][j] for j in range(len(d.data_set[0])) if j not in to_remove ] for i in range(len(d.data_set))]) features = X[0, :] X = convert_to_float(X[1:, ]) y_col_nb = d.data_set[0].index('Hogwarts House') y = np.array(d.extractColumn(y_col_nb)[1:]) m = MeanImputation(X) m.train() m.transform() sc = Scaling(X) sc.train() sc.transform() l = LogisticRegression(X=X, y=y, optimizer_params={'alpha': 0.5, 'n': 50}) l.train() return features, l.beta
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000) myStupidClassifier = StupidRecognizer(data.trainingSet, data.validationSet, data.testSet) myPerceptronClassifier = Perceptron(data.trainingSet, data.validationSet, data.testSet, learningRate=0.005, epochs=30) myLRClassifier = LogisticRegression(data.trainingSet, data.validationSet, data.testSet, learningRate=0.005, epochs=30) # Train the classifiers print("=========================") print("Training..") print("\nStupid Classifier has been training..") myStupidClassifier.train() print("Done..") print("\nPerceptron has been training..") myPerceptronClassifier.train() print("Done..") print("\nLogistic Regression has been training..") myLRClassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated stupidPred = myStupidClassifier.evaluate() perceptronPred = myPerceptronClassifier.evaluate() lrPred = myLRClassifier.evaluate() # Report the result print("=========================") evaluator = Evaluator() print("Result of the stupid recognizer:") # evaluator.printComparison(data.testSet, stupidPred) evaluator.printAccuracy(data.testSet, stupidPred) print("\nResult of the Perceptron recognizer:") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.testSet, perceptronPred) print("\nResult of the Logistic Regression recognizer:") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.testSet, lrPred)
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000) myStupidClassifier = StupidRecognizer(data.trainingSet, data.validationSet, data.testSet) myPerceptronClassifier = Perceptron(data.trainingSet, data.validationSet, data.testSet, learningRate=0.005, epochs=30) myLogisticRegressionClassifier = LogisticRegression(data.trainingSet, data.validationSet, data.testSet, learningRate=0.001, epochs=50, error='mse') # Train the classifiers print("=========================") print("Training..") #print("\nStupid Classifier has been training..") #myStupidClassifier.train() #print("Done..") #print("\nPerceptron has been training..") #myPerceptronClassifier.train() #print("Done..") print("\nLogistic Regression Classifier has been training..") myLogisticRegressionClassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated #stupidPred = myStupidClassifier.evaluate() #perceptronPred = myPerceptronClassifier.evaluate() LogisticRegressionPred = myLogisticRegressionClassifier.evaluate() # Report the result print("=========================") evaluator = Evaluator() #print("Result of the stupid recognizer:") # evaluator.printComparison(data.testSet, stupidPred) #evaluator.printAccuracy(data.testSet, stupidPred) #print("\nResult of the Perceptron recognizer:") # evaluator.printComparison(data.testSet, perceptronPred) #evaluator.printAccuracy(data.testSet, perceptronPred) print("Result of the Logistic Regression:") evaluator.printAccuracy(data.testSet, LogisticRegressionPred)
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000) # myStupidClassifier = StupidRecognizer(data.trainingSet, # data.validationSet, # data.testSet) # Uncomment this to make your Perceptron evaluated # myPerceptronClassifier = Perceptron(data.trainingSet, # data.validationSet, # data.testSet, # learningRate=0.005, # epochs=30) myNeuralNetwork = LogisticRegression(data.trainingSet, data.validationSet, data.testSet) # Train the classifiers print("=========================") print("Training..") # print("\nStupid Classifier has been training..") # myStupidClassifier.train() # print("Done..") print("\nNeural Network training..") #myPerceptronClassifier.train() myNeuralNetwork.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated # stupidPred = myStupidClassifier.evaluate() # Uncomment this to make your Perceptron evaluated # perceptronPred = myPerceptronClassifier.evaluate() neuralPred = myNeuralNetwork.evaluate() # Report the result print("=========================") evaluator = Evaluator() # print("Result of the stupid recognizer:") # evaluator.printComparison(data.testSet, stupidPred) # evaluator.printAccuracy(data.testSet, stupidPred) print("\nResult of the Perceptron recognizer:") # evaluator.printComparison(data.testSet, perceptronPred) # Uncomment this to make your Perceptron evaluated # evaluator.printAccuracy(data.testSet, perceptronPred) evaluator.printAccuracy(data.testSet, neuralPred) # evaluator.printConfusionMatrix(data.testSet, perceptronPred) evaluator.printConfusionMatrix(data.testSet, neuralPred)
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000) myStupidClassifier = StupidRecognizer(data.trainingSet, data.validationSet, data.testSet) # parameters learnRate = 0.005 maxEpochs = 20 #epochNumber = 30 xEpochs = [] yAccuracyPerceptron = [] yAccuracyLogistic = [] # loop for gathering data for graph plotting for epochNumber in xrange(1, maxEpochs + 1): myPerceptronClassifier = Perceptron( data.trainingSet, data.validationSet, data.testSet, learningRate=learnRate, #0.005, epochs=epochNumber) # Uncomment this to run Logistic Neuron Layer myLRClassifier = LogisticRegression( data.trainingSet, data.validationSet, data.testSet, learningRate=learnRate, #0.005, epochs=epochNumber #30 ) # Train the classifiers print("=========================") print("Training..") print("\nStupid Classifier has been training..") myStupidClassifier.train() print("Done..") print("\nPerceptron has been training..") myPerceptronClassifier.train() print("Done..") print("\nLogistic Regression has been training..") myLRClassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated stupidPred = myStupidClassifier.evaluate() perceptronPred = myPerceptronClassifier.evaluate() lrPred = myLRClassifier.evaluate() # Report the result print("=========================") evaluator = Evaluator() print("Result of the stupid recognizer:") #evaluator.printComparison(data.testSet, stupidPred) evaluator.printAccuracy(data.testSet, stupidPred) print("\nResult of the Perceptron recognizer:") #evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.testSet, perceptronPred) print("\nResult of the Logistic Regression recognizer:") #evaluator.printComparison(data.testSet, lrPred) evaluator.printAccuracy(data.testSet, lrPred) # accumulate plotting data xEpochs.append(epochNumber) yAccuracyPerceptron.append( accuracy_score(data.testSet.label, perceptronPred) * 100) yAccuracyLogistic.append( accuracy_score(data.testSet.label, lrPred) * 100) # === end of for loop === # plot the graph plt.plot(xEpochs, yAccuracyPerceptron, marker='o', label='Perceptron') plt.plot(xEpochs, yAccuracyLogistic, marker='o', color='r', label='Logistic Neuron') plt.xlabel('Number of epochs') plt.ylabel('Accuracy [%]') plt.title( 'Performance on different epochs\n(using: testSet | learningRate: ' + str(learnRate) + ')') #plt.legend() plt.legend(loc=4) #plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.) #plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.show()
def classify_one(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, one_hot=True, target_digit='7') # NOTE: # Comment out the MNISTSeven instantiation above and # uncomment the following to work with full MNIST task # data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, # one_hot=False) # NOTE: # Other 1-digit classifiers do not make sense now for comparison purpose # So you should comment them out, let alone the MLP training and evaluation # Train the classifiers # print("=========================") print("Training..") # Stupid Classifier myStupidClassifier = StupidRecognizer(data.training_set, data.validation_set, data.test_set) print("\nStupid Classifier has been training..") myStupidClassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated stupidPred = myStupidClassifier.evaluate() # Perceptron myPerceptronClassifier = Perceptron(data.training_set, data.validation_set, data.test_set, learning_rate=0.005, epochs=10) print("\nPerceptron has been training..") myPerceptronClassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated perceptronPred = myPerceptronClassifier.evaluate() # Logistic Regression myLRClassifier = LogisticRegression(data.training_set, data.validation_set, data.test_set, learning_rate=0.20, epochs=30) print("\nLogistic Regression has been training..") myLRClassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated lrPred = myLRClassifier.evaluate() # Logistic Regression myMLPClassifier = MultilayerPerceptron(data.training_set, data.validation_set, data.test_set, learning_rate=0.30, epochs=50) print("\nMultilayer Perceptron has been training..") myMLPClassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated mlpPred = myMLPClassifier.evaluate() # Report the result # print("=========================") evaluator = Evaluator() print("Result of the stupid recognizer:") # evaluator.printComparison(data.testSet, stupidPred) evaluator.printAccuracy(data.test_set, stupidPred) print("\nResult of the Perceptron recognizer (on test set):") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.test_set, perceptronPred) print("\nResult of the Logistic Regression recognizer (on test set):") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.test_set, lrPred) print("\nResult of the Multi-layer Perceptron recognizer (on test set):") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.test_set, mlpPred) # Draw plot = PerformancePlot("Logistic Regression") plot.draw_performance_epoch(myLRClassifier.performances, myLRClassifier.epochs)
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000) myStupidClassifier = StupidRecognizer(data.trainingSet, data.validationSet, data.testSet) mylogisticClassifier = LogisticRegression(data.trainingSet, data.validationSet, data.testSet, learningRate=0.005, epochs=30) # Train the classifiers print("=========================") print("Training..") print("\nStupid Classifier has been training..") myStupidClassifier.train() print("Done..") print("\nLogsticregression has been training..") mylogisticClassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated stupidPred = myStupidClassifier.evaluate() perceptronPred = mylogisticClassifier.evaluate() # Report the result print("=========================") evaluator = Evaluator() print("Result of the stupid recognizer:") # evaluator.printComparison(data.testSet, stupidPred) evaluator.printAccuracy(data.testSet, stupidPred) print("\n Result of the Logsticregression recognizer:") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.testSet, perceptronPred) for i in range(2): for j in range(2): learningRate = (i + 1) * 0.002 epochs = (j + 1) * 20 mylogisticClassifier = LogisticRegression( data.trainingSet, data.validationSet, data.testSet, learningRate=learningRate, epochs=epochs) # Train the classifiers print("=========================") print("learning rate :" + str(learningRate)) print("epoch :" + str(epochs)) print("Training..") print("\nLogsticregression has been training..") mylogisticClassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated perceptronPred = mylogisticClassifier.evaluate() # Report the result print("=========================") evaluator = Evaluator() print("\n Result of the Logsticregression recognizer:") # evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.testSet, perceptronPred)
def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000, oneHot=True) myStupidClassifier = StupidRecognizer(data.trainingSet, data.validationSet, data.testSet) myPerceptronClassifier = Perceptron(data.trainingSet, data.validationSet, data.testSet, learningRate=0.005, epochs=30) myLRClassifier = LogisticRegression(data.trainingSet, data.validationSet, data.testSet, learningRate=0.005, epochs=30) # Report the result # print("=========================") evaluator = Evaluator() # Train the classifiers print("=========================") print("Training..") print("\nStupid Classifier has been training..") myStupidClassifier.train() print("Done..") print("\nPerceptron has been training..") myPerceptronClassifier.train() print("Done..") print("\nLogistic Regression has been training..") myLRClassifier.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated stupidPred = myStupidClassifier.evaluate() perceptronPred = myPerceptronClassifier.evaluate() lrPred = myLRClassifier.evaluate() # Report the result print("=========================") evaluator = Evaluator() print("Result of the stupid recognizer:") #evaluator.printComparison(data.testSet, stupidPred) evaluator.printAccuracy(data.testSet, stupidPred) print("\nResult of the Perceptron recognizer:") #evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.testSet, perceptronPred) print("\nResult of the Logistic Regression recognizer:") #evaluator.printComparison(data.testSet, lrPred) evaluator.printAccuracy(data.testSet, lrPred) # Draw plot = PerformancePlot("Logistic Regression validation") plot.draw_performance_epoch(myLRClassifier.performances, myLRClassifier.epochs)