def neural_net_trial(Xtrain, Ttrain, hidden_units, question_part): fig = plt.figure(figsize=(4, 3)) plt.suptitle('Question 1(' + str(question_part) + '): Neural nets with ' + str(hidden_units) + ' hidden units') best_acc = 0 nn_trial = MLPClassifier(hidden_layer_sizes=(hidden_units, ), activation='logistic', solver='sgd', learning_rate_init=0.01, max_iter=1000, tol=0.0000001) for i in range(12): nn_trial.fit(Xtrain, Ttrain) test_acc = nn_trial.score(Xtest, Ttest) # print(test_acc) fig.add_subplot(4, 3, i + 1) plt.xlim(-3, 6) plt.ylim(-3, 6) plot_train(Xtrain, Ttrain) bonnerlib2.dfContour(nn_trial) if (test_acc > best_acc): best_acc = test_acc best_nn = nn_trial best_predict = best_nn.predict(Xtest) best_precision = precision(best_predict, Ttest) best_recall = recall(best_predict, Ttest) return best_nn, best_acc, best_precision, best_recall
def plt_nn(nn, question_part, num_layers): fig = plt.figure() plt.suptitle('Question 1(' + str(question_part) + '): Best neural net with ' + str(num_layers) + ' hidden units') plot_train(Xtrain, Ttrain) bonnerlib2.dfContour(nn)
def logregDemo(N, betaList): I = len(betaList) M = np.int(np.ceil(np.sqrt(I))) mu0 = np.array([2.0, -2.0]) mu1 = np.array([-2.0, 2.0]) fig1 = plt.figure() fig2 = plt.figure() fig1.suptitle('Figure 2: contour plots of logistic decision functions') fig2.suptitle('Figure 3: surface plots of logistic decision functions') for i in range(I): beta = betaList[i] X, t = genData(beta * mu0, beta * mu1, N) clf = lin.LogisticRegression() clf.fit(X, t) acc = clf.score(X, t) print('For beta={}, the accuracy is {}'.format(beta, acc)) ax = fig1.add_subplot(M, M, i + 1) ax.set_xlim(-6, 6) ax.set_ylim(-6, 6) colors = np.array(['r', 'b']) ax.scatter(X[:, 0], X[:, 1], color=colors[t], s=0.1) bonnerlib2.dfContour(clf, ax) ax = fig2.add_subplot(M, M, i + 1, projection='3d') ax.set_xlim(-9, 6) ax.set_ylim(-6, 9) bonnerlib2.df3D(clf, ax)
def fitMoons(): figct = plt.figure() figct.suptitle("Figure 3, Question 1(b): contour plots for various training sessions.") mine = 1 clfmin = 0 for i in range(9): clf = MLPClassifier(hidden_layer_sizes=[3], activation='tanh', solver='sgd', learning_rate_init=0.01, tol=10.0 ** (-20), max_iter=10000) clf.fit(Xtain,ttrain) acc = clf.score(Xtest,ttest) error = 1 - acc if error< mine: mine = error clfmin = clf print("The error is %f"%(error)) ax = figct.add_subplot(3,3,i+1) ax.set_xlim(-4,4) ax.set_ylim(-4,4) colors=np.array(['r','b']) ax.scatter(Xtain[:,0],Xtain[:,1],color = colors[ttrain],s= 2) bonnerlib2.dfContour(clf,ax) print("The minimum error is %f"%(mine)) fig1 = plt.figure() fig1.suptitle("Figure 4, Question 1(b): contour plot for best training session.") ax = fig1.add_subplot(1,1,1) ax.set_xlim(-4, 4) ax.set_ylim(-4, 4) colors = np.array(['r', 'b']) ax.scatter(Xtain[:, 0], Xtain[:, 1], color=colors[ttrain], s=2) bonnerlib2.dfContour(clfmin, ax)
def gbclf_train_test(mu0, mu1, cov0, cov1, N0_train, N1_train, N0_test, N1_test, str_question): # generate train data from 2(a) and test data from 2(f) X_train, t_train = gen_data(mu0, mu1, cov0, cov1, N0_train, N1_train) X_test, t_test = gen_data(mu0, mu1, cov0, cov1, N0_test, N1_test) # train sklearn QuadraticDiscriminantAnalysis GBclf = QuadraticDiscriminantAnalysis() GBclf.fit(X_train, t_train) # compute and print out the accuracy of your classifier # with the test data from q2(f) accuracy = GBclf.score(X_test, t_test) print '\tAccuracy of Gaussian Bayes clf ' + str_question + ':' print '\t\t' + str(accuracy) # plot the training data classToColor = np.array(['r', 'b']) plt.scatter(X_train[:, 0], X_train[:, 1], color=classToColor[t_train], s=2) # plot the decision boundary using dfContour dfContour(GBclf) # plt.xlim(-3, 6); plt.ylim(-3, 6); plt.title('Question ' + str_question + ': Decision boundary and contours') plt.show()
def q1_3_by_3_graphs(hidden_units, question_letter, Xtrain, tTrain, Xtest, tTest, show_graph): mlp = MLPClassifier(hidden_layer_sizes=(hidden_units, ), max_iter=10000, tol=10e-10, solver='sgd', learning_rate_init=.01, activation='tanh') accuracies = [] models = [] weights = [] predic_prob = [] for i in range(0, 9): model = mlp.fit(Xtrain, tTrain) prob = mlp.predict_proba(Xtrain) predic_prob.append(prob) w = mlp.coefs_ w0 = mlp.intercepts_ weights.append((w0, w)) models.append(model) acc = mlp.score(Xtest, tTest) accuracies.append(acc) plt.subplot(3, 3, i + 1) plt.scatter(Xtrain[:, 0], Xtrain[:, 1], s=2, color=np.where(tTrain == 0.0, 'r', 'b')) dfContour(mlp) bestNN = np.argmax(accuracies) accuracies = np.array(accuracies) models = np.array(models) if show_graph: plt.suptitle('Question 1({}): Neural net with {} hidden units.'.format( question_letter, hidden_units)) plt.show() plt.scatter(Xtrain[:, 0], Xtrain[:, 1], s=2, color=np.where(tTrain == 0.0, 'r', 'b')) dfContour(models[bestNN]) plt.title( 'Question 1({}): Best neural net with {} hidden units.'.format( question_letter, hidden_units)) print('Best NN Test accuracy: {:.4%}'.format(accuracies[bestNN])) plt.show() plt.close() return models[bestNN], weights[bestNN][0], weights[bestNN][ 1], accuracies, np.array(predic_prob[bestNN])
def decision_boundaries(hidden_units, question_letter, bestNN, w0, w, Xtrain, tTrain): plt.scatter(Xtrain[:, 0], Xtrain[:, 1], s=2, color=np.where(tTrain == 0.0, 'r', 'b')) plt.xlim((-5, 6)) plt.ylim((-5, 6)) plotDB(w, w0) plt.title('Question 1({}): Decision boundaries for {} hidden units'.format( question_letter, hidden_units)) dfContour(bestNN) plt.show()
def bestOfTwelveNN(n_units, str_question, X_train, t_train, X_test, t_test, N0_test, N1_test): # make 12 plots in 4x3 grid with decision boundaries classToColor = np.array(['r', 'b']) fig, axs = plt.subplots(4, 3) plt.suptitle('Question {}: Neural nets with {} hidden units.'.format(str_question, n_units)) best_clf, acc_best = None, 0 for i in range(12): plt.sca(axs[i / 3, i % 3]) plt.scatter(X_train[:, 0], X_train[:, 1], color=classToColor[t_train], s=2) clf = MLPClassifier(solver='sgd', hidden_layer_sizes=(n_units, ), activation='logistic', learning_rate_init=0.01, tol=np.power(10, -8, dtype=float), max_iter=1000) clf.fit(X_train, t_train) dfContour(clf) curr_acc, precision, recall = getMetrics(clf, X_test, t_test, N0_test, N1_test) # update best clf if curr_acc > acc_best: best_clf, acc_best = clf, curr_acc plt.show() # plot best model plt.scatter(X_train[:, 0], X_train[:, 1], color=classToColor[t_train], s=2) plt.xlim(-3, 6); plt.ylim(-3, 6) plt.title('Question {}: Best neural net with {} hidden units.'.format(str_question, n_units)) dfContour(best_clf) plt.show() # print best model metrics accuracy, precision, recall = getMetrics(best_clf, X_test, t_test, N0_test, N1_test) print '\nQuestion {}.'.format(str_question); print('-------------') print '\taccuracy: {}'.format(accuracy); print '\tprecision: {}'.format(precision); print '\trecall: {}'.format(recall)
def q1b(DATA): Xtrain = DATA[0] tTrain = DATA[1] mlp = MLPClassifier(hidden_layer_sizes=(1, ), max_iter=10000, tol=1e-10, solver='sgd', learning_rate_init=.01, activation='tanh') mlp.fit(Xtrain, tTrain) plt.scatter(Xtrain[:, 0], Xtrain[:, 1], s=2, color=np.where(tTrain == 0.0, 'r', 'b')) plt.title('Question 1(b): Neural net with 1 hidden unit.') dfContour(mlp) plt.show()
nn = MLPClassifier(hidden_layer_sizes=1, activation='logistic', solver='sgd', learning_rate_init=0.01, max_iter=1000, tol=0.0000001) nn.fit(Xtrain, Ttrain) prediction = nn.predict(Xtest) fig_1b = plt.figure() fig_1b.suptitle('Question 1(b): Neural net with 1 hidden unit') plt.xlim(-3, 6) plt.ylim(-3, 6) plot_train(Xtrain, Ttrain) bonnerlib2.dfContour(nn) print('Test accuracy: ' + str(nn.score(Xtest, Ttest))) print('Test precision: ' + str(precision(prediction, Ttest))) print('Test recall: ' + str(recall(prediction, Ttest))) #c print('\n') print('Question 1(c).') print('-------------') def accuracy(train, target): correct = sum(train == target) return float(correct) / float(len(target))
np.sum(true_pos_blue + false_neg_blue)) print "precision for P(C=1|x) = 0.05: ", precision_red print "recall for P(C=1|x) = 0.05: ", recall_red print "precision for P(C=1|x) = 0.5: ", precision_black print "recall for P(C=1|x) = 0.5: ", recall_black print "precision for P(C=1|x) = 0.6: ", precision_blue print "recall for P(C=1|x) = 0.6: ", recall_blue # Question 4(a) qda = QuadraticDiscriminantAnalysis() qda.fit(X_train, t_train) accuracy4a = qda.score(X_test, t_test) print "accuracy 4(a): ", accuracy4a plt.figure(4) plot_data(X_train, t_train) dfContour(qda) plt.title("Question 4(a): Decision boundary and contours") # Question 4(c) X_train_new, t_train_new = gen_data([1, 1], [2, 2], 0, 0.9, 1000, 500) qda.fit(X_train_new, t_train_new) X_test_new, t_test_new = gen_data([1, 1], [2, 2], 0, 0.9, 10000, 5000) accuracy_qda = qda.score(X_test_new, t_test_new) print "accuracy 4(c): ", accuracy_qda plt.figure(5) plot_data(X_train_new, t_train_new) dfContour(qda) plt.title("Question 4(c): Decision boundary and contours") # Question 4(d) X_train_new2, t_train_new2 = gen_data([1, 1], [2, 2], 0, 0.9, 1000, 5000)
clf = QuadraticDiscriminantAnalysis() model = clf.fit(X, t) accuracy4a = clf.score(X, t) print('accuracy: ' + str(accuracy4a)) fig_4a = plt.figure() fig_4a.suptitle('Question 4(a): Decision boundary and contours') X, y = X.T colors = [] for i in range(len(t)): if (t[i] == 0): colors.append('red') else: colors.append('blue') plt.scatter(X, y, c=colors, s=2) bonnerlib2.dfContour(clf) #c Xc, tc = gen_data(mu0=(1, 1), mu1=(2, 2), cov0=0, cov1=0.9, N0=1000, N1=500) clf2 = QuadraticDiscriminantAnalysis() model = clf2.fit(Xc, tc) accuracy = clf2.score(Xc, tc) print('accuracy: ' + str(accuracy)) fig_4c = plt.figure() fig_4c.suptitle('Question 4(c): Decision boundary and contours') Xc, yc = Xc.T colors = [] for i in range(len(t)): if (t[i] == 0): colors.append('red')
def q1(): # Question 1(a) # generate training set and test set N0_train, N1_train = 1000, 500 N0_test, N1_test = 10000, 5000 mu0, mu1 = (1, 1), (2, 2) cov0, cov1 = 0, 0.9 X_train, t_train = gen_data(mu0, mu1, cov0, cov1, N0_train, N1_train) X_test, t_test = gen_data(mu0, mu1, cov0, cov1, N0_test, N1_test) # Question 1(b) # train a NN with one unit in the hidden layer clf = MLPClassifier(solver='sgd', hidden_layer_sizes=(1,), activation='logistic', learning_rate_init=0.01, tol=np.power(10, -8, dtype=float), max_iter=1000) clf.fit(X_train, t_train) accuracy, precision, recall = getMetrics(clf, X_test, t_test, N0_test, N1_test) # print the accuracy, precision and recall of the neural net on the test data print '\nQuestion 1(b).'; print('-------------') print '\taccuracy: {}'.format(accuracy); print '\tprecision: {}'.format(precision); print '\trecall: {}'.format(recall) # plot training data classToColor = np.array(['r', 'b']) plt.scatter(X_train[:, 0], X_train[:, 1], color=classToColor[t_train], s=2) plt.xlim(-3, 6); plt.ylim(-3, 6); plt.title('Question 1(b): Neural net with 1 hidden unit.') # draw decision boundary dfContour(clf) plt.show() # Question 1(c) # 12 NNs with two units in the hidden layer bestOfTwelveNN(2, '1(c)', X_train, t_train, X_test, t_test, N0_test, N1_test) # Question 1(d) # 12 NNs with three units in the hidden layer bestOfTwelveNN(3, '1(d)', X_train, t_train, X_test, t_test, N0_test, N1_test) # Question 1(e) # 12 NNs with four units in the hidden layer bestOfTwelveNN(4, '1(e)', X_train, t_train, X_test, t_test, N0_test, N1_test)