forked from TenninYan/quantum-mnist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze_reduced.py
142 lines (129 loc) · 3.88 KB
/
analyze_reduced.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
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pickle
import numpy as np
from sklearn.tree import DecisionTreeClassifier
import itertools
train_fname = 'train_pca_reservoir_output_concatenated_samples0thru600_FIXED_LABELS.pickle'
# train_fname = 'train_pca_reservoir_output_concatenated_samples0thru600_reducedLocal_6_7_8_9_FIXED_LABELS.pickle'
# train_fname = 'train_pca_reservoir_output_concatenated_samples0thru600_reducedNonLocal_0_4_8_12_FIXED_LABELS.pickle'
with open(
train_fname,
'rb'
) as infile:
train = pickle.load(infile)
test_fname = 'test_pca_reservoir_output_concatenated_samples0thru300_FIXED_LABELS.pickle'
# test_fname = 'test_pca_reservoir_output_concatenated_samples0thru300_reducedLocal_6_7_8_9_FIXED_LABELS.pickle'
# test_fname = 'test_pca_reservoir_output_concatenated_samples0thru300_reducedNonLocal_0_4_8_12_FIXED_LABELS.pickle'
with open(
test_fname,
'rb'
) as infile:
test = pickle.load(infile)
X = train[0]
Y = train[1]
from sklearn import linear_model
from sklearn.tree import DecisionTreeClassifier
all_scores = []
cc = 0
for hyperparameters in itertools.product(
[6, 8, 10, 12, 15, 20] + [None],
list(range(2, 5)) + [.01, .5, .8],
list(range(1, 5)) + [.01, .2, .4],
['sqrt', None, 1, 2, 3],
[1234, 1012, 9999]
):
if np.random.random() > 0.5:
continue
hyperparameters = dict(zip(
[
'max_depth',
'min_samples_split',
'min_samples_leaf',
'max_features',
'random_state'
],
hyperparameters
))
print(hyperparameters)
clf = DecisionTreeClassifier(**hyperparameters)
# clf = linear_model.SGDClassifier()
clf.fit(X, Y)
# from sklearn.feature_selection import RFE
# selector = RFE(clf, 1000, step=.1)
# selector.fit(X, Y)
train_acc = sum([
int(guess==cor)
for guess, cor in zip(clf.predict(X), Y)
]) / float(len(X))
print('train accuracy: %s' % train_acc)
X_t = test[0]
Y_t = test[1]
test_acc = sum([
int(guess==cor)
for guess, cor in zip(clf.predict(X_t), Y_t)
]) / float(len(X))
print('test accuracy: %s' % test_acc)
all_scores.append((test_acc, train_acc, hyperparameters))
cc += 1
with open(
'treeClassifierAllScoresHyperparameterSearch.pickle',
'wb'
) as outfile:
pickle.dump(all_scores, outfile)
#
# for k, fname in enumerate([
# 'test_pca_reservoir_output_concatenated_samples0thru300_FIXED_LABELS.pickle',
# 'test_pca_reservoir_output_concatenated_samples0thru300_reducedLocal_6_7_8_9_FIXED_LABELS.pickle',
# 'test_pca_reservoir_output_concatenated_samples0thru300_reducedNonlocal_0_4_8_12_FIXED_LABELS.pickle'
# ]):
# with open(
# fname,
# 'rb'
# ) as infile:
# train = pickle.load(infile)
#
#
# pop0 = np.array([
# sample
# for i, sample in enumerate(train[0])
# if train[1][i] == 0
# ])
# pop1 = np.array([
# sample
# for i, sample in enumerate(train[0])
# if train[1][i] == 1
# ])
#
# np.mean(pop0, 0)
# np.mean(pop1, 0)
#
# fi0 = 0
# fi1 = 3
#
# plt.scatter(
# [_[fi0] for _ in pop0], [_[fi1] for _ in pop0],
# color='blue', alpha=.4
# )
# plt.scatter(
# [_[fi0] for _ in pop1], [_[fi1] for _ in pop1],
# color='red', alpha=.4
# )
# plt.xlim(
# (
# np.min([_[fi0] for _ in pop0] + [_[fi0] for _ in pop1]),
# np.max([_[fi0] for _ in pop0] + [_[fi0] for _ in pop1])
# )
# )
# plt.ylim(
# (
# np.min([_[fi1] for _ in pop0] + [_[fi1] for _ in pop1]),
# np.max([_[fi1] for _ in pop0] + [_[fi1] for _ in pop1])
# )
# )
# plt.title(fname)
# plt.savefig(
# 'figures/visualize_digits_separable_postreservoir_%s.png' % k,
# dpi=500
# )