# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # 8. Parameter Tuning

# +
import project1 as p1
import utils
import numpy as np

#-------------------------------------------------------------------------------
# Data loading. There is no need to edit code in this section.
#-------------------------------------------------------------------------------

train_data = utils.load_data('reviews_train.tsv')
val_data = utils.load_data('reviews_val.tsv')
test_data = utils.load_data('reviews_test.tsv')

train_texts, train_labels = zip(*((sample['text'], sample['sentiment']) for sample in train_data))
val_texts, val_labels = zip(*((sample['text'], sample['sentiment']) for sample in val_data))
test_texts, test_labels = zip(*((sample['text'], sample['sentiment']) for sample in test_data))

dictionary = p1.bag_of_words(train_texts)

train_bow_features = p1.extract_bow_feature_vectors(train_texts, dictionary)
val_bow_features = p1.extract_bow_feature_vectors(val_texts, dictionary)
test_bow_features = p1.extract_bow_feature_vectors(test_texts, dictionary)

# +
#Pegasus T= 25
theta, theta_0= p1.pegasos(test_bow_features, test_labels, 25,0.01)

def accuracy(preds, targets):
    return (preds == targets).mean()

def classify(feature_matrix, theta, theta_0):
    return((np.sum(feature_matrix*theta, axis=1) + theta_0) > 0)*2 - 1

def classifier_accuracy(classifier, train_feature_matrix, val_feature_matrix, train_labels, val_labels, T, L):
    train_theta, train_theta_0 = classifier(train_feature_matrix, train_labels, T, L)
    train_predictions= classify(train_feature_matrix, train_theta, train_theta_0)
    trained_validation  = classify(val_feature_matrix, train_theta, train_theta_0)
    train_accuracy = accuracy(train_predictions, train_labels)
    val_accuracy = accuracy(trained_validation, val_labels)
    return(train_accuracy, val_accuracy)

accuracy = classifier_accuracy(p1.pegasos, train_bow_features, test_bow_features,train_labels,test_labels, 25, 0.01)
#-------------------------------------------------------------------------------
#Assign to best_theta, the weights (and not the bias!) learned by your most
#accurate algorithm with the optimal choice of hyperparameters.
#-------------------------------------------------------------------------------
theta, theta_0= p1.pegasos(test_bow_features, test_labels, 25,0.01)
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[1:10])

# +
import utils
theta, theta_0= p1.pegasos(train_bow_features, train_labels, 25,0.01)

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[0:10])

StarcoderdataJupyterScriptsDedupFiltered