<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive')
import pandas as pd
import numpy as np
from sklearn.metrics import classification_report,roc_auc_score,roc_curve,r2_score,recall_score,confusion_matrix,precision_recall_curve
from collections import Counter
from sklearn.model_selection import StratifiedKFold,KFold,StratifiedShuffleSplit
from nltk import word_tokenize
from nltk.corpus import stopwords
# stop_words = stopwords.words('english')
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA, TruncatedSVD,SparsePCA
from sklearn.metrics import classification_report,confusion_matrix
from nltk.tokenize import word_tokenize
from collections import defaultdict
from collections import Counter
import seaborn as sns
from wordcloud import WordCloud,STOPWORDS
import nltk
from nltk.corpus import stopwords
import string
from plotly import tools
import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
!pip install wandb
txt = pd.read_csv("/content/drive/Shareddrives/Team Stars/Data/df_clean.csv",index_col=0)
txt.head(2)
reviews = txt[['reviews_text', 'reviews_rating']]
reviews.head(2)
reviews.info()
rev = txt['reviews_text']
rev.head(1)<jupyter_output><empty_output><jupyter_text>It is not possible to compute the similarity between any two reviews in their raw forms. To do this, we'll need to compute the word vectors of each review whereby word vectors are vectorized representation of words in a the reviews.<jupyter_code>#Import TfIdfVectorizer from scikit-learn
from sklearn.feature_extraction.text import TfidfVectorizer

#Define a TF-IDF Vectorizer Object. Remove all english stop words such as 'the', 'a'
tfidf = TfidfVectorizer(stop_words='english')

#Replace NaN with an empty string
rev = rev.fillna('')

#Construct the required TF-IDF matrix by fitting and transforming the data
tfidf_matrix = tfidf.fit_transform(rev)

#Output the shape of tfidf_matrix
tfidf_matrix.shape
#Array mapping from feature integer indices to feature name.
tfidf.get_feature_names()[2000:2010]
# Import linear_kernel
from sklearn.metrics.pairwise import linear_kernel

# Compute the cosine similarity matrix
cosinesim = linear_kernel(tfidf_matrix, tfidf_matrix)
cosinesim.shape
cosinesim[1]
#Construct a reverse map of indices and review titles
indices = pd.Series(txt.index, index=txt['name']).drop_duplicates()
indices[:10]
# Function that takes in product as input and outputs most similar products
def get_recommendations(name, cosine_sim=cosinesim):
    # Get the index of the product that matches the title
    idx = indices

    # Get the pairwsie similarity scores of all items with that product
    sim_scores = list(enumerate(cosine_sim[idx]))

    # Sort the items based on the similarity scores
    sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)

    # Get the scores of the 10 most similar products
    sim_scores = sim_scores[1:11]

    # Get the product indices
    product_indices = [i[0] for i in sim_scores]

    # Return the top 10 most similar products
    return txt['name'].iloc[product_indices]

txt.head(1)
txt.name[1]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06922162804515745, 'fraction_numerical': 0.008021390374331552, 'mean_word_length': 6.17910447761194, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '449590', 'license': 'MIT', 'n_tokens_mistral': 1065, 'n_tokens_neox': 1019, 'n_words': 332, 'path': 'Content_based_filtering.ipynb', 'repo_name': 'vngeno/E-COMMERCE-RECOMMENDATION-SYSTEM-', 'size': '3366'}