Sentimen_analysis amazon_echo model

This is a Naive Bayes classifier trained on 32 thousand amazon echo reviews for sentiment analysis. This model is suitable for English.

Labels: 0 -> Negative; 1 -> Positive

Model training

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from jupyterthemes import jtplot
jtplot.style(theme='monokai', context='notebook', ticks=True, grid=False)

# Load the data
amazon_df = pd.read_csv('amazon_reviews.csv')

# Drop the unnecessary columns
amazon_df = amazon_df.drop(['date'], axis=1)
amazon_df = amazon_df.drop(['rating'], axis=1)
amazon_df = amazon_df.drop(['variation'], axis=1)

# Let's define a pipeline to clean up all the messages 
# The pipeline performs the following: (1) remove punctuation, (2) remove stopwords

def message_cleaning(message):
    Test_punc_removed = [char for char in message if char not in string.punctuation]
    Test_punc_removed_join = ''.join(Test_punc_removed)
    Test_punc_removed_join_clean = [word for word in Test_punc_removed_join.split() if word.lower() not in stopwords.words('english')]
    return Test_punc_removed_join_clean

# Let's test the newly added function
amazon_df_clean = amazon_df['verified_reviews'].apply(message_cleaning)

from sklearn.feature_extraction.text import CountVectorizer

# Define the cleaning pipeline we defined earlier
vectorizer = CountVectorizer(analyzer = message_cleaning, dtype = np.uint8)
amazon_countvectorizer = vectorizer.fit_transform(amazon_df['verified_reviews'])

X = pd.DataFrame(amazon_countvectorizer.toarray())

##TRAIN A NAIVE BAYES CLASSIFIER MODEL

from sklearn.model_selection import train_test_split

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2)

from sklearn.naive_bayes import MultinomialNB

NB_classifier = MultinomialNB()
NB_classifier.fit(X_train,y_train)


Evaluating the model


from sklearn.metrics import classification_report, confusion_matrix

# Predicting the Test set results
y_predict_test = NB_classifier.predict(X_test)
cm = confusion_matrix(y_test, y_predict_test)
sns.heatmap(cm, annot=True)

! Confusion_Matrix

print(classification_report(y_test, y_predict_test))

Output:

 precision    recall  f1-score   support

           0       0.70      0.49      0.58        57
           1       0.95      0.98      0.96       573

    accuracy                           0.93       630
   macro avg       0.83      0.74      0.77       630
weighted avg       0.93      0.93      0.93       630