-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazon_analysis.py
135 lines (119 loc) · 4.68 KB
/
amazon_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import json
import logging
import pdb
import sys
from collections import defaultdict
from multiprocessing import Pool, Process, cpu_count
import numpy as np
import pandas as pd
from gensim.models import Word2Vec
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegressionCV
from sklearn.metrics.pairwise import cosine_similarity
from vec2pca import multitokenize, train
logging.basicConfig(format='%asctime)s %(levelname)s: %message)s',
level=logging.INFO, datefmt='%H:%M:%S')
infile = "reviews_Books_5_short.json"
if len(sys.argv) > 1:
infile = sys.argv[1]
print("loading in file")
counter = 0
asins = defaultdict(lambda: 0)
rtext = []
rratings = []
with open(infile) as f:
for review in f:
counter += 1
if counter % 100000 == 0:
print("processed %d reviews" % counter)
reviewjson = json.loads(review)
if asins[reviewjson['asin']] < 30:
rtext.append(reviewjson['reviewText'])
rratings.append(reviewjson['overall'])
asins[reviewjson['asin']] = asins[reviewjson['asin']] + 1
print("processed %d reviews, done." % counter)
def to_wordlist(corpus):
wordlists = []
for document in corpus:
wordlist = document
for splitchar in '.,;:/()&!?-_+"':
wordlist = ' '.join(wordlist.split(splitchar))
wordlist = [word for word in wordlist.split(" ") if word]
wordlists.append(wordlist)
return wordlists
print("building wordlist")
tokenized_reviews = multitokenize(rtext, processes=16)
print("train test split")
fullset = np.array(list(zip(tokenized_reviews, rratings)))
np.random.shuffle(fullset)
splitpos = int(len(fullset) * 0.8)
training, test = fullset[:splitpos, :], fullset[splitpos:, ]
for badnum in [a for a in range(len(training[:, 0])) if not training[a, 0]]:
training[badnum, 0] = [""]
for badnum in [a for a in range(len(test[:, 0])) if not test[a, 0]]:
test[badnum, 0] = [""]
print("training model")
model = Word2Vec.load("amazon_model")
prcomps = dict(zip(model.index2word, PCA().fit_transform(
pd.DataFrame(model[model.index2word]))))
# adapted from
# https://www.kaggle.com/c/word2vec-nlp-tutorial/details/part-3-more-fun-with-word-vectors
def make_feature_vec(words, model, prcomps, num_features):
feature_vec = np.zeros((num_features,), dtype="float32")
nwords = 0.
index2word_set = set(model.index2word)
for word in words:
if word in index2word_set:
nwords = nwords + 1.
feature_vec = np.add(feature_vec, prcomps[word])
feature_vec = np.divide(feature_vec, nwords)
return feature_vec
# adapted from
# https://www.kaggle.com/c/word2vec-nlp-tutorial/details/part-3-more-fun-with-word-vectors
def get_average_feature_vecs(reviews, model=model, prcomps=prcomps, num_features=100):
counter = 0
review_feature_vecs = np.zeros((len(reviews), num_features), dtype="float32")
for review in reviews:
review_feature_vecs[counter] = make_feature_vec(
review, model, prcomps, num_features)
counter += 1
return review_feature_vecs
print("starting multiprocess averaging")
def mapred(func, data, processes=cpu_count()):
def chunks(l, n):
n = int(len(l) / n)
for i in range(0, len(l), n):
yield l[i:i + n]
print("initializing pool")
pool = Pool(processes=processes)
print("partitioning input")
partitioned = chunks(data, processes * 100)
print("averaging")
mapped = pool.imap_unordered(func, partitioned)
aggregator = []
print("aggregating")
for section in mapped:
aggregator.extend(section)
return aggregator
train_data_vecs = mapred(get_average_feature_vecs, training[:, 0])
np.save('train_data_vecs', train_data_vecs)
for idx, row in enumerate(train_data_vecs):
if not np.isfinite(row).all():
train_data_vecs[idx] = np.zeros_like(train_data_vecs[0])
for idx, row in enumerate(test_data_vecs):
if not np.isfinite(row).all():
test_data_vecs[idx] = np.zeros_like(test_data_vecs[0])
np.save('train_data_vecs', train_data_vecs)
np.save('test_data_vecs', test_data_vecs)
np.save('test', test)
np.save('training', training)
print("fitting model...")
lm = LogisticRegressionCV(n_jobs=-1, max_iter=500, solver='sag')
lm_fit = lm.fit(train_data_vecs, list(training[:, 1]))
lm_preds = lm.predict(test_data_vecs)
print("score: ", lm_fit.score(test_data_vecs, list(test[:, 1])))
print("MSE: ", np.mean((lm_fit.predict(test_data_vecs) - test[:, 1] ** 2)))
df = pd.DataFrame(data={"text": list(map(" ".join, test[:, 0])),
"actual": test[:, 1],
"result": lm_preds}).sort_values("result")
print(df[['result', 'actual']].astype('int').corr())