<jupyter_start><jupyter_text>Vectorization<jupyter_code>import warnings
warnings.filterwarnings("ignore")                     #Ignoring unnecessory warnings
from tqdm import tqdm
import numpy as np                                  
import pandas as pd                                 
from sklearn.feature_extraction.text import CountVectorizer          #For Bag of words
from sklearn.feature_extraction.text import TfidfTransformer
import matplotlib.pyplot as plt
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import confusion_matrix
import itertools
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
full_train = pd.read_csv('./preprocess/processed_train/full_train_data.csv',sep = '\t')
full_train.head()
full_test = pd.read_csv('./preprocess/processed_test/full_test_data.csv',sep = '\t')
full_test.head()<jupyter_output><empty_output><jupyter_text>Full train/test label and feature  Labels for train and test<jupyter_code># Get labels
full_train['sentiment_actual'] = full_train.sentiment_actual.map({'neg': 0, 'pos': 1})
full_train_label = full_train['sentiment_actual']

full_test['sentiment_actual'] = full_test.sentiment_actual.map({'neg': 0, 'pos': 1})
full_test_label = full_test['sentiment_actual']
camera_train = full_train.loc[full_train['product_category'] == "Camera"]
camera_test = full_test.loc[full_test['product_category'] == "Camera"]
camera_train.head()
# Labels
camera_train_label = camera_train['sentiment_actual']
camera_test_label = camera_test['sentiment_actual']
grocery_train = full_train.loc[full_train['product_category'] == "Grocery"]
grocery_test = full_test.loc[full_test['product_category'] == "Grocery"]
grocery_train.head()
# Labels
grocery_train_label = grocery_train['sentiment_actual']
grocery_test_label = grocery_test['sentiment_actual']
watches_train = full_train.loc[full_train['product_category'] == "Watches"]
watches_test = full_test.loc[full_test['product_category'] == "Watches"]
watches_train.head()
# Labels
watches_train_label = watches_train['sentiment_actual']
watches_test_label = watches_test['sentiment_actual']
videogames_train = full_train.loc[full_train['product_category'] == "Video Games"]
videogames_test = full_test.loc[full_test['product_category'] == "Video Games"]
videogames_train.head()
# Labels
videogames_train_label = videogames_train['sentiment_actual']
videogames_test_label = videogames_test['sentiment_actual']<jupyter_output><empty_output><jupyter_text>Features for train and testVectorize using bag of words<jupyter_code># Get processed texts
train_texts = full_train['processed'].astype('U')
test_texts = full_test['processed'].astype('U')

camera_train_texts = camera_train['processed'].astype('U')
camera_test_texts = camera_test['processed'].astype('U')

grocery_train_texts = grocery_train['processed'].astype('U')
grocery_test_texts = grocery_test['processed'].astype('U')

watches_train_texts = watches_train['processed'].astype('U')
watches_test_texts = watches_test['processed'].astype('U')

videogames_train_texts = videogames_train['processed'].astype('U')
videogames_test_texts = videogames_test['processed'].astype('U')

# count vectorize
count_vect = CountVectorizer()

train_counts = count_vect.fit_transform(train_texts)
test_counts = count_vect.transform(test_texts)

camera_train_counts = count_vect.transform(camera_train_texts)
camera_test_counts = count_vect.transform(camera_test_texts)

grocery_train_counts = count_vect.transform(grocery_train_texts)
grocery_test_counts = count_vect.transform(grocery_test_texts)

watches_train_counts = count_vect.transform(watches_train_texts)
watches_test_counts = count_vect.transform(watches_test_texts)

videogames_train_counts = count_vect.transform(videogames_train_texts)
videogames_test_counts = count_vect.transform(videogames_test_texts)

# Transform with tfidf
transformer = TfidfTransformer()

transformer = transformer.fit(train_counts)
transformer = transformer.fit(test_counts)

transformer = transformer.fit(camera_train_counts)
transformer = transformer.fit(camera_test_counts)

transformer = transformer.fit(grocery_train_counts)
transformer = transformer.fit(grocery_test_counts)

transformer = transformer.fit(watches_train_counts)
transformer = transformer.fit(watches_test_counts)

transformer = transformer.fit(videogames_train_counts)
transformer = transformer.fit(videogames_test_counts)

# Get features
train_features = transformer.transform(train_counts)
test_features = transformer.transform(test_counts)

camera_train_features = transformer.transform(camera_train_counts)
camera_test_features = transformer.transform(camera_test_counts)

grocery_train_features = transformer.transform(grocery_train_counts)
grocery_test_features = transformer.transform(grocery_test_counts)

