Beispiel #1
0
def tune(train_fn, param_vals, train_feats, train_labels, val_feats,
         val_labels):
    train_accs = np.ndarray(len(param_vals))
    val_accs = np.ndarray(len(param_vals))
    for i, val in enumerate(param_vals):
        theta, theta_0 = train_fn(train_feats, train_labels, val)

        train_preds = p1.classify(train_feats, theta, theta_0)
        train_accs[i] = p1.accuracy(train_preds, train_labels)

        val_preds = p1.classify(val_feats, theta, theta_0)
        val_accs[i] = p1.accuracy(val_preds, val_labels)
    return train_accs, val_accs
Beispiel #2
0
def tune(train_fn, param_vals, train_feats, train_labels, val_feats, val_labels):
    train_accs = np.ndarray(len(param_vals))
    val_accs = np.ndarray(len(param_vals))

    for i, val in enumerate(param_vals):
        theta, theta_0 = train_fn(train_feats, train_labels, val)

        train_preds = p1.classify(train_feats, theta, theta_0)
        train_accs[i] = p1.accuracy(train_preds, train_labels)

        val_preds = p1.classify(val_feats, theta, theta_0)
        val_accs[i] = p1.accuracy(val_preds, val_labels)

    return train_accs, val_accs
    def test_classification_test_dataset(self):
        # -------------------------------------------------------------------------------
        # 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.
        # -------------------------------------------------------------------------------
        T = 25
        L = 0.01
        theta, theta_0 = p1.pegasos(feature_matrix=train_bow_features, labels=train_labels, T=T, L=L)
        pred_labels = p1.classify(test_bow_features, theta, theta_0)
        accuracy = p1.accuracy(pred_labels, test_labels)
        print(f'Accuracy on test data : {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 = theta
        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])
        print("Least Explanatory Word Features")
        print(sorted_word_features[-10:])
        return
Beispiel #4
0
# against the test dataset. The test data has been provided as
# test_bow_features and test_labels.
#-------------------------------------------------------------------------------

# Your code here

data = (train_bow_features, train_labels, test_bow_features, test_labels)

theta, theta_Q = p1.pegasos(train_bow_features, train_labels, 25, 0.01)
# values of T and lambda to try
# Ts = [25]
# fix_L = 0.01


preds = p1.classify(test_bow_features, theta, theta_Q)
print(p1.accuracy(preds, test_labels))
print(theta)
# peg_tune_results_T = utils.tune_pegasos_T(fix_L, Ts, *data)
# print('Pegasos valid: tune T', list(zip(Ts, peg_tune_results_T[1])))
# print('best = {:.4f}, T={:.4f}'.format(
    # np.max(peg_tune_results_T[1]), Ts[np.argmax(peg_tune_results_T[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 = theta
Beispiel #5
0
# 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.
#-------------------------------------------------------------------------------

T = 25
L = 0.01
theta, theta_0 = p1.pegasos(train_bow_features, train_labels, T, L)
test_result_labels = p1.classify(test_bow_features, theta, theta_0)
print("pegasos accuracy on test:{:.4f}".format(
    p1.accuracy(test_result_labels, test_labels)))
#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------
print("best_theta:{}".format(theta))
best_theta = theta
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])
print("Most Negative Explanatory Word Features")
print(sorted_word_features[::-1][:10])
Beispiel #6
0
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.
#-------------------------------------------------------------------------------

(test_theta, test_theta_0) = p1.pegasos(train_bow_features, train_labels, 25,
                                        0.01)
n = np.size(test_bow_features, 0)
z = p1.classify(test_bow_features, test_theta, test_theta_0)
train_error = p1.accuracy(z, test_labels)
print("Train accuracy is: ", train_error)
# #-------------------------------------------------------------------------------
# # Assign to best_theta, the weights (and not the bias!) learned by your most
# # accurate algorithm with the optimal choice of hyperparameters.
# #-------------------------------------------------------------------------------

best_theta = test_theta
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])
Beispiel #7
0
# 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("")
print("#######")
print("pegasos accuracy on test set")
print("#######")

thetas = p1.pegasos(train_bow_features, train_labels, T=25, L=0.01)
test_classified = p1.classify(test_bow_features, thetas[0], thetas[1])
print(p1.accuracy(test_classified, test_labels))

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

best_theta = thetas[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])
Beispiel #8
0
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.
#-------------------------------------------------------------------------------

#Your code here

