-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
170 lines (146 loc) · 5.21 KB
/
main.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
import warnings
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.preprocessing import MinMaxScaler
from sklearn.utils import shuffle
from GN.KTSNE.ktsne import Ktsne
from GN.NN.NClassifier import NClassifier
from GN.NN.NRegressor import NRegressor
from GN.Plot.Plotter import Plotter
from GN.Threads.PThread import PThread
from GN.Threads.SetInterval import SetInterval
warnings.filterwarnings("ignore", category=RuntimeWarning)
class NetMain(object):
def __init__(self):
self.scaler = MinMaxScaler(feature_range=(-1, 1))
self.net = None
self.plotter = None
self.interval = None
self.datas = None
def getXY(self, dt="iris"):
if dt == "iris":
iris = datasets.load_iris()
X = iris.data
y = iris.target
else:
digits = datasets.load_digits()
X = digits.data
y = digits.target
return X, y
def ktsne(self, x, kernel="pca"):
f_opts = {'p_degree': 1.0, 'p_dims': 24, 'eta': 50.0,
'perplexity': 25.0, 'n_dims': 2, 'ker': kernel, 'gamma': 0.1}
k_tsne = Ktsne(x, f_opts=f_opts)
X_reduced = k_tsne.get_solution(3000)
X_reduced = self.scaler.fit_transform(X_reduced)
return X_reduced
def top_features(self, X, top=2):
ix = self.net.get_indexes(top)
print "=====Top features======"
print ix
row_ix = ix[0, :]
X2 = X[:, row_ix]
t = int(top/2)
X2_1 = X2[:, :t]
X2_2 = X2[:, t:]
X3 = np.hstack((X2_1.mean(1).reshape((-1, 1)),
X2_2.mean(1).reshape((-1, 1))))
return X3
def plotme(self, *largs):
gon = self.net.getLoading()
if not gon:
self.interval.stop()
_, Yh = self.net.getYhat(True, False)
print Yh, "=====PRED======"
print self.datas["nY"], "=====REAL======"
X, Yh = self.net.getYhat(False, False)
Y = self.datas["Y"]
if self.net.getMode() == "CLA" and X.shape[1] > 2:
X3 = self.top_features(X, top=2)
self.plotter.plot(X3, Y, Yh)
else:
self.plotter.plot(X, Y, Yh)
self.plotter.ioff()
else:
X, Yh = self.net.getYhat(False, False)
Y = self.datas["Y"]
if self.net.getMode() == "CLA" and X.shape[1] > 2:
X3 = self.top_features(X, top=2)
self.plotter.plot(X3, Y, Yh)
else:
self.plotter.plot(X, Y, Yh)
def classify(self):
nrOpts = {"opx": 1, "depth": 1, "nvars": None, "pvc": .6,
"pf": 1., "cross": .4, "mut": .5, "mrand": .5}
gOpts = {"mxepoch": 1500, "bsize": 192, "bupdate": 10, "fraction": .25,
"history": 5, "mxtries": 5, "mode": "CLA"}
print nrOpts
print gOpts
X, y = self.getXY("iris")
#X, y = self.getXY("digits")
# print i, o, "dim"
X, y = shuffle(X, y)
o = np.unique(y).size
X = X[:500]
y = y[:500]
X1 = self.scaler.fit_transform(X)
kernel = 'pca'
X_pca = PCA(n_components=2).fit_transform(X1)
#XX = X1
#XX = X_pca
XX = self.ktsne(X1, kernel)
Xtr = XX[:-10]
Xts = XX[-10:]
ytr = y[:-10]
yts = y[-10:]
i = Xtr.shape[1]
self.datas = {"X": Xtr, "Y": ytr, "nX": Xts, "nY": yts}
l_dims = [i, o]
self.net = NClassifier(dim=l_dims, datas=self.datas,
nr_opts=nrOpts, g_opts=gOpts)
self.net.setLoading(True)
self.plotter = Plotter(mode="CLA")
lf = PThread(target=self.net.train)
lf.start()
self.interval = SetInterval(5, self.plotme)
def reg(self):
nrOpts = {"opx": 3, "depth": 1, "nvars": None, "pvc": .6,
"pf": 1., "cross": .3, "mut": .5, "mrand": .5}
gOpts = {"mxepoch": 2500, "bsize": 192, "bupdate": 10, "fraction": .25,
"history": 5, "mxtries": 5, "mode": "REG"}
print nrOpts
print gOpts
# X = np.random.normal(size=100)
# XX=np.arange(0, 105, 1)
# XX = np.random.randint(low=-50, high=50, size=1000)
XX = np.linspace(-10., 11., num=100)
YY = (XX - 2) * np.cos(2 * XX)
# YY = XX**2 + XX - 1
# Make sure that it X is 2D
# N = 1000
# s = 10
# XX = s*np.random.rand(N)
# XX = np.sort(XX)
# YY = np.sin(XX) + 0.1*np.random.randn(N)
Y = YY[:-5]
nY = YY[-5:]
X = XX[:-5]
X = X[:, np.newaxis]
nX = XX[-5:]
nX = nX[:, np.newaxis]
self.datas = {"X": X, "Y": Y, "nX": nX, "nY": nY}
i = X.shape[1]
l_dims = [i, 1]
self.net = NRegressor(dim=l_dims, datas=self.datas,
nr_opts=nrOpts, g_opts=gOpts)
self.net.setLoading(True)
self.plotter = Plotter(mode="REG")
lf = PThread(target=self.net.train)
lf.start()
self.interval = SetInterval(5, self.plotme)
if __name__ == "__main__":
m = NetMain()
# m.reg()
m.classify()