watches_train_features = transformer.transform(watches_train_counts)
watches_test_features = transformer.transform(watches_test_counts)

videogames_train_features = transformer.transform(videogames_train_counts)
videogames_test_features = transformer.transform(videogames_test_counts)
print(train_features.shape[0])
print(full_train_label.shape[0])
print(test_features.shape[0])
print(full_test_label.shape[0])
print(camera_train_features.shape[0])
print(grocery_train_features.shape[0])
print(watches_train_features.shape[0])
print(videogames_train_features.shape[0])

print(camera_test_features.shape[0])
print(grocery_test_features.shape[0])
print(watches_test_features.shape[0])
print(videogames_test_features.shape[0])<jupyter_output>100000
100000
100000
100000
19914
20136
20266
19522<jupyter_text>Binary Classification<jupyter_code>def eval(name, y_gold, y_pred, verbose=1, setting='macro'):
    '''Yield metrics accuracy, precision, recall and f1-score'''
    eval_measures = precision_recall_fscore_support(y_gold, y_pred, average=setting)
    
    if verbose==1:
        print("{} acc: {:4f} prec: {:4f} rec: {:4f} f1: {:4f}".format(
        name, accuracy_score(y_gold, y_pred), eval_measures[0], eval_measures[1], eval_measures[2]))       
    return eval_measures
# For plotting confusion matrix
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('Predicted label')
    plt.xlabel('True label')<jupyter_output><empty_output><jupyter_text>Naive bayes<jupyter_code>naivebayes = MultinomialNB()
naivebayes.fit(train_features, full_train_label)
predict_nb = naivebayes.predict(test_features)
cm_nb = confusion_matrix(full_test_label,predict_nb)
plot_confusion_matrix(cm_nb, classes=naivebayes.classes_.tolist(),
                      title='Naive Bayes(Full train, full test)')
# plt.show()
plt.savefig('./plots/full_full_nb.png')
full_full_eval_nb = eval("Naive Bayes", full_test_label, predict_nb)<jupyter_output>Naive Bayes acc: 0.842995 prec: 0.739344 rec: 0.849266 f1: 0.769886<jupyter_text>Test on camera<jupyter_code>camera_predict_nb = naivebayes.predict(camera_test_features)
cm_nb = confusion_matrix(camera_test_label,camera_predict_nb)
plot_confusion_matrix(cm_nb, classes=naivebayes.classes_.tolist(),
                      title='Naive Bayes(Full train, camera test)')
plt.savefig('./plots/full_camera_nb.png')
full_camera_eval_nb = eval("Naive Bayes", camera_test_label, camera_predict_nb)<jupyter_output>Naive Bayes acc: 0.825148 prec: 0.723967 rec: 0.854609 f1: 0.752214<jupyter_text>Test on grocery<jupyter_code>grocery_predict_nb = naivebayes.predict(grocery_test_features)
cm_nb = confusion_matrix(grocery_test_label,grocery_predict_nb)
plot_confusion_matrix(cm_nb, classes=naivebayes.classes_.tolist(),
                      title='Naive Bayes(Full train, grocery test)')
plt.savefig('./plots/full_grocery_nb.png')
full_grocery_eval_nb = eval("Naive Bayes", grocery_test_label, grocery_predict_nb)<jupyter_output>Naive Bayes acc: 0.850020 prec: 0.729260 rec: 0.802216 f1: 0.755148<jupyter_text>Test on watches<jupyter_code>watches_predict_nb = naivebayes.predict(watches_test_features)
cm_nb = confusion_matrix(watches_test_label,watches_predict_nb)
plot_confusion_matrix(cm_nb, classes=naivebayes.classes_.tolist(),
                      title='Naive Bayes(Full train, watches test)')
plt.savefig('./plots/full_watches_nb.png')
full_watches_eval_nb = eval("Naive Bayes", watches_test_label, watches_predict_nb)<jupyter_output>Naive Bayes acc: 0.881674 prec: 0.780106 rec: 0.883060 f1: 0.814971<jupyter_text>Test on videogames<jupyter_code>videogames_predict_nb = naivebayes.predict(videogames_test_features)
cm_nb = confusion_matrix(videogames_test_label,videogames_predict_nb)
plot_confusion_matrix(cm_nb, classes=naivebayes.classes_.tolist(),
                      title='Naive Bayes(Full train, videogames test)')
plt.savefig('./plots/full_videogames_nb.png')
full_videogames_eval_nb = eval("Naive Bayes", videogames_test_label, videogames_predict_nb)<jupyter_output>Naive Bayes acc: 0.813800 prec: 0.733132 rec: 0.851882 f1: 0.756104<jupyter_text>Logistic regression<jupyter_code>logregression = LogisticRegression()
logregression.fit(train_features, full_train_label)
predict_lr = logregression.predict(test_features)
cm_lr = confusion_matrix(full_test_label,predict_lr)
plot_confusion_matrix(cm_lr, classes=logregression.classes_.tolist(),
                      title='Logistic Regression(Full train, full test)')