T = 25
L =0.0100
Q_theta, Q_theta_0 = p1.pegasos(train_bow_features, train_labels, T, L)
Q_pred = p1.classify(test_bow_features, Q_theta, Q_theta_0)
Q_accuracy = p1.accuracy(Q_pred, test_labels)
print(Q_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 = Q_theta # Your code here
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])
print(sorted_word_features[-10:])
fix_T = Ts[np.argmax(peg_tune_results_T[1])]
peg_tune_results_L = utils.tune_pegasos_L(fix_T, Ls, *data)
print('Pegasos valid: tune L', list(zip(Ls, peg_tune_results_L[1])))
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)

#Using the best method (perceptron, average perceptron or Pegasos) along with
#the optimal hyperparameters according to validation accuracies to test
#against the test data.
optimal_T = 25
optimal_eta = 0.0100
best_theta, best_theta_0 = p1.pegasos(train_bow_features, train_labels,
                                      optimal_T, optimal_eta)
best_one = p1.classify(test_bow_features, best_theta, best_theta_0)
best_one_accuracy = p1.accuracy(best_one, test_labels)
print(best_one_accuracy)

#Assign to best_theta, the weights learned by most accurate algorithm
#with the optimal choice of hyperparameters.
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])
Beispiel #10
0
# 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.
#-------------------------------------------------------------------------------

# Your code here
theta, theta_0 = p1.pegasos(train_bow_features, train_labels, 25, 0.01)
labels = p1.classify(test_bow_features, theta, theta_0)
print(p1.accuracy(labels, test_labels))

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

best_theta = theta  # Your code here
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])
Beispiel #11
0
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.
#-------------------------------------------------------------------------------

theta, theta_0 = p1.pegasos(train_bow_features, train_labels, T = 25, L = 0.01)
test_preds = p1.classify(test_bow_features, theta, theta_0)
test_accu = p1.accuracy(test_preds, test_labels)
print(test_accu)

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

best_theta = theta
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])


# =============================================================================
Beispiel #12
0
# 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.
#-------------------------------------------------------------------------------

# Your code here
L_best = 0.01
T_best = 25
theta, theta_0 = p1.pegasos(train_bow_features,
                            train_labels,
                            L=L_best,
                            T=T_best)
labels_hat = p1.classify(test_bow_features, theta, theta_0)
print(p1.accuracy(labels_hat, test_labels))
#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

best_theta = theta
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:])
Beispiel #13
0
# 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.
#-------------------------------------------------------------------------------

# Your code here
T = 25
L = 0.0100
theta, theta_0 = p1.pegasos(train_bow_features,train_labels,T,L)
train_accuracy = p1.accuracy(p1.classify(train_bow_features,theta, theta_0),train_labels)
val_accuracy = p1.accuracy(p1.classify(val_bow_features,theta, theta_0),val_labels)
test_accuracy = p1.accuracy(p1.classify(test_bow_features,theta, theta_0),test_labels)

print (train_accuracy, val_accuracy, test_accuracy)

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------
# T = 25
# L = 0.0100
# best_theta = p1.pegasos(train_bow_features,train_labels,T,L)[0] # Your code here
# 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")
peg_tune_results_L = utils.tune_pegasos_L(fix_T, Ls, *data)
print('Pegasos valid: tune L', list(zip(Ls, peg_tune_results_L[1])))
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.
#-------------------------------------------------------------------------------

theta, theta_0 = p1.pegasos(train_bow_features, train_labels, 25, 0.0100)
predicted_labels = p1.classify(test_bow_features, theta, theta_0)
print(p1.accuracy(predicted_labels, test_labels))

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

# best_theta = None # Your code here
# 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])
Beispiel #15
0
# 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.
#-------------------------------------------------------------------------------

# Your code here

T = 25
L = 0.01

theta, theta_0 = p1.pegasos(train_bow_features,train_labels,T,L)

predictions = p1.classify(test_bow_features,theta, theta_0)
test_accuracy = p1.accuracy(predictions,test_labels)
print(f"test_accuracy: {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 = theta # Your code here
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])
Beispiel #16
0
# print('Pegasos valid: tune L', list(zip(Ls, peg_tune_results_L[1])))
# 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.
#-------------------------------------------------------------------------------

theta, theta_0 = p1.pegasos(train_bow_features, train_labels, 25, 0.01)
test_classified = p1.classify(test_bow_features, theta, theta_0)
acc = p1.accuracy(test_classified, test_labels)
print('Accuracy: ', acc)

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

# best_theta = theta # Your code here
# 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])
Beispiel #17
0
# 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.
#-------------------------------------------------------------------------------

# Your code here
T = 25
L = 0.01

theta, theta_0 = p1.pegasos(train_bow_features, train_labels, T, L)
results = p1.classify(test_bow_features, theta, theta_0)
print('Pegasos accuracy results:', p1.accuracy(results, test_labels))

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

best_theta = theta  # Your code here
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])
Beispiel #18
0
# 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 Hyperparameters for Pegasos
T, L = 25, 0.01

