<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
import string

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.metrics import accuracy_score
from sklearn.neural_network import MLPClassifier


%matplotlib inline
%config InlineBackend.figure_format='retina'
yelp_df = pd.read_csv('https://raw.githubusercontent.com/rabin1323/Data_Set/main/yelp_review_csv1.csv')
yelp_df<jupyter_output><empty_output><jupyter_text>**EXPLORING DATASET**<jupyter_code>yelp_df.info()
yelp_df.describe()
#to check if we have any null values
sns.heatmap(yelp_df.isnull(), yticklabels=False, cbar= False, cmap="Blues")
#if there is any dot on the plot that means we have null values
yelp_df=yelp_df.drop(['useful','funny','cool','review_id','user_id','business_id','date'], axis=1)
yelp_df
#data cleaning
def preprocess(review_text):
  remove_pctn=[char for char in review_text if char not in string.punctuation]
  remove_pctn= ''.join(remove_pctn)
  lwr = [word.lower() for word in remove_pctn.split()]
  final_word = [word for word in lwr if word not in stopwords.words('english')]
  return final_word<jupyter_output><empty_output><jupyter_text>For the sentiment analsysis we want to consider only two types of stars here i.e, one star for negative reviews and fives stars for positive reviews.We will also use count vectorizer to make a model which will be used to understand the review text. After that we will transform the vectorized text and assign to variable x. Lastly, we will split the entire data to train and test model using train_test_split()<jupyter_code>#Filtering Data
filtered_data = yelp_df[(yelp_df['stars']==1) | (yelp_df['stars']==5)]
x = filtered_data['text'] #assigning review text to variable x
y=filtered_data['stars'] #assigning stars to variable y
vectorizer=CountVectorizer(analyzer=preprocess).fit(x)
x=vectorizer.transform(x) #transforming the vectorized text 
X_train, X_test, y_train, y_test= train_test_split(x, y, random_state=42)
sns.countplot(filtered_data['stars'])
model= MLPClassifier()
model.fit(X_train, y_train)
y_predict = model.predict(X_test)
#plotting the reviews using confusion matrix
def conf_matrix(y, y_predict, reviews, title= 'Confusion_Matrix'):
  c_matrix = confusion_matrix(y, y_predict)
  clsfn_report = classification_report(y, y_predict)
  ticks = np.arange(len(reviews))
  score = accuracy_score(y, y_predict)
  score=round(score*100,2)
  print("Accuracy_score:", score)
  print('classification_report', clsfn_report)
  sns.heatmap(c_matrix, cmap= 'PuBu', annot= True, fmt='g', annot_kws={'size':20})
  plt.xticks(ticks, reviews)
  plt.yticks(ticks, reviews)
  plt.xlabel('predicted', fontsize=20)
  plt.ylabel('actual', fontsize=20)
  plt.title(title, fontsize=20)
  plt.show
conf_matrix(y_test, y_predict, reviews=['negative(1)', 'positive(5'])<jupyter_output>Accuracy_score: 93.86
classification_report               precision    recall  f1-score   support

           1       0.94      0.76      0.84       234
           5       0.94      0.99      0.96       873

    accuracy                           0.94      1107
   macro avg       0.94      0.87      0.90      1107
weighted avg       0.94      0.94      0.94      1107
StarcoderdataJupyterStructuredCleanDedup