text_plain_output_1.png
from pandas import DataFrame

from sklearn import neighbors

from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer

from sklearn.metrics import classification_report

import numpy as np

import pandas as pd

import re

data_meta = pd.read_csv('/kaggle/input/content-based/meta_full_csv')
data_meta = data_meta.sample(50000)
data_meta['summaryRev'] = data_meta['summary'] + ' ' + data_meta['reviewText']

data_meta.query("asin == 'B00GIDADP0'")

data_meta.query("asin == 'B002Q46RDW'")

data_meta_1 = data_meta[['asin', 'overall', 'unixReviewTime']]
data_meta_1
data_meta_2 = data_meta[['asin', 'overall', 'summaryRev']]

data_meta_2
regEx = re.compile('[^a-z]+')
def cleanReviews(reviewText):
    reviewText = reviewText.lower()
    reviewText = regEx.sub(' ', reviewText).strip()
    return reviewText
data_meta_2['summaryClean'] = data_meta_2['summaryRev'].apply(cleanReviews)
data_meta_2 = data_meta_2.reset_index()

reviews = data_meta_2['summaryClean']
countVector = CountVectorizer(max_features=300, stop_words='english')
transformedReviews = countVector.fit_transform(reviews)
dfReviews = DataFrame(transformedReviews.A, columns=countVector.get_feature_names())
dfReviews = dfReviews.astype(int)

X = np.array(dfReviews)
tpercent = 0.9
tsize = int(np.floor(tpercent * len(dfReviews)))
dfReviews_train = X[:tsize]
dfReviews_test = X[tsize:]
lentrain = len(dfReviews_train)
lentest = len(dfReviews_test)

df5_train_target = data_meta_2['overall'][:lentrain]
df5_test_target = data_meta_2['overall'][lentrain:lentrain + lentest]
df5_train_target = df5_train_target.astype(int)
df5_test_target = df5_test_target.astype(int)
n_neighbors = 3
knnclf = neighbors.KNeighborsClassifier(n_neighbors, weights='distance')
knnclf.fit(dfReviews_train, df5_train_target)
knnpreds_test = knnclf.predict(dfReviews_test)

df5_train_target = data_meta_2['overall'][:lentrain]
df5_test_target = data_meta_2['overall'][lentrain:lentrain + lentest]
df5_train_target = df5_train_target.astype(int)
df5_test_target = df5_test_target.astype(int)
n_neighbors = 28
knnclf = neighbors.KNeighborsClassifier(n_neighbors, weights='distance')
knnclf.fit(dfReviews_train, df5_train_target)
knnpreds_test = knnclf.predict(dfReviews_test)

X = np.array(dfReviews)
tpercent = 0.85
tsize = int(np.floor(tpercent * len(dfReviews)))
dfReviews_train = X[:tsize]
dfReviews_test = X[tsize:]
lentrain = len(dfReviews_train)
lentest = len(dfReviews_test)

df5_train_target = data_meta_2['overall'][:lentrain]
df5_test_target = data_meta_2['overall'][lentrain:lentrain + lentest]
df5_train_target = df5_train_target.astype(int)
df5_test_target = df5_test_target.astype(int)
n_neighbors = 7
knnclf = neighbors.KNeighborsClassifier(n_neighbors, weights='distance')
knnclf.fit(dfReviews_train, df5_train_target)
knnpreds_test = knnclf.predict(dfReviews_test)
print(classification_report(df5_test_target, knnpreds_test))