def problem9a(T = 25, L = 0.01): print('T = {:.4f}, Lambda = {:.4f}'.format(T, L)) #test_bow_features and test_labels avg_peg_train_accuracy, avg_peg_test_accuracy = p1.classifier_accuracy( p1.pegasos, train_bow_features, test_bow_features, train_labels, test_labels, T = T,L = L) print("{:50} {:.4f}".format("Training accuracy for Pegasos:", avg_peg_train_accuracy)) print("{:50} {:.4f}".format("Testing accuracy for Pegasos:", avg_peg_test_accuracy))
def problem7(T = 10, L = 0.01): pct_train_accuracy, pct_val_accuracy = p1.classifier_accuracy( p1.perceptron, train_bow_features, val_bow_features, train_labels,val_labels,T = T) print("{:35} {:.4f}".format("Training accuracy for perceptron:", pct_train_accuracy)) print("{:35} {:.4f}".format("Validation accuracy for perceptron:", pct_val_accuracy)) avg_pct_train_accuracy, avg_pct_val_accuracy = p1.classifier_accuracy( p1.average_perceptron, train_bow_features, val_bow_features, train_labels,val_labels,T = T) print("{:43} {:.4f}".format("Training accuracy for average perceptron:", avg_pct_train_accuracy)) print("{:43} {:.4f}".format("Validation accuracy for average perceptron:", avg_pct_val_accuracy)) avg_peg_train_accuracy, avg_peg_val_accuracy = p1.classifier_accuracy( p1.pegasos, train_bow_features,val_bow_features, train_labels,val_labels,T = T,L = L) print("{:50} {:.4f}".format("Training accuracy for Pegasos:", avg_peg_train_accuracy)) print("{:50} {:.4f}".format("Validation accuracy for Pegasos:", avg_peg_val_accuracy))
def test_algorithm_compare(self): # ------------------------------------------------------------------------------- # Problem 7 # ------------------------------------------------------------------------------- T = 10 L = 0.01 pct_train_accuracy, pct_val_accuracy = \ p1.classifier_accuracy(p1.perceptron, train_bow_features, val_bow_features, train_labels, val_labels, T=T) print("{:35} {:.4f}".format("Training accuracy for perceptron:", pct_train_accuracy)) print("{:35} {:.4f}".format("Validation accuracy for perceptron:", pct_val_accuracy)) avg_pct_train_accuracy, avg_pct_val_accuracy = \ p1.classifier_accuracy(p1.average_perceptron, train_bow_features, val_bow_features, train_labels, val_labels, T=T) print("{:43} {:.4f}".format("Training accuracy for average perceptron:", avg_pct_train_accuracy)) print("{:43} {:.4f}".format("Validation accuracy for average perceptron:", avg_pct_val_accuracy)) avg_peg_train_accuracy, avg_peg_val_accuracy = \ p1.classifier_accuracy(p1.pegasos, train_bow_features, val_bow_features, train_labels, val_labels, T=T, L=L) print("{:50} {:.4f}".format("Training accuracy for Pegasos:", avg_peg_train_accuracy)) print("{:50} {:.4f}".format("Validation accuracy for Pegasos:", avg_peg_val_accuracy)) return
# utils.plot_tune_results('Avg Perceptron', 'T', Ts, *avg_pct_tune_results) # utils.plot_tune_results('Pegasos', 'T', Ts, *peg_tune_results_T) # utils.plot_tune_results('Pegasos', 'L', Ls, *peg_tune_results_L) #------------------------------------------------------------------------------- # Use the best method (perceptron, average perceptron or Pegasos) along with # the optimal hyperparameters according to validation accuracies to test # against the test dataset. The test data has been provided as # test_bow_features and test_labels. #------------------------------------------------------------------------------- print( p1.classifier_accuracy(p1.pegasos, train_bow_features, test_bow_features, train_labels, test_labels, T=25, L=0.01)) #------------------------------------------------------------------------------- # Assign to best_theta, the weights (and not the bias!) learned by your most # accurate algorithm with the optimal choice of hyperparameters. #------------------------------------------------------------------------------- best_theta, best_theta_0 = p1.pegasos(train_bow_features, train_labels, T=25, L=0.01) wordlist = [ word for (idx, word) in sorted(zip(dictionary.values(), dictionary.keys()))
utils.plot_toy_data(algo_name, toy_features, toy_labels, thetas) plot_toy_results('Perceptron', thetas_perceptron) plot_toy_results('Average Perceptron', thetas_avg_perceptron) plot_toy_results('Pegasos', thetas_pegasos) #------------------------------------------------------------------------------- # Problem 7 #------------------------------------------------------------------------------- T = 10 L = 0.01 pct_train_accuracy, pct_val_accuracy = \ p1.classifier_accuracy(p1.perceptron, train_bow_features,val_bow_features,train_labels,val_labels,T=T) print("{:35} {:.4f}".format("Training accuracy for perceptron:", pct_train_accuracy)) print("{:35} {:.4f}".format("Validation accuracy for perceptron:", pct_val_accuracy)) avg_pct_train_accuracy, avg_pct_val_accuracy = \ p1.classifier_accuracy(p1.average_perceptron, train_bow_features,val_bow_features,train_labels,val_labels,T=T) print("{:43} {:.4f}".format("Training accuracy for average perceptron:", avg_pct_train_accuracy)) print("{:43} {:.4f}".format("Validation accuracy for average perceptron:", avg_pct_val_accuracy)) avg_peg_train_accuracy, avg_peg_val_accuracy = \ p1.classifier_accuracy(p1.pegasos, train_bow_features,val_bow_features,train_labels,val_labels,T=T,L=L) print("{:50} {:.4f}".format("Training accuracy for Pegasos:",
#------------------------------------------------------------------------------- # Use the best method (perceptron, average perceptron or Pegasos) along with # the optimal hyperparameters according to validation accuracies to test # against the test dataset. The test data has been provided as # test_bow_features and test_labels. #------------------------------------------------------------------------------- # Your code here T = 25 L = 0.01 avg_peg_train_accuracy, avg_peg_test_accuracy = p1.classifier_accuracy( p1.pegasos, train_bow_features, test_bow_features, train_labels, test_labels, T=T, L=L) print("{:50} {:.4f}".format("Training accuracy for Pegasos:", avg_peg_train_accuracy)) print("{:50} {:.4f}".format("Test accuracy for Pegasos:", avg_peg_test_accuracy)) #------------------------------------------------------------------------------- # Assign to best_theta, the weights (and not the bias!) learned by your most # accurate algorithm with the optimal choice of hyperparameters. #------------------------------------------------------------------------------- # best_theta, _ = p1.pegasos(train_bow_features, train_labels, T=25, L=0.01) # Your code here # wordlist = [word for (idx, word) in sorted(zip(dictionary.values(), dictionary.keys()))]
print('theta_0 for', algo_name, 'is', str(thetas[1])) utils.plot_toy_data(algo_name, toy_features, toy_labels, thetas) #plot_toy_results('Perceptron', thetas_perceptron) #plot_toy_results('Average Perceptron', thetas_avg_perceptron) #plot_toy_results('Pegasos', thetas_pegasos) #------------------------------------------------------------------------------- # Problem 7 #------------------------------------------------------------------------------- T = 10 L = 0.01 pct_train_accuracy, pct_val_accuracy = \ p1.classifier_accuracy(p1.perceptron, train_bow_features,val_bow_features,train_labels,val_labels,T=T) print("{:35} {:.4f}".format("Training accuracy for perceptron:", pct_train_accuracy)) print("{:35} {:.4f}".format("Validation accuracy for perceptron:", pct_val_accuracy)) avg_pct_train_accuracy, avg_pct_val_accuracy = \ p1.classifier_accuracy(p1.average_perceptron, train_bow_features,val_bow_features,train_labels,val_labels,T=T) print("{:43} {:.4f}".format("Training accuracy for average perceptron:", avg_pct_train_accuracy)) print("{:43} {:.4f}".format("Validation accuracy for average perceptron:", avg_pct_val_accuracy)) avg_peg_train_accuracy, avg_peg_val_accuracy = \ p1.classifier_accuracy(p1.pegasos, train_bow_features,val_bow_features,train_labels,val_labels,T=T,L=L) print("{:50} {:.4f}".format("Training accuracy for Pegasos:", avg_peg_train_accuracy)) print("{:50} {:.4f}".format("Validation accuracy for Pegasos:",
print('theta_0 for', algo_name, 'is', str(thetas[1])) utils.plot_toy_data(algo_name, toy_features, toy_labels, thetas) plot_toy_results('Perceptron', thetas_perceptron) plot_toy_results('Average Perceptron', thetas_avg_perceptron) plot_toy_results('Pegasos', thetas_pegasos) #------------------------------------------------------------------------------- # Problem 7 #------------------------------------------------------------------------------- T = 10 L = 0.01 pct_train_accuracy, pct_val_accuracy = \ p1.classifier_accuracy(p1.perceptron, train_bow_features,val_bow_features,train_labels,val_labels,T=T) print("{:35} {:.4f}".format("Training accuracy for perceptron:", pct_train_accuracy)) print("{:35} {:.4f}".format("Validation accuracy for perceptron:", pct_val_accuracy)) # avg_pct_train_accuracy, avg_pct_val_accuracy = \ p1.classifier_accuracy(p1.average_perceptron, train_bow_features,val_bow_features,train_labels,val_labels,T=T) print("{:43} {:.4f}".format("Training accuracy for average perceptron:", avg_pct_train_accuracy)) print("{:43} {:.4f}".format("Validation accuracy for average perceptron:", avg_pct_val_accuracy)) # avg_peg_train_accuracy, avg_peg_val_accuracy = \ p1.classifier_accuracy(p1.pegasos, train_bow_features,val_bow_features,train_labels,val_labels,T=T,L=L) print("{:50} {:.4f}".format("Training accuracy for Pegasos:", avg_peg_train_accuracy)) print("{:50} {:.4f}".format("Validation accuracy for Pegasos:", avg_peg_val_accuracy)) #------------------------------------------------------------------------------- # Problem 8
plot_toy_results('Average Perceptron', thetas_avg_perceptron) plot_toy_results('Pegasos', thetas_pegasos) # In[ ]: #------------------------------------------------------------------------------- # Problem 7 #------------------------------------------------------------------------------- T = 10 L = 0.01 pct_train_accuracy, pct_val_accuracy = p1.classifier_accuracy( p1.perceptron, train_bow_features, val_bow_features, train_labels, val_labels, T=T) print("{:35} {:.4f}".format("Training accuracy for perceptron:", pct_train_accuracy)) print("{:35} {:.4f}".format("Validation accuracy for perceptron:", pct_val_accuracy)) avg_pct_train_accuracy, avg_pct_val_accuracy = p1.classifier_accuracy( p1.average_perceptron, train_bow_features, val_bow_features, train_labels, val_labels, T=T)
# ------------------------------------------------------------------------------- # Use the best method (perceptron, average perceptron or Pegasos) along with # the optimal hyperparameters according to validation accuracies to test # against the test dataset. The test data has been provided as # test_bow_features and test_labels. # ------------------------------------------------------------------------------- optimal_T = 25 optimal_L = 0.01 accuracy_train, accuracy_val = p1.classifier_accuracy( p1.pegasos, train_bow_features, test_bow_features, train_labels, test_labels, T=optimal_T, L=optimal_L, ) print("Accuracy train", accuracy_train, accuracy_val) # ------------------------------------------------------------------------------- # Assign to best_theta, the weights (and not the bias!) learned by your most # accurate algorithm with the optimal choice of hyperparameters. # ------------------------------------------------------------------------------- best_theta, _ = p1.pegasos(train_bow_features, train_labels, 25, 0.01) wordlist = [ word for (idx, word) in sorted(zip(dictionary.values(), dictionary.keys())) ]
# utils.plot_tune_results('Pegasos', 'T', Ts, *peg_tune_results_T) # utils.plot_tune_results('Pegasos', 'L', Ls, *peg_tune_results_L) #------------------------------------------------------------------------------- # Use the best method (perceptron, average perceptron or Pegasos) along with # the optimal hyperparameters according to validation accuracies to test # against the test dataset. The test data has been provided as # test_bow_features and test_labels. #------------------------------------------------------------------------------- peg_best_T = 25 peg_best_L = 0.01 peg_train_accuracy, peg_test_accuracy = p1.classifier_accuracy(p1.pegasos, train_bow_features, test_bow_features, train_labels, test_labels, T=peg_best_T, L=peg_best_L) print(peg_test_accuracy) peg_theta, peg_theta_0 = p1.pegasos(train_bow_features, train_labels, peg_best_T, peg_best_L) print(peg_theta, peg_theta_0) peg_test_preds = p1.classify(test_bow_features, peg_theta, peg_theta_0) #------------------------------------------------------------------------------- # Assign to best_theta, the weights (and not the bias!) learned by your most # accurate algorithm with the optimal choice of hyperparameters. #-------------------------------------------------------------------------------
# print('best = {:.4f}, L={:.4f}'.format(np.max(peg_tune_results_L[1]), Ls[np.argmax(peg_tune_results_L[1])])) # utils.plot_tune_results('Perceptron', 'T', Ts, *pct_tune_results) # utils.plot_tune_results('Avg Perceptron', 'T', Ts, *avg_pct_tune_results) # utils.plot_tune_results('Pegasos', 'T', Ts, *peg_tune_results_T) # utils.plot_tune_results('Pegasos', 'L', Ls, *peg_tune_results_L) #------------------------------------------------------------------------------- # Use the best method (perceptron, average perceptron or Pegasos) along with # the optimal hyperparameters according to validation accuracies to test # against the test dataset. The test data has been provided as # test_bow_features and test_labels. #------------------------------------------------------------------------------- hyper_param = dict({'T': 25, 'L': 0.01}) accuracy_train, accuracy_test = p1.classifier_accuracy(p1.pegasos, train_bow_features, \ test_bow_features, train_labels, test_labels, **hyper_param) print("accuracy on test set= {:.4f} ".format(accuracy_test)) #------------------------------------------------------------------------------- # Assign to best_theta, the weights (and not the bias!) learned by your most # accurate algorithm with the optimal choice of hyperparameters. #------------------------------------------------------------------------------- best_theta = p1.pegasos(train_bow_features, train_labels, hyper_param.get('T'), hyper_param.get('L'))[0] wordlist = [ word for (idx, word) in sorted(zip(dictionary.values(), dictionary.keys())) ] sorted_word_features = utils.most_explanatory_word(best_theta, wordlist) print("Most Explanatory Word Features") print(sorted_word_features[:10])
#utils.plot_tune_results('Perceptron', 'T', Ts, *pct_tune_results) #utils.plot_tune_results('Avg Perceptron', 'T', Ts, *avg_pct_tune_results) #utils.plot_tune_results('Pegasos', 'T', Ts, *peg_tune_results_T) #utils.plot_tune_results('Pegasos', 'L', Ls, *peg_tune_results_L) #------------------------------------------------------------------------------- # Use the best method (perceptron, average perceptron or Pegasos) along with # the optimal hyperparameters according to validation accuracies to test # against the test dataset. The test data has been provided as # test_bow_features and test_labels. #------------------------------------------------------------------------------- avg_accuracy = p1.classifier_accuracy(p1.pegasos, train_bow_features, test_bow_features, train_labels, test_labels, T=25, L=0.0100) print("%.4f" % avg_accuracy[1]) #------------------------------------------------------------------------------- # Assign to best_theta, the weights (and not the bias!) learned by your most # accurate algorithm with the optimal choice of hyperparameters. #------------------------------------------------------------------------------- #best_theta = p1.pegasos(train_bow_features,train_labels, T=25, L=0.01)[0] #wordlist = [word for (idx, word) in sorted(zip(dictionary.values(), dictionary.keys()))] #sorted_word_features = utils.most_explanatory_word(best_theta, wordlist) #print("Most Explanatory Word Features") #print(sorted_word_features[:10])