plt.savefig('./plots/full_full_lr.png')
full_full_eval_lr = eval("Logistic Regression", full_test_label, predict_lr)<jupyter_output>Logistic Regression acc: 0.894774 prec: 0.800089 rec: 0.888313 f1: 0.832783<jupyter_text>Test on camera<jupyter_code>camera_predict_lr = logregression.predict(camera_test_features)
cm_lr = confusion_matrix(camera_test_label,camera_predict_lr)
plot_confusion_matrix(cm_lr, classes=logregression.classes_.tolist(),
                      title='Logistic regression(Full train, camera test)')
plt.savefig('./plots/full_camera_lr.png')
full_camera_eval_lr = eval("Logistic Regerssion", camera_test_label, camera_predict_lr)<jupyter_output>Logistic Regerssion acc: 0.901828 prec: 0.806006 rec: 0.904651 f1: 0.841892<jupyter_text>Test on grocery<jupyter_code>grocery_predict_lr = logregression.predict(grocery_test_features)
cm_lr = confusion_matrix(grocery_test_label,grocery_predict_lr)
plot_confusion_matrix(cm_lr, classes=logregression.classes_.tolist(),
                      title='Logistic regression(Full train, grocery test)')
plt.savefig('./plots/full_grocery_lr.png')
full_grocery_eval_lr = eval("Logistic Regerssion", grocery_test_label, grocery_predict_lr)<jupyter_output>Logistic Regerssion acc: 0.873808 prec: 0.762649 rec: 0.838229 f1: 0.790949<jupyter_text>Test on watches<jupyter_code>watches_predict_lr = logregression.predict(watches_test_features)
cm_lr = confusion_matrix(watches_test_label,watches_predict_lr)
plot_confusion_matrix(cm_lr, classes=logregression.classes_.tolist(),
                      title='Logistic regression(Full train, watches test)')
plt.savefig('./plots/full_watches_lr.png')
full_watches_eval_lr = eval("Logistic Regerssion", watches_test_label, watches_predict_lr)<jupyter_output>Logistic Regerssion acc: 0.913550 prec: 0.824309 rec: 0.914100 f1: 0.858764<jupyter_text>Test on video games<jupyter_code>videogames_predict_lr = logregression.predict(videogames_test_features)
cm_lr = confusion_matrix(videogames_test_label,videogames_predict_lr)
plot_confusion_matrix(cm_lr, classes=logregression.classes_.tolist(),
                      title='Logistic regression(Full train, videogames test)')
plt.savefig('./plots/full_videogames_lr.png')
full_videogames_eval_lr = eval("Logistic Regerssion", videogames_test_label, videogames_predict_lr)<jupyter_output>Logistic Regerssion acc: 0.889714 prec: 0.806239 rec: 0.894032 f1: 0.837709<jupyter_text>Individual class  Camera  Naive Bayes<jupyter_code># Naive Bayes
naivebayes = MultinomialNB()
naivebayes.fit(camera_train_features, camera_train_label)
camera_predict_nb = naivebayes.predict(camera_test_features)
cm_nb = confusion_matrix(camera_test_label,camera_predict_nb)
plot_confusion_matrix(cm_nb, classes=naivebayes.classes_.tolist(),
                      title='Naive Bayes(Camera train, Camera test)')
plt.savefig('./plots/camera_camera_nb.png')
camera_camera_eval_nb = eval("Naive Bayes", camera_test_label, camera_predict_nb)<jupyter_output>Naive Bayes acc: 0.834237 prec: 0.730242 rec: 0.855593 f1: 0.760436<jupyter_text>Logistic Regression<jupyter_code># Logistic regression
logregression = LogisticRegression()
logregression.fit(camera_train_features, camera_train_label)
camera_predict_lr = logregression.predict(camera_test_features)
cm_lr = confusion_matrix(camera_test_label,camera_predict_lr)
plot_confusion_matrix(cm_lr, classes=logregression.classes_.tolist(),
                      title='Logistic Regression(Camera train, camera test)')