# Calculates the parameters using the optimal hyperparameters
theta, theta_0 = p1.pegasos(train_bow_features, train_labels, T, L)

# Classify the test data using the optimal hyperparameters
h_test = p1.classify(test_bow_features, theta, theta_0)

# Calculates the accuracy of the test data set
accuracy_test = p1.accuracy(h_test, test_labels)

print(f"Pegasos Parameters - Theta: {theta} | Theta_0: {theta_0}")
print(f"Pegasos Accuracy on test data set is {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 = theta  # Your code here
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")
Beispiel #19
0
#-------------------------------------------------------------------------------
# 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.
#-------------------------------------------------------------------------------

#learn theta and theta_0 from the training data
theta, theta_0 = p1.pegasos(train_bow_features, train_labels, 25, 0.01)

#create a prediction vector from the test data
prediction_vector = p1.classify(test_bow_features, theta, theta_0)

#calculate the accuracy on the test data
test_accuracy = p1.accuracy(prediction_vector, test_labels)

print("Accuracy on the test set: {:.3f}".format(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 = theta
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])
Beispiel #20
0
# peg_tune_results_L = utils.tune_pegasos_L(fix_T, Ls, *data)
# print('Pegasos valid: tune L', list(zip(Ls, peg_tune_results_L[1])))
# 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.
#-------------------------------------------------------------------------------

t, t0 = p1.pegasos(train_bow_features, train_labels, 25, 0.01)
y_pred = p1.classify(test_bow_features, t, t0)
print(p1.accuracy(y_pred, test_labels))

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

# best_theta = t
# 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('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.
#-------------------------------------------------------------------------------

theta, theta_0 = p1.pegasos(train_bow_features, train_labels, 25, 0.01)

pred_test = p1.classify(test_bow_features, theta, theta_0)

test_acc = p1.accuracy(pred_test, test_labels)

print("{:50} {:.4f}".format("Test accuracy for Pegasos:", test_acc))

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

best_theta = theta
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])
Beispiel #22
0
#
# 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.
#-------------------------------------------------------------------------------

# Your code here
theta, theta_0 = p1.pegasos(train_bow_features, train_labels, 25, 0.0100)
test_pred = p1.classify(test_bow_features, theta, theta_0)
test_acc = p1.accuracy(test_pred, test_labels)
print(test_acc)
#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

best_theta = theta
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])
Beispiel #23
0
# 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.
#-------------------------------------------------------------------------------

# Your code here
theta, theta_0 = p1.pegasos(train_bow_features, train_labels, 25, 0.01)
class_test = p1.classify(test_bow_features, theta, theta_0)
print(p1.accuracy(class_test, test_labels))
#0.802
#0.808
#0.77

#-------------------------------------------------------------------------------
# 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()))]
# sorted_word_features = utils.most_explanatory_word(best_theta, wordlist)
# print("Most Explanatory Word Features")
# print(sorted_word_features[:10])
Beispiel #24
0
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_bow_features.
#-------------------------------------------------------------------------------

print("")
# Your code here
T = 25
L = 0.01
b_theta, b_theta_0 = p1.pegasos(train_bow_features, train_labels, T, L)
result_labels = p1.classify(test_bow_features, b_theta, b_theta_0)
acc = p1.accuracy(test_labels, result_labels)
print(b_theta)
print(b_theta_0)
print(acc)

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------

best_theta = b_theta  # Your code here
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")
Beispiel #25
0
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.
#-------------------------------------------------------------------------------

best_theta, best_theta_0 = p1.pegasos(train_bow_features, train_labels, 25,
                                      0.01)
best_accuracy = p1.accuracy(
    p1.classify(test_bow_features, best_theta, best_theta_0), test_labels)
print('Accuracy on the test set: {:.4f}'.format(best_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 = best_theta
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])
Beispiel #26
0
'''

#-------------------------------------------------------------------------------
# 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
th, th_0 = p1.pegasos(train_bow_features, train_labels, T, L)
rezul = p1.classify(test_bow_features, th, th_0)
print('REZ = {:.4f}'.format(p1.accuracy(rezul, test_labels)))

#-------------------------------------------------------------------------------
# Assign to best_theta, the weights (and not the bias!) learned by your most
# accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------
'''
T = 25
L = 0.01
th, th_0 = p1.pegasos(train_bow_features , train_labels, T, L)
best_theta = th
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])
'''
Beispiel #27
0
# 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.
#-------------------------------------------------------------------------------

# Your code here

#-------------------------------------------------------------------------------
# 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.0100)[0]
print("Accuracy on test",
      p1.accuracy(p1.classify(test_bow_features, best_theta, 0), test_labels))
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])