def main(): data = MNISTSeven("../data/mnist_seven.csv", 3000, 1000, 1000) myPerceptronClassifier = Perceptron(data.trainingSet, data.validationSet, data.testSet, learningRate=0.01, epochs=10) # Train the classifiers print("=========================") print("Training..") print("\nTraining the Perceptron..") myPerceptronClassifier.train() print("Done..") # Do the recognizer perceptronPred = myPerceptronClassifier.evaluate() # Report the result print("=========================") evaluator = Evaluator() print("\nResult of the Perceptron recognizer:") #evaluator.printComparison(data.testSet, perceptronPred) evaluator.printAccuracy(data.testSet, perceptronPred)
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, epochs=10) # Train the classifiers print("=========================") print("Training..") myStupidClassifier.train() myPerceptronClassifier.train() # Do the recognizer stupidPred = myStupidClassifier.evaluate() perceptronPred = myPerceptronClassifier.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)
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, 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) # 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) # Uncomment this to make your Perceptron evaluated myPerceptronClassifier = Perceptron( data.trainingSet, data.validationSet, data.testSet, learningRate=1.0, #0.005, epochs=1 #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..") # Do the recognizer # Explicitly specify the test set to be evaluated stupidPred = myStupidClassifier.evaluate() # Uncomment this to make your Perceptron evaluated perceptronPred = myPerceptronClassifier.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.printConfusionMatrix(data.testSet, perceptronPred) evaluator.printClassificationResult(data.testSet, perceptronPred, ['class 0', 'class 1']) #target_names)
def __init__(self): self.iris = pd.read_csv('https://archive.ics.uci.edu/ml/' 'machine-learning-databases/iris/iris.data', header=None) print(self.iris.head()) print('----------------------') print(self.iris.tail) print('----------------------') print(self.iris.columns) ''' [150 rows x 5 columns]> ---------------------- Int64Index([0, 1, 2, 3, 4], dtype='int64') ''' # Iris-setosa 와 versicolor 선택 (MCP 는 이진분류만 할 수 있다) t = self.iris.iloc[0:100,4].values self.y = np.where(t == 'Iris-setosa', -1, 1) # 꽃받침 길이, 꽃잎 추출 self.X = self.iris.iloc[0:100, [0,2]].values self.clf = Perceptron(eta = 0.1, n_iter=10)
class IrisModel: def __init__(self): self.iris = pd.read_csv( 'https://archive.ics.uci.edu/ml/' 'machine-learning-databases/iris/iris.data', header=None) print(self.iris.head()) print('----------------------') print(self.iris.tail) print('----------------------') print(self.iris.columns) ''' [150 rows x 5 columns]> ---------------------- Int64Index([0, 1, 2, 3, 4], dtype='int64') ''' # Iris-setosa 와 versicolor 선택 (MCP 는 이진분류만 할 수 있다) t = self.iris.iloc[0:100, 4].values self.y = np.where(t == 'Iris-setosa', -1, 1) # 꽃받침 길이, 꽃잎 추출 self.X = self.iris.iloc[0:100, [0, 2]].values self.clf = Perceptron(eta=0.1, n_iter=10) def get_iris(self): return self.iris def get_X(self): return self.X def get_y(self): return self.y def draw_scatter(self): X = self.X plt.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa') plt.scatter(X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor') plt.xlabel('sepal length[cm]') plt.ylabel('petal length[cm]') plt.legend(loc='upper left') plt.show() def draw_errors(self): X = self.X y = self.y self.clf.fit(X, y) plt.plot(range(1, len(self.clf.errors_) + 1), self.clf.errors_, marker='o') plt.xlabel('Epoch') plt.ylabel('Number of Errors') plt.show() def plot_decision_regions(self, X, y, classifier, resolution=0.02): # 마커와 컬러맵을 설정합니다 markers = ('s', 'x', 'o', '^', 'v') colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan') cmap = ListedColormap(colors[:len(np.unique(y))]) # 결정 경계를 그립니다 x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 """ numpy 모듈의 arrange 함수는 반열린구간 [start, stop) 에서 step 의 크기만큼 일정하게 떨어져 있는 숫자들을 array 형태로 반환하는 함수 meshgrid 함수는 사각형 영역을 구성하는 가로축의 점들과 세로축의 점을 나타내는 두 벡터를 인수로 받아서 이 사각형 영역을 이루는 조합을 출력한다. 결과는 그리드 포인트의 x 값만을 표시하는 행렬과 y 값만을 표시하는 행렬 두 개로 분리하여 출력한다 """ xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution)) Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T) Z = Z.reshape(xx1.shape) plt.contourf(xx1, xx2, Z, alpha=0.3, cmap=cmap) plt.xlim(xx1.min(), xx1.max()) plt.ylim(xx2.min(), xx2.max()) # 샘플의 산점도를 그립니다 for idx, cl in enumerate(np.unique(y)): plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1], alpha=0.8, c=colors[idx], marker=markers[idx], label=cl, edgecolor='black') plot_decision_regions(X, y, classifier=self.clf) plt.xlabel('sepal length [cm]') plt.ylabel('petal length [cm]') plt.legend(loc='upper left') plt.show() def draw_adaline_graph(self): X = self.X y = self.y fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 4)) ada1 = AdalineGD(n_iter=10, eta=0.01).fit(X, y) ax[0].plot(range(1, len(ada1.cost_) + 1), np.log10(ada1.cost_), marker='o') ax[0].set_xlabel('Epochs') ax[0].set_ylabel('log(Sum-squared-error)') ax[0].set_title('Adaline - Learning rate 0.01') ada2 = AdalineGD(n_iter=10, eta=0.0001).fit(X, y) ax[1].plot(range(1, len(ada2.cost_) + 1), ada2.cost_, marker='o') ax[1].set_xlabel('Epochs') ax[1].set_ylabel('Sum-squared-error') ax[1].set_title('Adaline - Learning rate 0.0001') plt.show() def draw_adaline_gd_graph(self): # 특성을 표준화합니다. X = self.X y = self.y X_std = np.copy(X) X_std[:, 0] = (X[:, 0] - X[:, 0].mean()) / X[:, 0].std() X_std[:, 1] = (X[:, 1] - X[:, 1].mean()) / X[:, 1].std() ada = AdalineGD(n_iter=15, eta=0.01) ada.fit(X_std, y) plot_decision_regions(X_std, y, classifier=ada) plt.title('Adaline - Gradient Descent') plt.xlabel('sepal length [standardized]') plt.ylabel('petal length [standardized]') plt.legend(loc='upper left') plt.tight_layout() plt.show() plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker='o') plt.xlabel('Epochs') plt.ylabel('Sum-squared-error') plt.tight_layout() plt.show()
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, 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..") myMLP = MultilayerPerceptron(data.trainingSet, data.validationSet, data.testSet, learningRate=0.01, epochs=30, loss="ce", outputActivation="softmax", weight_decay=0.1) print("\nMLP has been training..") myMLP.train() print("Done..") # Do the recognizer # Explicitly specify the test set to be evaluated # stupidPred = myStupidClassifier.evaluate() # perceptronPred = myPerceptronClassifier.evaluate() # lrPred = myLRClassifier.evaluate() mlpPred = myMLP.evaluate(data.validationSet) # # 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) print("\nResult of the Multilayer Perceptron recognizer:") #evaluator.printComparison(data.testSet, lrPred) # evaluator.printAccuracy(data.testSet, mlpPred) plot = PerformancePlot("MLP validation") plot.draw_performance_epoch(myMLP.performances, myMLP.epochs)
X = pd.DataFrame(iris.data, columns=iris.feature_names).iloc[0:100, [0,2]].values y = iris.target[0:100] y = np.where(y == 0, 1, -1) plt.scatter(df.iloc[:49, 0], df.iloc[:49, 2], color='red', marker='o', label='setosa') plt.scatter(df.iloc[50:101,0], df.iloc[50:101, 2], color='blue', marker='x', label='versicolor') plt.scatter(df.iloc[102:,0], df.iloc[102:, 2], color='green', marker='^', label='virginica') plt.xlabel('sepal length[cm]') plt.ylabel('petal length[cm]') plt.legend(loc='upper left') plt.show() ppn = Perceptron(eta=0.01, n_iter=10) ppn.fit(X,y) plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='o') plt.xlabel('Epoch') plt.ylabel('Number of errors') plt.show() from model.plot import plot_decision_regions plot_decision_regions(X, y, classifier=ppn) plt.xlabel('sepal length [cm]') plt.ylabel('petal length [cm]') plt.legend(loc='upper left')