Пример #1
0
#
#-------------------------------------------------------------------------------
# Section 2.9.b
#-------------------------------------------------------------------------------
T = 5
L = 1

pct_train_accuracy, pct_val_accuracy = \
   p1.perceptron_accuracy(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.average_perceptron_accuracy(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_pa_train_accuracy, avg_pa_val_accuracy = \
   p1.average_passive_aggressive_accuracy(train_bow_features,val_bow_features,train_labels,val_labels,T=T,L=L)
print("{:50} {:.4f}".format(
    "Training accuracy for average passive-aggressive:",
    avg_pa_train_accuracy))
print("{:50} {:.4f}".format(
    "Validation accuracy for average passive-aggressive:",
    avg_pa_val_accuracy))
##-------------------------------------------------------------------------------
#
Пример #2
0
# avg_peg_tune_results_L = utils.tune_pegasos_L(best_T, Ls, *data)

# 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, *avg_peg_tune_results_T)
# utils.plot_tune_results('Pegasos', 'L', Ls, *avg_peg_tune_results_L)
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# Section 2.11a
#
# Call one of the accuracy functions that you wrote in part 2.9.a and report
# the hyperparameter and accuracy of your best classifier on the test data.
# The test data has been provided as test_bow_features and test_labels.

x = p1.average_perceptron_accuracy(train_bow_features, test_bow_features, train_labels, test_labels, T=25)
print(x)


#-------------------------------------------------------------------------------
# pass
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# Section 2.11b
#
# 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.average_perceptron(test_bow_features, test_labels, T=25)[0]
wordlist   = [word for (idx, word) in sorted(zip(dictionary.values(), dictionary.keys()))]
#
# After constructing a final feature representation, use code similar to that in
# sections 2.9b and 2.10 to assess its performance on the validation set.
# You may use your best classifier from before as a baseline.
# When you are satisfied with your features, evaluate their accuracy on the test
# set using the same procedure as in section 2.11a.
#-------------------------------------------------------------------------------

## Base Case
dictionary = p1.bag_of_words(train_texts)

train_final_features = p1.extract_final_features(train_texts, dictionary)
val_final_features   = p1.extract_final_features(val_texts, dictionary)
test_final_features  = p1.extract_final_features(test_texts, dictionary)

print(p1.average_perceptron_accuracy(train_final_features,val_final_features,train_labels,val_labels,T=15))
print(p1.average_perceptron_accuracy(val_final_features,test_final_features,val_labels,test_labels,T=15))

## Improve 1
dictionary = p1.bag_of_better_words(train_texts)

train_final_features = p1.extract_final_features(train_texts, dictionary)
val_final_features   = p1.extract_final_features(val_texts, dictionary)
test_final_features  = p1.extract_final_features(test_texts, dictionary)

#data = (train_final_features, train_labels, val_final_features, val_labels)
#
## values of T and lambda to try
#Ts = [1, 5, 10, 15, 25, 50, 100]
#
#avg_pct_tune_results = utils.tune_avg_perceptron(Ts, *data)
Пример #4
0
# utils.plot_tune_results('Pegasos', 'T', Ts, *avg_peg_tune_results_T)
# utils.plot_tune_results('Pegasos', 'L', Ls, *avg_peg_tune_results_L)
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# Section 2.11a
#
# Call one of the accuracy functions that you wrote in part 2.9.a and report
# the hyperparameter and accuracy of your best classifier on the test data.
# The test data has been provided as test_bow_features and test_labels.
#-------------------------------------------------------------------------------

best_T_case = 15

print(
    p1.average_perceptron_accuracy(train_bow_features, test_bow_features,
                                   train_labels, test_labels, best_T_case))

#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# Section 2.11b
#
# 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.average_perceptron(train_bow_features, train_labels, 50)[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])
#-------------------------------------------------------------------------------