<jupyter_start><jupyter_text>Sentiment Analysis on Women's Dress Review

* Tutorial: https://towardsai.net/p/nlp/sentiment-analysis-opinion-mining-with-python-nlp-tutorial-d1f173ca4e3c

* Github: https://github.com/towardsai/tutorials/tree/master/sentyment_analysis_tutorial **Download Dataset**<jupyter_code>!wget https://raw.githubusercontent.com/towardsai/tutorials/master/sentiment_analysis_tutorial/women_clothing_review.csv<jupyter_output>--2020-12-09 22:10:49--  https://raw.githubusercontent.com/towardsai/tutorials/master/sentiment_analysis_tutorial/women_clothing_review.csv
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 172.16.58.3, 192.168.3.11, 192.168.3.11, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|172.16.58.3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8480236 (8.1M) [text/plain]
Saving to: ‘women_clothing_review.csv’

women_clothing_revi 100%[===================>]   8.09M  25.7MB/s    in 0.3s    

2020-12-09 22:10:50 (25.7 MB/s) - ‘women_clothing_review.csv’ saved [8480236/8480236]<jupyter_text>**Import All Required Packages**<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import re
import string
from string import punctuation
import nltk
from nltk.corpus import stopwords
nltk.download('stopwords')


import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Dropout
from tensorflow.keras.callbacks import EarlyStopping<jupyter_output>[nltk_data] Downloading package stopwords to /root/nltk_data...
[nltk_data]   Package stopwords is already up-to-date!<jupyter_text>**Read data from csv**<jupyter_code>df = pd.read_csv('women_clothing_review.csv')
df.head()<jupyter_output><empty_output><jupyter_text>**Drop unnecessary columns**<jupyter_code>df = df.drop(['Title', 'Positive Feedback Count', 'Unnamed: 0', ], axis=1)
df.dropna(inplace=True)<jupyter_output><empty_output><jupyter_text>**Calculation of Polarity**<jupyter_code>df['Polarity_Rating'] = df['Rating'].apply(lambda x: 'Positive' if x > 3 else('Neutral' if x == 3  else 'Negative'))
df.head()<jupyter_output><empty_output><jupyter_text>**Plot the Rating visualization graph**<jupyter_code>sns.set_style('whitegrid')
sns.countplot(x='Rating',data=df, palette='YlGnBu_r')<jupyter_output><empty_output><jupyter_text>**Plot the Polarity Rating count**<jupyter_code>sns.set_style('whitegrid')
sns.countplot(x='Polarity_Rating',data=df, palette='summer')<jupyter_output><empty_output><jupyter_text>**Data Preprocessing**<jupyter_code>df_Positive = df[df['Polarity_Rating'] == 'Positive'][0:8000]
df_Neutral = df[df['Polarity_Rating'] == 'Neutral']
df_Negative = df[df['Polarity_Rating'] == 'Negative']<jupyter_output><empty_output><jupyter_text>**Sample negative and neutral polarity dataset and create final dataframe**<jupyter_code>df_Neutral_over = df_Neutral.sample(8000, replace=True)
df_Negative_over = df_Negative.sample(8000, replace=True)
df = pd.concat([df_Positive, df_Neutral_over, df_Negative_over], axis=0)<jupyter_output><empty_output><jupyter_text>**Text Preprocessing**<jupyter_code>def get_text_processing(text):
    stpword = stopwords.words('english')
    no_punctuation = [char for char in text if char not in string.punctuation]
    no_punctuation = ''.join(no_punctuation)
    return ' '.join([word for word in no_punctuation.split() if word.lower() not in stpword])<jupyter_output><empty_output><jupyter_text>**Apply the method "get_text_processing" into column review text**<jupyter_code>df['review'] = df['Review Text'].apply(get_text_processing)
df.head()<jupyter_output><empty_output><jupyter_text>**Visualize Text Review with Polarity Rating**<jupyter_code>df = df[['review', 'Polarity_Rating']]
df.head()<jupyter_output><empty_output><jupyter_text>**Apply One hot encoding on negative, neutral, and positive**<jupyter_code>one_hot = pd.get_dummies(df["Polarity_Rating"])
df.drop(['Polarity_Rating'],axis=1,inplace=True)
df = pd.concat([df,one_hot],axis=1)
df.head()<jupyter_output><empty_output><jupyter_text>**Apply Train Test Split**<jupyter_code>X = df['review'].values
y = df.drop('review', axis=1).values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=42)<jupyter_output><empty_output><jupyter_text>**Apply vectorization**<jupyter_code>vect = CountVectorizer()
X_train = vect.fit_transform(X_train)
X_test = vect.transform(X_test)<jupyter_output><empty_output><jupyter_text>**Apply frequency, inverse document frequency:**<jupyter_code>tfidf = TfidfTransformer()
X_train = tfidf.fit_transform(X_train)
X_test = tfidf.transform(X_test)
X_train = X_train.toarray()
X_test = X_test.toarray()<jupyter_output><empty_output><jupyter_text>**Add different layers**<jupyter_code>model = Sequential()

model.add(Dense(units=12673,activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(units=4000,activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(units=500,activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(units=3, activation='softmax'))

opt=tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=2)<jupyter_output><empty_output><jupyter_text>**Fit the Model**<jupyter_code>model.fit(x=X_train, y=y_train, batch_size=256, epochs=100, validation_data=(X_test, y_test), verbose=1, callbacks=early_stop)<jupyter_output>Epoch 1/100
58/66 [=========================>....] - ETA: 41s - loss: 0.6450 - accuracy: 0.7124<jupyter_text>**Evaluation of Model**<jupyter_code>model_score = model.evaluate(X_test, y_test, batch_size=64, verbose=1)
print('Test accuracy:', model_score[1])<jupyter_output><empty_output><jupyter_text>**Prediction**<jupyter_code>preds = model.predict(X_test)
preds<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14845663890249877, 'fraction_numerical': 0.03152049648864935, 'mean_word_length': 9.055829228243022, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 81, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '194150', 'license': 'MIT', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2243, 'n_words': 338, 'path': 'sentiment_analysis_tutorial/sentiment_analysis_tutorial.ipynb', 'repo_name': 'fimoziq/tutorials', 'size': '6132'}