forked from dalchen/EECS545FinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmazonPlot.py
135 lines (112 loc) · 5.48 KB
/
AmazonPlot.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
#!/usr/bin/env python3
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from matplotlib import pyplot as plt
from sklearn.datasets import fetch_20newsgroups_vectorized
from pprint import pprint
import json
import gzip
import pandas as pd
import gzip
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.feature_extraction.text import TfidfVectorizer
from scipy.sparse import coo_matrix, vstack
import numpy as np
from Sampling import Sampler
from RandomSampling import RandomSampler
from MarginSampling import MarginSampler
from HierarchicalSampler import HierarchicalSampler
class amazonDataset():
def parse(path):
g = gzip.open(path, 'r')
for l in g:
yield eval(l)
def getDF(path):
i = 0
df = {}
for d in amazonDataset.parse(path):
df[i] = d
i += 1
''' Return python dataframe of the data '''
return pd.DataFrame.from_dict(df, orient='index')
#This function takes in training and test data, calculates the logistic regression function, predicts test data, and returns the error
def calculateError(x_train, y_train, x_test, y_test, lambda_value):
clf = LogisticRegression(random_state=0, solver='lbfgs', C=1/lambda_value, multi_class='multinomial').fit(x_train, y_train)
y_test_predict = clf.predict(x_test)
error = 1 - accuracy_score(y_test_predict, y_test)
return error
def Plotting(training_size, max_unlabeled_size, x_test, y_test, x_train_random, y_train_random, x_train_margin, y_train_margin, x_train_Hierarchical, y_train_Hierarchical, lambda_value):
# Initialize vectors to be used for plotting
error_random_vector = []
error_margin_vector = []
error_Hierarchical_vector = []
num_samples_vector = []
# Iterating through number of samples, and adding the resulting errors to plotting vectors
i = training_size + 1
for num_samples in range(training_size, training_size + max_unlabeled_size):
# Each iteration you are using more labeled data points to train
num_samples_vector.append(num_samples + 1)
error_random_vector.append(calculateError(x_train_random[:num_samples, :], y_train_random[:num_samples], x_test, y_test, lambda_value))
error_margin_vector.append(calculateError(x_train_margin[:num_samples, :], y_train_margin[:num_samples], x_test, y_test, lambda_value))
error_Hierarchical_vector.append(calculateError(x_train_Hierarchical[:num_samples, :], y_train_Hierarchical[:num_samples], x_test, y_test, lambda_value))
print(i)
i = i + 1
# Plotting
data_set_title = "Amazon"
plt.gca().set_color_cycle(['red', 'green', 'blue'])
plt.plot(num_samples_vector, error_random_vector)
plt.plot(num_samples_vector, error_margin_vector)
plt.plot(num_samples_vector, error_Hierarchical_vector)
plt.legend(['Random', 'Margin', 'Hierarchical'], loc='upper right')
plt.xlabel("Number Of Labels")
plt.ylabel("Error")
# plt.show()
plt.savefig(data_set_title+str("_")+str(lambda_value)+".jpg")
if __name__ == '__main__':
'''
We can import this file safely into other files and use RandomSampler.
This driver in this section is just for when you run "python3 RandomSampling.py"
'''
print("Start")
training_size = 100#100
max_unlabeled_size = 400#400#20% of the data maybe
# lambda_value = 10**(-4)#This needs to be tuned
########################Amazon
# load the amazon dataset
amazon = amazonDataset.getDF('reviews_Musical_Instruments_5.json.gz')
tfidf = TfidfVectorizer()
X = tfidf.fit_transform(amazon.reviewText)
# split train and test data #overall refers to the ratings
x_train_base, X_test, y_train_base, y_test = train_test_split(X, amazon.overall, random_state=0, train_size = 2000)
y_test = np.array(y_test)
y_train_base = np.array(y_train_base)
print('Successfully loaded the Amazon dataset into train and test set.')
print(x_train_base.size)
X_train, y_train = x_train_base[:training_size], y_train_base[:training_size]
X_unlabeled, y_unlabeled = x_train_base[training_size:], y_train_base[training_size:]
rs = RandomSampler(X_train, y_train, X_unlabeled, y_unlabeled)
ms = MarginSampler(X_train, y_train, X_unlabeled, y_unlabeled)
hs = HierarchicalSampler(X_train, y_train, X_unlabeled, y_unlabeled)
x_train_random = X_train
y_train_random = y_train
x_train_margin = X_train
y_train_margin = y_train
x_train_Hierarchical = X_train
y_train_Hierarchical = y_train
for num_samples in range(max_unlabeled_size):
# Add data, random
x_sample, y_sample = rs.sample()
x_train_random = vstack([x_train_random, x_sample]).toarray()
y_train_random = np.append(y_train_random, y_sample)
# Add data, margin
x_sample, y_sample = ms.sample()
x_train_margin = vstack([x_train_margin, x_sample]).toarray()
y_train_margin = np.append(y_train_margin, y_sample)
# Add data, Hierarchical
x_sample, y_sample = hs.sample()
x_train_Hierarchical = vstack([x_train_Hierarchical, x_sample]).toarray()
y_train_Hierarchical = np.append(y_train_Hierarchical, y_sample)
for i in range(-8,9):
lambda_value = 10**(i)
print("Lambda = " + str(lambda_value))
Plotting(training_size, max_unlabeled_size, X_test, y_test, x_train_random, y_train_random, x_train_margin, y_train_margin, x_train_Hierarchical, y_train_Hierarchical, lambda_value)