forked from dalchen/EECS545FinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check1.py
executable file
·192 lines (164 loc) · 7.67 KB
/
check1.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/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):
# Initialize parameters and total number of labeled points
lambda_value = 10 ** (-4) # This needs to be tuned
# 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
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()
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 = 2
max_unlabeled_size = 5
#Newsgroup Data
train_dataset = fetch_20newsgroups_vectorized(subset='train')
X_train_base = train_dataset.data
y_train_base = train_dataset.target
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:]
test_dataset = fetch_20newsgroups_vectorized(subset='test')
X_test = test_dataset.data
y_test = test_dataset.target
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
print('Successfully loaded the Newsgroups dataset into train and test set.')
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)
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)
#
# ########################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)
# 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.')
#
# 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)
# for num_samples in range(max_unlabeled_size):
# x_sample, y_sample = rs.sample()
# X_train = vstack([X_train,x_sample]).toarray()
# y_train = np.append(y_train, y_sample)
#
#
# Plotting(training_size, max_unlabeled_size, X_train, y_train, X_test, y_test)
#
#
#
#
# ##################Movie data set
# docs = []
# for category in movie_reviews.categories():
# for fileid in movie_reviews.fileids(category):
# docs.append((movie_reviews.words(fileid), category))
# random.shuffle(docs)
# stop = stopwords.words('english') + list(string.punctuation)
# lemmatizer = WordNetLemmatizer()
#
# modified_docs = [(movieDataset.clean(docs), category) for docs, category in docs]
# text_docs = [" ".join(docs) for docs, category in modified_docs]
# category = [category for docs, category in modified_docs]
# tfidf = TfidfVectorizer()
# text_docs = tfidf.fit_transform(text_docs)
# x_train_base, x_test, y_train_base, y_test = train_test_split(text_docs, category, random_state=0)
# y_test = np.array(y_test)
# y_train_base = np.array(y_train_base)
# print('Successfully loaded the Movies dataset into train and test set.')
#
# 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)
# for num_samples in range(max_unlabeled_size):
# x_sample, y_sample = rs.sample()
# X_train = vstack([X_train,x_sample]).toarray()
# y_train = np.append(y_train, y_sample)
#
#
# Plotting(training_size, max_unlabeled_size, X_train, y_train, X_test, y_test)
#
print("Done")