text_plain_output_2.png text_plain_output_1.png image_output_2.png image_output_1.png
from imblearn.over_sampling import SMOTE

from sklearn import preprocessing

from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import classification_report

from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

from sklearn.naive_bayes import BernoulliNB

import matplotlib.pyplot as plt

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

data = pd.read_csv('/kaggle/input/review/reviews.csv')
label_encoder = preprocessing.LabelEncoder()
data['label'] = label_encoder.fit_transform(data['label'])
tfidf_vectorizer = TfidfVectorizer(max_features=5000, ngram_range=(2, 2))
X = tfidf_vectorizer.fit_transform(data['body'])
y = data['label']
smote = SMOTE(random_state=42)
X_res, y_res = smote.fit_resample(X, y)

logreg = LogisticRegression(solver='lbfgs', max_iter=1000, random_state=0)
naiveBayes = BernoulliNB()
logreg.fit(X_train, y_train)
clf1 = logreg.predict(X_test)
naiveBayes.fit(X_train, y_train)
clf2 = naiveBayes.predict(X_test)
len(clf1)
data['prediction'] = logreg.predict(X)
data.to_csv('logisticRegression.csv')
data['prediction'] = naiveBayes.predict(X)
data.to_csv('naiveBayes.csv')
print('Classification Report of Logistic Regression:\n', classification_report(y_test, clf1))
cm = confusion_matrix(y_test, clf1)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=['Negative', 'Neutral', 'Positive'])
disp.plot()
plt.show()
print('\n')
print('Classification Report of Naive Bayes:\n', classification_report(y_test, clf2))
cm = confusion_matrix(y_test, clf2)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=['Negative', 'Neutral', 'Positive'])
disp.plot()
plt.show()