-
Notifications
You must be signed in to change notification settings - Fork 87
/
testLearner.py
executable file
·151 lines (136 loc) · 3.27 KB
/
testLearner.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
#
# How to plot 2D data for learning
#
# by Tucker Balch
# September 2010
#
#
# imports
#
import numpy as np
import matplotlib.pyplot as plt
import qstklearn.kdtknn as kdt
from mpl_toolkits.mplot3d import Axes3D
from pylab import *
import datetime as dt
#
# Choose colors
#
def findcolors(Y):
miny = min(Y)
maxy = max(Y)
tY = (Y-miny)/(maxy-miny)
colors =[]
for i in tY:
if (i>0.66):
j = min(1,(i-.66)*3)
colors.append([1,(1-j),0])
elif (i<=.66) and (i>.33):
j = (i-.33)*3
colors.append([j,1,0])
else:
j = i*3
colors.append([0,j,1])
#print i,j
return colors
#
# read in and slice up the data
#
#data = np.loadtxt('data-classification-prob.csv',delimiter=',',skiprows=1)
data = np.loadtxt('data-ripple-prob.csv',delimiter=',',skiprows=1)
X1 = data[:,0]
X2 = data[:,1]
Y = data[:,2]
colors = findcolors(Y)
#
# scatter plot X1 vs X2 and colors are Y
#
plt.clf()
fig = plt.figure()
fig1 = fig.add_subplot(221)
plt.scatter(X1,X2,c=colors,edgecolors='none')
plt.xlabel('X1')
plt.ylabel('X2')
plt.xlim(-1,1) # set x scale
plt.ylim(-1,1) # set y scale
plt.title('Training Data 2D View',fontsize=12)
# plot the 3d view
ax = fig.add_subplot(222,projection='3d')
ax.scatter(X1,X2,Y,c=colors,edgecolors='none')
#ax.scatter(X1,X2,Y,c=colors)
ax.set_xlabel('X1')
ax.set_ylabel('X2')
ax.set_zlabel('Y')
ax.set_xlim3d(-1,1)
ax.set_ylim3d(-1,1)
ax.set_zlim3d(-1,1)
plt.title('Training Data 3D View',fontsize=12)
##########
# OK, now create and train a learner
#
learner = kdt.kdtknn(k=30,method='mean')
numpoints = X1.shape[0]
dataX = np.zeros([numpoints,2])
dataX[:,0] = X1
dataX[:,1] = X2
trainsize = floor(dataX.shape[0] * .6)
learner.addEvidence(dataX[0:trainsize],dataY=Y[0:trainsize])
steps = 50.0
stepsize = 2.0/steps
Xtest = np.zeros([steps*steps,2])
count = 0
for i in np.arange(-1,1,stepsize):
for j in np.arange(-1,1,stepsize):
Xtest[count,0] = i + stepsize/2
Xtest[count,1] = j + stepsize/2
count = count+1
Ytest = learner.query(Xtest) # to check every point
#
# Choose colors
#
colors = findcolors(Ytest)
#
# scatter plot X1 vs X2 and colors are Y
#
fig1 = fig.add_subplot(223)
plt.scatter(Xtest[:,0],Xtest[:,1],c=colors,edgecolors='none')
plt.xlabel('X1')
plt.ylabel('X2')
plt.xlim(-1,1) # set x scale
plt.ylim(-1,1) # set y scale
plt.title('Learned Model 2D',fontsize=12)
# plot the 3d view
ax = fig.add_subplot(224,projection='3d')
ax.scatter(Xtest[:,0],Xtest[:,1],Ytest,c=colors,edgecolors='none')
#X1 = Xtest[:,0]
#X2 = Xtest[:,1]
#X1 = np.reshape(X1,(steps,steps))
#X2 = np.reshape(X2,(steps,steps))
#Ytest = np.reshape(Ytest,(steps,steps))
ax.set_xlabel('X1')
ax.set_ylabel('X2')
ax.set_zlabel('Y')
ax.set_xlim3d(-1,1)
ax.set_ylim3d(-1,1)
ax.set_zlim3d(-1,1)
plt.title('Learned Model 3D',fontsize=12)
savefig("scatterdata3D.png", format='png')
plt.close()
#
# Compare to ground truth
#
print 'trainsize ' + str(trainsize)
Ytruth = Y[-trainsize:]
print 'Ytruth.shape ' + str(Ytruth.shape)
Xtest = dataX[-trainsize:,:]
print 'Xtest.shape ' + str(Xtest.shape)
Ytest = learner.query(Xtest) # to check every point
print 'Ytest.shape ' + str(Ytest.shape)
plt.clf()
plt.scatter(Ytruth,Ytest,edgecolors='none')
plt.xlim(-1.2,1.2) # set x scale
plt.ylim(-1.2,1.2) # set y scale
plt.xlabel('Ground Truth')
plt.ylabel('Estimated')
savefig("scatterdata.png", format='png')
print corrcoef(Ytruth,Ytest)