<jupyter_start><jupyter_text>In this kernel, We will make sentiment analysis using the reviews text.<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordCloud
#Importing dataset
df = pd.read_csv("../input/womens-ecommerce-clothing-reviews/Womens Clothing E-Commerce Reviews.csv", index_col=0)
df.head(10)
pd.DataFrame.info(df)<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 23486 entries, 0 to 23485
Data columns (total 10 columns):
Clothing ID                23486 non-null int64
Age                        23486 non-null int64
Title                      19676 non-null object
Review Text                22641 non-null object
Rating                     23486 non-null int64
Recommended IND            23486 non-null int64
Positive Feedback Count    23486 non-null int64
Division Name              23472 non-null object
Department Name            23472 non-null object
Class Name                 23472 non-null object
dtypes: int64(5), object(5)
memory usage: 2.0+ MB<jupyter_text>The dataset has 10 columns and 23486 rows. Some features has null values. Now we will extract the features that we will explore.<jupyter_code>reviewsDf = df[["Rating","Review Text"]]<jupyter_output><empty_output><jupyter_text>The updated dataset:<jupyter_code>reviewsDf.head()
pd.DataFrame.info(reviewsDf)<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 23486 entries, 0 to 23485
Data columns (total 2 columns):
Rating         23486 non-null int64
Review Text    22641 non-null object
dtypes: int64(1), object(1)
memory usage: 550.5+ KB<jupyter_text>Deleting rows with null values.<jupyter_code>reviewsDf = reviewsDf.dropna(subset=['Review Text'])<jupyter_output><empty_output><jupyter_text>Resetting index.<jupyter_code>reviewsDf.index = pd.Series(list(range(reviewsDf.shape[0])))
reviewsDf.head()<jupyter_output><empty_output><jupyter_text>Plotting the wordclouds.<jupyter_code>rev = reviewsDf['Review Text']

plt.subplots(figsize=(15,4))
wordcloud = WordCloud(background_color='white', width=900, height=300).generate(" ".join(rev))
plt.imshow(wordcloud)
plt.title('Words from Reviews\n',size=20)
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>We can see that "top", "love", "dress" and "shirt" is very used on the reviews. **Preprocessing our data.**<jupyter_code>#Removing stop words.
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
import nltk
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
!pip install nltk --upgrade
nltk.download('wordnet')

sw = set(stopwords.words('english'))

def preproc(data):
    #converting all to lowercase
    data = data.lower() 
    #Tokenize
    words = RegexpTokenizer(r'[a-z]+').tokenize(data)
    #Deleting stopwords
    words = [w for w in words if not w in sw]
    
    #Lemmatizing
    for pos in [wordnet.NOUN, wordnet.VERB, wordnet.ADJ, wordnet.ADV]:
        words = [WordNetLemmatizer().lemmatize(x, pos) for x in words]
    return " ".join(words)
reviewsDf['New Text'] = reviewsDf['Review Text'].apply(preproc)
reviewsDf.head()<jupyter_output><empty_output><jupyter_text>**Creating the sentiment classifier.**We will create a new column that will consider the ratings as follows:Rating 4 or higher: Positiverating 3: NeutralRating 2 or less: Negative<jupyter_code>def polarity (row):
  if row['Rating'] >= 4:
    return 'Positive'
  if row['Rating'] == 3:
    return 'Neutral'
  if row['Rating'] <= 2:
    return 'Negative'

reviewsDf['Class'] = reviewsDf.apply(lambda row: polarity(row), axis=1)
reviewsDf.head()<jupyter_output><empty_output><jupyter_text>Creating train and test base.<jupyter_code>text, classe = reviewsDf["New Text"], reviewsDf["Class"]
text
classe
train_text = text[:16980]
test_text = text[16981:22640]
train_classe = classe[:16980]
test_classe = classe[16981:22640]<jupyter_output><empty_output><jupyter_text>**Processing TD-IDF Matrix**<jupyter_code>#Feature extraction

from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer(encoding='latin-1')
X_train_counts = count_vect.fit_transform(train_text)
X_train_counts.shape
count_vect.vocabulary_.get('dress')
from sklearn.feature_extraction.text import TfidfTransformer
tfidf_transformer = TfidfTransformer(use_idf=True)
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
X_train_tfidf.shape<jupyter_output><empty_output><jupyter_text>Naive Bayes.<jupyter_code>from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB().fit(X_train_tfidf, train_classe)
from sklearn.metrics import accuracy_score
X_test_counts = count_vect.transform(test_text)
X_test_tfidf = tfidf_transformer.transform(X_test_counts)
predito = clf.predict(X_test_tfidf)
gaussian_acc = accuracy_score(test_classe, predito)
print(gaussian_acc)<jupyter_output>0.7752253048241738<jupyter_text>Random Forest.<jupyter_code>from sklearn.ensemble import RandomForestClassifier

ran = RandomForestClassifier(n_estimators=50)
ran.fit(X_train_tfidf, train_classe)
#Accuracy score
X_test_counts = count_vect.transform(test_text)
X_test_tfidf = tfidf_transformer.transform(X_test_counts)
predito = ran.predict(X_test_tfidf)
ran_acc = accuracy_score(test_classe, predito)
print(ran_acc)<jupyter_output>0.7865347234493727<jupyter_text>Support Vector Machine<jupyter_code>from sklearn.svm import SVC

svm = SVC()
svm.fit(X_train_tfidf, train_classe)
#Accuracy score

X_test_counts = count_vect.transform(test_text)
X_test_tfidf = tfidf_transformer.transform(X_test_counts)
predito = svm.predict(X_test_tfidf)
svm_acc = accuracy_score(test_classe, predito)
print(svm_acc)<jupyter_output>0.7734582081639866<jupyter_text>Neural network.<jupyter_code>from sklearn.neural_network import MLPClassifier

nn = MLPClassifier()
nn.fit(X_train_tfidf, train_classe)
#Accuracy score

X_test_counts = count_vect.transform(test_text)
X_test_tfidf = tfidf_transformer.transform(X_test_counts)
predito = nn.predict(X_test_tfidf)
nn_acc = accuracy_score(test_classe, predito)
print(nn_acc)<jupyter_output>0.7815868528008482<jupyter_text>Logistic Regression.<jupyter_code>from sklearn.linear_model import LogisticRegression

lr = LogisticRegression()
lr.fit(X_train_tfidf, train_classe)
#Accuracy score

X_test_counts = count_vect.transform(test_text)
X_test_tfidf = tfidf_transformer.transform(X_test_counts)
predito = lr.predict(X_test_tfidf)
lr_acc = accuracy_score(test_classe, predito)
print(lr_acc)<jupyter_output>0.813924721682276<jupyter_text>The ranking bellow show us that Logistic Regression was the most accurate model and Support Vector Machines was the worse.<jupyter_code>models = pd.DataFrame({
    'Model': ['Support Vector Machines', 'Logistic Regression', 
              'Random Forest', 'Naive Bayes', 'Neural Network'],
    'Score': [svm_acc, lr_acc, 
              ran_acc, gaussian_acc, nn_acc]})
models.sort_values(by='Score', ascending=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1020614098313392, 'fraction_numerical': 0.03762433328528182, 'mean_word_length': 6.204569055036345, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '258514', 'license': 'Unlicense', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2476, 'n_words': 500, 'path': 'AnalyticsNotebooks/e-commerce-predict-recommendation.ipynb', 'repo_name': 'shagilsiddiqui/MyntraHackathon', 'size': '6937'}