plt.savefig('./plots/camera_camera_lr.png')
camera_camera_eval_lr = eval("Logistic Regerssion", camera_test_label, camera_predict_lr)<jupyter_output>Logistic Regerssion acc: 0.903686 prec: 0.808628 rec: 0.905105 f1: 0.844153<jupyter_text>Grocery  Naive Bayes<jupyter_code># Naive Bayes
naivebayes = MultinomialNB()
naivebayes.fit(grocery_train_features, grocery_train_label)
grocery_predict_nb = naivebayes.predict(grocery_test_features)
cm_nb = confusion_matrix(grocery_test_label,grocery_predict_nb)
plot_confusion_matrix(cm_nb, classes=naivebayes.classes_.tolist(),
                      title='Naive Bayes(Grocery train, grocery test)')
plt.savefig('./plots/grocery_grocery_nb.png')
grocery_grocery_eval_nb = eval("Naive Bayes", grocery_test_label, grocery_predict_nb)<jupyter_output>Naive Bayes acc: 0.820123 prec: 0.706058 rec: 0.811806 f1: 0.732736<jupyter_text>Logistic Regression<jupyter_code># Logistic regression
logregression = LogisticRegression()
logregression.fit(grocery_train_features, grocery_train_label)
grocery_predict_lr = logregression.predict(grocery_test_features)
cm_lr = confusion_matrix(grocery_test_label,grocery_predict_lr)
plot_confusion_matrix(cm_lr, classes=logregression.classes_.tolist(),
                      title='Logistic Regression(Grocery train, grocery test)')
plt.savefig('./plots/grocery_grocery_lr.png')
grocery_grocery_eval_lr = eval("Logistic Regerssion", grocery_test_label, grocery_predict_lr)<jupyter_output>Logistic Regerssion acc: 0.875695 prec: 0.765493 rec: 0.842349 f1: 0.794246<jupyter_text>Watches  Naive Bayes<jupyter_code># Naive Bayes
naivebayes = MultinomialNB()
naivebayes.fit(watches_train_features, watches_train_label)
watches_predict_nb = naivebayes.predict(watches_test_features)
cm_nb = confusion_matrix(watches_test_label,watches_predict_nb)
plot_confusion_matrix(cm_nb, classes=naivebayes.classes_.tolist(),
                      title='Naive Bayes(Watches train, watches test)')
plt.savefig('./plots/watches_watches_nb.png')
watches_watches_eval_nb = eval("Naive Bayes", watches_test_label, watches_predict_nb)<jupyter_output>Naive Bayes acc: 0.863071 prec: 0.761104 rec: 0.883715 f1: 0.796264<jupyter_text>Logistic Regression<jupyter_code># Logistic regression
logregression = LogisticRegression()
logregression.fit(watches_train_features, watches_train_label)
watches_predict_lr = logregression.predict(watches_test_features)
cm_lr = confusion_matrix(watches_test_label,watches_predict_lr)
plot_confusion_matrix(cm_lr, classes=logregression.classes_.tolist(),
                      title='Logistic Regression(Watches train, watches test)')
plt.savefig('./plots/watches_watches_lr.png')
watches_watches_eval_lr = eval("Logistic Regerssion", watches_test_label, watches_predict_lr)<jupyter_output>Logistic Regerssion acc: 0.911921 prec: 0.821787 rec: 0.914014 f1: 0.856779<jupyter_text>Video games  Naive Bayes<jupyter_code># Naive Bayes
naivebayes = MultinomialNB()
naivebayes.fit(videogames_train_features, videogames_train_label)
videogames_predict_nb = naivebayes.predict(videogames_test_features)
cm_nb = confusion_matrix(videogames_test_label,videogames_predict_nb)
plot_confusion_matrix(cm_nb, classes=naivebayes.classes_.tolist(),
                      title='Naive Bayes(Videogames train, videogames test)')
plt.savefig('./plots/videogames_videogames_nb.png')
videogames_videogames_eval_nb = eval("Naive Bayes", videogames_test_label, videogames_predict_nb)<jupyter_output>Naive Bayes acc: 0.783885 prec: 0.715063 rec: 0.840108 f1: 0.728851<jupyter_text>Logistic Regression<jupyter_code># Logistic regression
logregression = LogisticRegression()
logregression.fit(videogames_train_features, videogames_train_label)
videogames_predict_lr = logregression.predict(videogames_test_features)
cm_lr = confusion_matrix(videogames_test_label,videogames_predict_lr)
plot_confusion_matrix(cm_lr, classes=logregression.classes_.tolist(),
                      title='Logistic Regression(Videogames train,videogames test)')
plt.savefig('./plots/videogames_videogames_lr.png')
videogames_videogames_eval_lr = eval("Logistic Regerssion", videogames_test_label, videogames_predict_lr)<jupyter_output>Logistic Regerssion acc: 0.890790 prec: 0.807568 rec: 0.893685 f1: 0.838754
StarcoderdataJupyterStructuredCleanDedup