Welcome, guest | Sign In | My Account | Store | Cart

A simple genetic algorithm neural network.

Python, 548 lines
  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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
from operator import itemgetter, attrgetter
import math
import random
import string
import timeit
from timeit import Timer as t
import matplotlib.pyplot as plt
import numpy as np

def sigmoid (x):
  return math.tanh(x)

def makeMatrix ( I, J, fill=0.0):
  m = []
  for i in range(I):
    m.append([fill]*J)
  return m
  
def randomizeMatrix ( matrix, a, b):
  for i in range ( len (matrix) ):
    for j in range ( len (matrix[0]) ):
      matrix[i][j] = random.uniform(a,b)

class NN:
  def __init__(self, NI, NH, NO):
    self.ni = NI
    self.nh = NH
    self.no = NO
    self.ai = [1.0]*self.ni
    self.ah = [1.0]*self.nh
    self.ao = [1.0]*self.no
    self.wi = [ [0.0]*self.nh for i in range(self.ni) ]
    self.wo = [ [0.0]*self.no for j in range(self.nh) ]
    randomizeMatrix ( self.wi, -0.2, 0.2 )
    randomizeMatrix ( self.wo, -2.0, 2.0 )

  def runNN (self, inputs):
    if len(inputs) != self.ni:
      print 'incorrect number of inputs'
    for i in range(self.ni):
      self.ai[i] = inputs[i]
    for j in range(self.nh):
      self.ah[j] = sigmoid(sum([ self.ai[i]*self.wi[i][j] for i in range(self.ni) ]))
    for k in range(self.no):
      self.ao[k] = sigmoid(sum([ self.ah[j]*self.wo[j][k] for j in range(self.nh) ]))
    return self.ao

  def weights(self):
    print 'Input weights:'
    for i in range(self.ni):
      print self.wi[i]
    print
    print 'Output weights:'
    for j in range(self.nh):
      print self.wo[j]
    print ''

  def test(self, patterns):
    results, targets = [], []
    for p in patterns:
      inputs = p[0]
      rounded = [ round(i) for i in self.runNN(inputs) ]
      if rounded == p[1]: result = '+++++'
      else: result = '-----'
      print '%s %s %s %s %s %s %s' %( 'Inputs:', p[0], '-->', str(self.runNN(inputs)).rjust(65), 'Target', p[1], result)
      results+= self.runNN(inputs)
      targets += p[1]
    return results, targets

  def sumErrors (self):
    error = 0.0
    for p in pat:
      inputs = p[0]
      targets = p[1]
      self.runNN(inputs)
      error += self.calcError(targets)
    inverr = 1.0/error
    return inverr

  def calcError (self, targets):
    error = 0.0
    for k in range(len(targets)):
      error += 0.5 * (targets[k]-self.ao[k])**2
    return error

  def assignWeights (self, weights, I):
    io = 0
    for i in range(self.ni):
      for j in range(self.nh):
        self.wi[i][j] = weights[I][io][i][j]
    io = 1
    for j in range(self.nh):
      for k in range(self.no):
        self.wo[j][k] = weights[I][io][j][k]

  def testWeights (self, weights, I):
    same = []
    io = 0
    for i in range(self.ni):
      for j in range(self.nh):
        if self.wi[i][j] != weights[I][io][i][j]:
          same.append(('I',i,j, round(self.wi[i][j],2),round(weights[I][io][i][j],2),round(self.wi[i][j] - weights[I][io][i][j],2)))

    io = 1
    for j in range(self.nh):
      for k in range(self.no):
        if self.wo[j][k] !=  weights[I][io][j][k]:
          same.append((('O',j,k), round(self.wo[j][k],2),round(weights[I][io][j][k],2),round(self.wo[j][k] - weights[I][io][j][k],2)))
    if same != []:
      print same

def roulette (fitnessScores):
  cumalativeFitness = 0.0
  r = random.random()
  for i in range(len(fitnessScores)): 
    cumalativeFitness += fitnessScores[i]
    if cumalativeFitness > r: 
      return i
      
def calcFit (numbers):  # each fitness is a fraction of the total error
  total, fitnesses = sum(numbers), []
  for i in range(len(numbers)):           
    fitnesses.append(numbers[i]/total)
  return fitnesses

# takes a population of NN objects
def pairPop (pop):
  weights, errors = [], []
  for i in range(len(pop)):                 # for each individual
    weights.append([pop[i].wi,pop[i].wo])   # append input & output weights of individual to list of all pop weights
    errors.append(pop[i].sumErrors())       # append 1/sum(MSEs) of individual to list of pop errors
  fitnesses = calcFit(errors)               # fitnesses are a fraction of the total error
  for i in range(int(pop_size*0.15)): 
    print str(i).zfill(2), '1/sum(MSEs)', str(errors[i]).rjust(15), str(int(errors[i]*graphical_error_scale)*'-').rjust(20), 'fitness'.rjust(12), str(fitnesses[i]).rjust(17), str(int(fitnesses[i]*1000)*'-').rjust(20)
  del pop
  return zip(weights, errors,fitnesses)            # weights become item[0] and fitnesses[1] in this way fitness is paired with its weight in a tuple
  
def rankPop (newpopW,pop):
  errors, copy = [], []           # a fresh pop of NN's are assigned to a list of len pop_size
  #pop = [NN(ni,nh,no)]*pop_size # this does not work as they are all copies of eachother
  pop = [NN(ni,nh,no) for i in range(pop_size) ]
  for i in range(pop_size): copy.append(newpopW[i])
  for i in range(pop_size):  
    pop[i].assignWeights(newpopW, i)                                    # each individual is assigned the weights generated from previous iteration
    pop[i].testWeights(newpopW, i)
  for i in range(pop_size):  
    pop[i].testWeights(newpopW, i)
  pairedPop = pairPop(pop)                                              # the fitness of these weights is calculated and tupled with the weights
  rankedPop = sorted(pairedPop, key = itemgetter(-1), reverse = True)   # weights are sorted in descending order of fitness (fittest first)
  errors = [ eval(repr(x[1])) for x in rankedPop ]
  return rankedPop, eval(repr(rankedPop[0][1])), float(sum(errors))/float(len(errors))

def iteratePop (rankedPop):
  rankedWeights = [ item[0] for item in rankedPop]
  fitnessScores = [ item[-1] for item in rankedPop]
  newpopW = [ eval(repr(x)) for x in rankedWeights[:int(pop_size*0.15)] ]
  while len(newpopW) <= pop_size:                                       # Breed two randomly selected but different chromos until pop_size reached
    ch1, ch2 = [], []
    index1 = roulette(fitnessScores)                                    
    index2 = roulette(fitnessScores)
    while index1 == index2:                                             # ensures different chromos are used for breeeding 
      index2 = roulette(fitnessScores)
    #index1, index2 = 3,4
    ch1.extend(eval(repr(rankedWeights[index1])))
    ch2.extend(eval(repr(rankedWeights[index2])))
    if random.random() < crossover_rate: 
      ch1, ch2 = crossover(ch1, ch2)
    mutate(ch1)
    mutate(ch2)
    newpopW.append(ch1)
    newpopW.append(ch2)
  return newpopW

graphical_error_scale = 100
max_iterations = 4000
pop_size = 100
mutation_rate = 0.1
crossover_rate = 0.8
ni, nh, no = 4,6,1

def main ():
  # Rank first random population
  pop = [ NN(ni,nh,no) for i in range(pop_size) ] # fresh pop
  pairedPop = pairPop(pop)
  rankedPop = sorted(pairedPop, key = itemgetter(-1), reverse = True) # THIS IS CORRECT
  # Keep iterating new pops until max_iterations
  iters = 0
  tops, avgs = [], []
  while iters != max_iterations:
    if iters%1 == 0:
      print 'Iteration'.rjust(150), iters
    newpopW = iteratePop(rankedPop)
    rankedPop, toperr, avgerr = rankPop(newpopW,pop)
    tops.append(toperr)
    avgs.append(avgerr)
    iters+=1
  
  # test a NN with the fittest weights
  tester = NN (ni,nh,no)
  fittestWeights = [ x[0] for x in rankedPop ]
  tester.assignWeights(fittestWeights, 0)
  results, targets = tester.test(testpat)
  x = np.arange(0,150)
  title2 = 'Test after '+str(iters)+' iterations'
  plt.title(title2)
  plt.ylabel('Node output')
  plt.xlabel('Instances')
  plt.plot( results, 'xr', linewidth = 0.5)
  plt.plot( targets, 's', color = 'black',linewidth = 3)
  #lines = plt.plot( results, 'sg')
  plt.annotate(s='Target Values', xy = (110, 0),color = 'black', family = 'sans-serif', size  ='small')
  plt.annotate(s='Test Values', xy = (110, 0.5),color = 'red', family = 'sans-serif', size  ='small', weight = 'bold')
  plt.figure(2)
  plt.subplot(121)
  plt.title('Top individual error evolution')
  plt.ylabel('Inverse error')
  plt.xlabel('Iterations')
  plt.plot( tops, '-g', linewidth = 1)
  plt.subplot(122)
  plt.plot( avgs, '-g', linewidth = 1)
  plt.title('Population average error evolution')
  plt.ylabel('Inverse error')
  plt.xlabel('Iterations')
  
  plt.show()
  
  print 'max_iterations',max_iterations,'\tpop_size',pop_size,'pop_size*0.15',int(pop_size*0.15),'\tmutation_rate',mutation_rate,'crossover_rate',crossover_rate,'ni, nh, no',ni, nh, no

def crossover (m1, m2):
  r = random.randint(0, (ni*nh)+(nh*no) ) # ni*nh+nh*no  = total weights
  output1 = [ [[0.0]*nh]*ni ,[[0.0]*no]*nh ]
  output2 = [ [[0.0]*nh]*ni ,[[0.0]*no]*nh ]
  for i in range(len(m1)):
    for j in range(len(m1[i])):
      for k in range(len(m1[i][j])):
        if r >= 0:
          output1[i][j][k] = m1[i][j][k]
          output2[i][j][k] = m2[i][j][k]
        elif r < 0:
          output1[i][j][k] = m2[i][j][k]
          output2[i][j][k] = m1[i][j][k]
        r -=1
  return output1, output2

def mutate (m):
  # could include a constant to control 
  # how much the weight is mutated by
  for i in range(len(m)):
    for j in range(len(m[i])):
      for k in range(len(m[i][j])):
        if random.random() < mutation_rate:
            m[i][j][k] = random.uniform(-2.0,2.0)
  
if __name__ == "__main__":
    main()
pat = [
  [[5.1, 3.5, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[4.9, 3.0, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[4.7, 3.2, 1.3, 0.2], [-1], ['Iris-setosa']] ,
  [[5.4, 3.9, 1.7, 0.4], [-1], ['Iris-setosa']] ,
  [[4.6, 3.4, 1.4, 0.3], [-1], ['Iris-setosa']] ,
  [[5.0, 3.4, 1.5, 0.2], [-1], ['Iris-setosa']] ,
  [[4.4, 2.9, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[4.9, 3.1, 1.5, 0.1], [-1], ['Iris-setosa']] ,
  [[5.4, 3.7, 1.5, 0.2], [-1], ['Iris-setosa']] ,
  [[4.8, 3.4, 1.6, 0.2], [-1], ['Iris-setosa']] ,
  [[4.8, 3.0, 1.4, 0.1], [-1], ['Iris-setosa']] ,
  [[4.3, 3.0, 1.1, 0.1], [-1], ['Iris-setosa']] ,
  [[5.8, 4.0, 1.2, 0.2], [-1], ['Iris-setosa']] ,
  [[5.7, 4.4, 1.5, 0.4], [-1], ['Iris-setosa']] ,
  [[5.4, 3.9, 1.3, 0.4], [-1], ['Iris-setosa']] ,
  [[5.1, 3.5, 1.4, 0.3], [-1], ['Iris-setosa']] ,
  [[5.7, 3.8, 1.7, 0.3], [-1], ['Iris-setosa']] ,
  [[5.1, 3.8, 1.5, 0.3], [-1], ['Iris-setosa']] ,
  [[5.4, 3.4, 1.7, 0.2], [-1], ['Iris-setosa']] ,
  [[5.1, 3.7, 1.5, 0.4], [-1], ['Iris-setosa']] ,
  [[4.6, 3.6, 1.0, 0.2], [-1], ['Iris-setosa']] ,
  [[5.1, 3.3, 1.7, 0.5], [-1], ['Iris-setosa']] ,
  [[4.8, 3.4, 1.9, 0.2], [-1], ['Iris-setosa']] ,
  [[5.0, 3.0, 1.6, 0.2], [-1], ['Iris-setosa']] ,
  [[5.0, 3.4, 1.6, 0.4], [-1], ['Iris-setosa']] ,
  [[5.2, 3.5, 1.5, 0.2], [-1], ['Iris-setosa']] ,
  [[5.2, 3.4, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[4.7, 3.2, 1.6, 0.2], [-1], ['Iris-setosa']] ,
  [[4.8, 3.1, 1.6, 0.2], [-1], ['Iris-setosa']] ,
  [[5.4, 3.4, 1.5, 0.4], [-1], ['Iris-setosa']] ,
  [[5.2, 4.1, 1.5, 0.1], [-1], ['Iris-setosa']] ,
  [[5.5, 4.2, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[4.9, 3.1, 1.5, 0.1], [-1], ['Iris-setosa']] ,
  [[5.0, 3.2, 1.2, 0.2], [-1], ['Iris-setosa']] ,
  [[5.5, 3.5, 1.3, 0.2], [-1], ['Iris-setosa']] ,
  [[4.9, 3.1, 1.5, 0.1], [-1], ['Iris-setosa']] ,
  [[4.4, 3.0, 1.3, 0.2], [-1], ['Iris-setosa']] ,
  [[5.1, 3.4, 1.5, 0.2], [-1], ['Iris-setosa']] ,
  [[5.0, 3.5, 1.3, 0.3], [-1], ['Iris-setosa']] ,
  [[4.5, 2.3, 1.3, 0.3], [-1], ['Iris-setosa']] ,
  [[4.4, 3.2, 1.3, 0.2], [-1], ['Iris-setosa']] ,
  [[5.0, 3.5, 1.6, 0.6], [-1], ['Iris-setosa']] ,
  [[5.1, 3.8, 1.9, 0.4], [-1], ['Iris-setosa']] ,
  [[4.8, 3.0, 1.4, 0.3], [-1], ['Iris-setosa']] ,
  [[5.1, 3.8, 1.6, 0.2], [-1], ['Iris-setosa']] ,
  [[4.6, 3.2, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[5.3, 3.7, 1.5, 0.2], [-1], ['Iris-setosa']] ,
  [[5.0, 3.3, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[7.0, 3.2, 4.7, 1.4], [0], ['Iris-versicolor']] ,
  [[6.4, 3.2, 4.5, 1.5], [0], ['Iris-versicolor']] ,
  [[6.9, 3.1, 4.9, 1.5], [0], ['Iris-versicolor']] ,
  [[5.5, 2.3, 4.0, 1.3], [0], ['Iris-versicolor']] ,
  [[6.5, 2.8, 4.6, 1.5], [0], ['Iris-versicolor']] ,
  [[5.7, 2.8, 4.5, 1.3], [0], ['Iris-versicolor']] ,
  [[6.3, 3.3, 4.7, 1.6], [0], ['Iris-versicolor']] ,
  [[4.9, 2.4, 3.3, 1.0], [0], ['Iris-versicolor']] ,
  [[6.6, 2.9, 4.6, 1.3], [0], ['Iris-versicolor']] ,
  [[5.2, 2.7, 3.9, 1.4], [0], ['Iris-versicolor']] ,
  [[5.0, 2.0, 3.5, 1.0], [0], ['Iris-versicolor']] ,
  [[5.9, 3.0, 4.2, 1.5], [0], ['Iris-versicolor']] ,
  [[6.0, 2.2, 4.0, 1.0], [0], ['Iris-versicolor']] ,
  [[6.1, 2.9, 4.7, 1.4], [0], ['Iris-versicolor']] ,
  [[5.6, 2.9, 3.6, 1.3], [0], ['Iris-versicolor']] ,
  [[6.7, 3.1, 4.4, 1.4], [0], ['Iris-versicolor']] ,
  [[5.6, 3.0, 4.5, 1.5], [0], ['Iris-versicolor']] ,
  [[5.8, 2.7, 4.1, 1.0], [0], ['Iris-versicolor']] ,
  [[6.2, 2.2, 4.5, 1.5], [0], ['Iris-versicolor']] ,
  [[5.6, 2.5, 3.9, 1.1], [0], ['Iris-versicolor']] ,
  [[5.9, 3.2, 4.8, 1.8], [0], ['Iris-versicolor']] ,
  [[6.1, 2.8, 4.0, 1.3], [0], ['Iris-versicolor']] ,
  [[6.3, 2.5, 4.9, 1.5], [0], ['Iris-versicolor']] ,
  [[6.1, 2.8, 4.7, 1.2], [0], ['Iris-versicolor']] ,
  [[6.4, 2.9, 4.3, 1.3], [0], ['Iris-versicolor']] ,
  [[6.6, 3.0, 4.4, 1.4], [0], ['Iris-versicolor']] ,
  [[6.8, 2.8, 4.8, 1.4], [0], ['Iris-versicolor']] ,
  [[6.7, 3.0, 5.0, 1.7], [0], ['Iris-versicolor']] ,
  [[6.0, 2.9, 4.5, 1.5], [0], ['Iris-versicolor']] ,
  [[5.7, 2.6, 3.5, 1.0], [0], ['Iris-versicolor']] ,
  [[5.5, 2.4, 3.8, 1.1], [0], ['Iris-versicolor']] ,
  [[5.5, 2.4, 3.7, 1.0], [0], ['Iris-versicolor']] ,
  [[5.8, 2.7, 3.9, 1.2], [0], ['Iris-versicolor']] ,
  [[6.0, 2.7, 5.1, 1.6], [0], ['Iris-versicolor']] ,
  [[5.4, 3.0, 4.5, 1.5], [0], ['Iris-versicolor']] ,
  [[6.0, 3.4, 4.5, 1.6], [0], ['Iris-versicolor']] ,
  [[6.7, 3.1, 4.7, 1.5], [0], ['Iris-versicolor']] ,
  [[6.3, 2.3, 4.4, 1.3], [0], ['Iris-versicolor']] ,
  [[5.6, 3.0, 4.1, 1.3], [0], ['Iris-versicolor']] ,
  [[6.1, 3.0, 4.6, 1.4], [0], ['Iris-versicolor']] ,
  [[5.8, 2.6, 4.0, 1.2], [0], ['Iris-versicolor']] ,
  [[5.0, 2.3, 3.3, 1.0], [0], ['Iris-versicolor']] ,
  [[5.6, 2.7, 4.2, 1.3], [0], ['Iris-versicolor']] ,
  [[5.7, 3.0, 4.2, 1.2], [0], ['Iris-versicolor']] ,
  [[5.7, 2.9, 4.2, 1.3], [0], ['Iris-versicolor']] ,
  [[6.2, 2.9, 4.3, 1.3], [0], ['Iris-versicolor']] ,
  [[5.1, 2.5, 3.0, 1.1], [0], ['Iris-versicolor']] ,
  [[5.7, 2.8, 4.1, 1.3], [0], ['Iris-versicolor']] ,
  [[6.3, 3.3, 6.0, 2.5], [1], ['Iris-virginica']] ,
  [[5.8, 2.7, 5.1, 1.9], [1], ['Iris-virginica']] ,
  [[7.1, 3.0, 5.9, 2.1], [1], ['Iris-virginica']] ,
  [[6.3, 2.9, 5.6, 1.8], [1], ['Iris-virginica']] ,
  [[6.5, 3.0, 5.8, 2.2], [1], ['Iris-virginica']] ,
  [[7.6, 3.0, 6.6, 2.1], [1], ['Iris-virginica']] ,
  [[4.9, 2.5, 4.5, 1.7], [1], ['Iris-virginica']] ,
  [[7.3, 2.9, 6.3, 1.8], [1], ['Iris-virginica']] ,
  [[6.7, 2.5, 5.8, 1.8], [1], ['Iris-virginica']] ,
  [[7.2, 3.6, 6.1, 2.5], [1], ['Iris-virginica']] ,
  [[6.5, 3.2, 5.1, 2.0], [1], ['Iris-virginica']] ,
  [[6.4, 2.7, 5.3, 1.9], [1], ['Iris-virginica']] ,
  [[6.8, 3.0, 5.5, 2.1], [1], ['Iris-virginica']] ,
  [[5.7, 2.5, 5.0, 2.0], [1], ['Iris-virginica']] ,
  [[5.8, 2.8, 5.1, 2.4], [1], ['Iris-virginica']] ,
  [[7.7, 3.8, 6.7, 2.2], [1], ['Iris-virginica']] ,
  [[7.7, 2.6, 6.9, 2.3], [1], ['Iris-virginica']] ,
  [[6.0, 2.2, 5.0, 1.5], [1], ['Iris-virginica']] ,
  [[6.9, 3.2, 5.7, 2.3], [1], ['Iris-virginica']] ,
  [[5.6, 2.8, 4.9, 2.0], [1], ['Iris-virginica']] ,
  [[7.7, 2.8, 6.7, 2.0], [1], ['Iris-virginica']] ,
  [[6.3, 2.7, 4.9, 1.8], [1], ['Iris-virginica']] ,
  [[6.7, 3.3, 5.7, 2.1], [1], ['Iris-virginica']] ,
  [[7.2, 3.2, 6.0, 1.8], [1], ['Iris-virginica']] ,
  [[6.2, 2.8, 4.8, 1.8], [1], ['Iris-virginica']] ,
  [[6.1, 3.0, 4.9, 1.8], [1], ['Iris-virginica']] ,
  [[6.4, 2.8, 5.6, 2.1], [1], ['Iris-virginica']] ,
  [[7.2, 3.0, 5.8, 1.6], [1], ['Iris-virginica']] ,
  [[7.4, 2.8, 6.1, 1.9], [1], ['Iris-virginica']] ,
  [[7.9, 3.8, 6.4, 2.0], [1], ['Iris-virginica']] ,
  [[6.4, 2.8, 5.6, 2.2], [1], ['Iris-virginica']] ,
  [[6.3, 2.8, 5.1, 1.5], [1], ['Iris-virginica']] ,
  [[6.1, 2.6, 5.6, 1.4], [1], ['Iris-virginica']] ,
  [[7.7, 3.0, 6.1, 2.3], [1], ['Iris-virginica']] ,
  [[6.3, 3.4, 5.6, 2.4], [1], ['Iris-virginica']] ,
  [[6.4, 3.1, 5.5, 1.8], [1], ['Iris-virginica']] ,
  [[6.0, 3.0, 4.8, 1.8], [1], ['Iris-virginica']] ,
  [[6.9, 3.1, 5.4, 2.1], [1], ['Iris-virginica']] ,
  [[6.7, 3.1, 5.6, 2.4], [1], ['Iris-virginica']] ,
  [[6.9, 3.1, 5.1, 2.3], [1], ['Iris-virginica']] ,
  [[5.8, 2.7, 5.1, 1.9], [1], ['Iris-virginica']] ,
  [[6.8, 3.2, 5.9, 2.3], [1], ['Iris-virginica']] ,
  [[6.7, 3.3, 5.7, 2.5], [1], ['Iris-virginica']] ,
  [[6.7, 3.0, 5.2, 2.3], [1], ['Iris-virginica']] ,
  [[6.3, 2.5, 5.0, 1.9], [1], ['Iris-virginica']] ,
  [[6.5, 3.0, 5.2, 2.0], [1], ['Iris-virginica']] ,
  [[6.2, 3.4, 5.4, 2.3], [1], ['Iris-virginica']] ,
  [[5.9, 3.0, 5.1, 1.8], [1], ['Iris-virginica']]
]

testpat = [
  [[5.1, 3.5, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[4.9, 3.0, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[4.7, 3.2, 1.3, 0.2], [-1], ['Iris-setosa']] ,
  [[5.4, 3.9, 1.7, 0.4], [-1], ['Iris-setosa']] ,
  [[4.6, 3.4, 1.4, 0.3], [-1], ['Iris-setosa']] ,
  [[5.0, 3.4, 1.5, 0.2], [-1], ['Iris-setosa']] ,
  [[4.4, 2.9, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[4.9, 3.1, 1.5, 0.1], [-1], ['Iris-setosa']] ,
  [[5.4, 3.7, 1.5, 0.2], [-1], ['Iris-setosa']] ,
  [[4.8, 3.4, 1.6, 0.2], [-1], ['Iris-setosa']] ,
  [[4.8, 3.0, 1.4, 0.1], [-1], ['Iris-setosa']] ,
  [[4.3, 3.0, 1.1, 0.1], [-1], ['Iris-setosa']] ,
  [[5.8, 4.0, 1.2, 0.2], [-1], ['Iris-setosa']] ,
  [[5.7, 4.4, 1.5, 0.4], [-1], ['Iris-setosa']] ,
  [[5.4, 3.9, 1.3, 0.4], [-1], ['Iris-setosa']] ,
  [[5.1, 3.5, 1.4, 0.3], [-1], ['Iris-setosa']] ,
  [[5.7, 3.8, 1.7, 0.3], [-1], ['Iris-setosa']] ,
  [[5.1, 3.8, 1.5, 0.3], [-1], ['Iris-setosa']] ,
  [[5.4, 3.4, 1.7, 0.2], [-1], ['Iris-setosa']] ,
  [[5.1, 3.7, 1.5, 0.4], [-1], ['Iris-setosa']] ,
  [[4.6, 3.6, 1.0, 0.2], [-1], ['Iris-setosa']] ,
  [[5.1, 3.3, 1.7, 0.5], [-1], ['Iris-setosa']] ,
  [[4.8, 3.4, 1.9, 0.2], [-1], ['Iris-setosa']] ,
  [[5.0, 3.0, 1.6, 0.2], [-1], ['Iris-setosa']] ,
  [[5.0, 3.4, 1.6, 0.4], [-1], ['Iris-setosa']] ,
  [[5.2, 3.5, 1.5, 0.2], [-1], ['Iris-setosa']] ,
  [[5.2, 3.4, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[4.7, 3.2, 1.6, 0.2], [-1], ['Iris-setosa']] ,
  [[4.8, 3.1, 1.6, 0.2], [-1], ['Iris-setosa']] ,
  [[5.4, 3.4, 1.5, 0.4], [-1], ['Iris-setosa']] ,
  [[5.2, 4.1, 1.5, 0.1], [-1], ['Iris-setosa']] ,
  [[5.5, 4.2, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[4.9, 3.1, 1.5, 0.1], [-1], ['Iris-setosa']] ,
  [[5.0, 3.2, 1.2, 0.2], [-1], ['Iris-setosa']] ,
  [[5.5, 3.5, 1.3, 0.2], [-1], ['Iris-setosa']] ,
  [[4.9, 3.1, 1.5, 0.1], [-1], ['Iris-setosa']] ,
  [[4.4, 3.0, 1.3, 0.2], [-1], ['Iris-setosa']] ,
  [[5.1, 3.4, 1.5, 0.2], [-1], ['Iris-setosa']] ,
  [[5.0, 3.5, 1.3, 0.3], [-1], ['Iris-setosa']] ,
  [[4.5, 2.3, 1.3, 0.3], [-1], ['Iris-setosa']] ,
  [[4.4, 3.2, 1.3, 0.2], [-1], ['Iris-setosa']] ,
  [[5.0, 3.5, 1.6, 0.6], [-1], ['Iris-setosa']] ,
  [[5.1, 3.8, 1.9, 0.4], [-1], ['Iris-setosa']] ,
  [[4.8, 3.0, 1.4, 0.3], [-1], ['Iris-setosa']] ,
  [[5.1, 3.8, 1.6, 0.2], [-1], ['Iris-setosa']] ,
  [[4.6, 3.2, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[5.3, 3.7, 1.5, 0.2], [-1], ['Iris-setosa']] ,
  [[5.0, 3.3, 1.4, 0.2], [-1], ['Iris-setosa']] ,
  [[7.0, 3.2, 4.7, 1.4], [0], ['Iris-versicolor']] ,
  [[6.4, 3.2, 4.5, 1.5], [0], ['Iris-versicolor']] ,
  [[6.9, 3.1, 4.9, 1.5], [0], ['Iris-versicolor']] ,
  [[5.5, 2.3, 4.0, 1.3], [0], ['Iris-versicolor']] ,
  [[6.5, 2.8, 4.6, 1.5], [0], ['Iris-versicolor']] ,
  [[5.7, 2.8, 4.5, 1.3], [0], ['Iris-versicolor']] ,
  [[6.3, 3.3, 4.7, 1.6], [0], ['Iris-versicolor']] ,
  [[4.9, 2.4, 3.3, 1.0], [0], ['Iris-versicolor']] ,
  [[6.6, 2.9, 4.6, 1.3], [0], ['Iris-versicolor']] ,
  [[5.2, 2.7, 3.9, 1.4], [0], ['Iris-versicolor']] ,
  [[5.0, 2.0, 3.5, 1.0], [0], ['Iris-versicolor']] ,
  [[5.9, 3.0, 4.2, 1.5], [0], ['Iris-versicolor']] ,
  [[6.0, 2.2, 4.0, 1.0], [0], ['Iris-versicolor']] ,
  [[6.1, 2.9, 4.7, 1.4], [0], ['Iris-versicolor']] ,
  [[5.6, 2.9, 3.6, 1.3], [0], ['Iris-versicolor']] ,
  [[6.7, 3.1, 4.4, 1.4], [0], ['Iris-versicolor']] ,
  [[5.6, 3.0, 4.5, 1.5], [0], ['Iris-versicolor']] ,
  [[5.8, 2.7, 4.1, 1.0], [0], ['Iris-versicolor']] ,
  [[6.2, 2.2, 4.5, 1.5], [0], ['Iris-versicolor']] ,
  [[5.6, 2.5, 3.9, 1.1], [0], ['Iris-versicolor']] ,
  [[5.9, 3.2, 4.8, 1.8], [0], ['Iris-versicolor']] ,
  [[6.1, 2.8, 4.0, 1.3], [0], ['Iris-versicolor']] ,
  [[6.3, 2.5, 4.9, 1.5], [0], ['Iris-versicolor']] ,
  [[6.1, 2.8, 4.7, 1.2], [0], ['Iris-versicolor']] ,
  [[6.4, 2.9, 4.3, 1.3], [0], ['Iris-versicolor']] ,
  [[6.6, 3.0, 4.4, 1.4], [0], ['Iris-versicolor']] ,
  [[6.8, 2.8, 4.8, 1.4], [0], ['Iris-versicolor']] ,
  [[6.7, 3.0, 5.0, 1.7], [0], ['Iris-versicolor']] ,
  [[6.0, 2.9, 4.5, 1.5], [0], ['Iris-versicolor']] ,
  [[5.7, 2.6, 3.5, 1.0], [0], ['Iris-versicolor']] ,
  [[5.5, 2.4, 3.8, 1.1], [0], ['Iris-versicolor']] ,
  [[5.5, 2.4, 3.7, 1.0], [0], ['Iris-versicolor']] ,
  [[5.8, 2.7, 3.9, 1.2], [0], ['Iris-versicolor']] ,
  [[6.0, 2.7, 5.1, 1.6], [0], ['Iris-versicolor']] ,
  [[5.4, 3.0, 4.5, 1.5], [0], ['Iris-versicolor']] ,
  [[6.0, 3.4, 4.5, 1.6], [0], ['Iris-versicolor']] ,
  [[6.7, 3.1, 4.7, 1.5], [0], ['Iris-versicolor']] ,
  [[6.3, 2.3, 4.4, 1.3], [0], ['Iris-versicolor']] ,
  [[5.6, 3.0, 4.1, 1.3], [0], ['Iris-versicolor']] ,
  [[6.1, 3.0, 4.6, 1.4], [0], ['Iris-versicolor']] ,
  [[5.8, 2.6, 4.0, 1.2], [0], ['Iris-versicolor']] ,
  [[5.0, 2.3, 3.3, 1.0], [0], ['Iris-versicolor']] ,
  [[5.6, 2.7, 4.2, 1.3], [0], ['Iris-versicolor']] ,
  [[5.7, 3.0, 4.2, 1.2], [0], ['Iris-versicolor']] ,
  [[5.7, 2.9, 4.2, 1.3], [0], ['Iris-versicolor']] ,
  [[6.2, 2.9, 4.3, 1.3], [0], ['Iris-versicolor']] ,
  [[5.1, 2.5, 3.0, 1.1], [0], ['Iris-versicolor']] ,
  [[5.7, 2.8, 4.1, 1.3], [0], ['Iris-versicolor']] ,
  [[6.3, 3.3, 6.0, 2.5], [1], ['Iris-virginica']] ,
  [[5.8, 2.7, 5.1, 1.9], [1], ['Iris-virginica']] ,
  [[7.1, 3.0, 5.9, 2.1], [1], ['Iris-virginica']] ,
  [[6.3, 2.9, 5.6, 1.8], [1], ['Iris-virginica']] ,
  [[6.5, 3.0, 5.8, 2.2], [1], ['Iris-virginica']] ,
  [[7.6, 3.0, 6.6, 2.1], [1], ['Iris-virginica']] ,
  [[4.9, 2.5, 4.5, 1.7], [1], ['Iris-virginica']] ,
  [[7.3, 2.9, 6.3, 1.8], [1], ['Iris-virginica']] ,
  [[6.7, 2.5, 5.8, 1.8], [1], ['Iris-virginica']] ,
  [[7.2, 3.6, 6.1, 2.5], [1], ['Iris-virginica']] ,
  [[6.5, 3.2, 5.1, 2.0], [1], ['Iris-virginica']] ,
  [[6.4, 2.7, 5.3, 1.9], [1], ['Iris-virginica']] ,
  [[6.8, 3.0, 5.5, 2.1], [1], ['Iris-virginica']] ,
  [[5.7, 2.5, 5.0, 2.0], [1], ['Iris-virginica']] ,
  [[5.8, 2.8, 5.1, 2.4], [1], ['Iris-virginica']] ,
  [[7.7, 3.8, 6.7, 2.2], [1], ['Iris-virginica']] ,
  [[7.7, 2.6, 6.9, 2.3], [1], ['Iris-virginica']] ,
  [[6.0, 2.2, 5.0, 1.5], [1], ['Iris-virginica']] ,
  [[6.9, 3.2, 5.7, 2.3], [1], ['Iris-virginica']] ,
  [[5.6, 2.8, 4.9, 2.0], [1], ['Iris-virginica']] ,
  [[7.7, 2.8, 6.7, 2.0], [1], ['Iris-virginica']] ,
  [[6.3, 2.7, 4.9, 1.8], [1], ['Iris-virginica']] ,
  [[6.7, 3.3, 5.7, 2.1], [1], ['Iris-virginica']] ,
  [[7.2, 3.2, 6.0, 1.8], [1], ['Iris-virginica']] ,
  [[6.2, 2.8, 4.8, 1.8], [1], ['Iris-virginica']] ,
  [[6.1, 3.0, 4.9, 1.8], [1], ['Iris-virginica']] ,
  [[6.4, 2.8, 5.6, 2.1], [1], ['Iris-virginica']] ,
  [[7.2, 3.0, 5.8, 1.6], [1], ['Iris-virginica']] ,
  [[7.4, 2.8, 6.1, 1.9], [1], ['Iris-virginica']] ,
  [[7.9, 3.8, 6.4, 2.0], [1], ['Iris-virginica']] ,
  [[6.4, 2.8, 5.6, 2.2], [1], ['Iris-virginica']] ,
  [[6.3, 2.8, 5.1, 1.5], [1], ['Iris-virginica']] ,
  [[6.1, 2.6, 5.6, 1.4], [1], ['Iris-virginica']] ,
  [[7.7, 3.0, 6.1, 2.3], [1], ['Iris-virginica']] ,
  [[6.3, 3.4, 5.6, 2.4], [1], ['Iris-virginica']] ,
  [[6.4, 3.1, 5.5, 1.8], [1], ['Iris-virginica']] ,
  [[6.0, 3.0, 4.8, 1.8], [1], ['Iris-virginica']] ,
  [[6.9, 3.1, 5.4, 2.1], [1], ['Iris-virginica']] ,
  [[6.7, 3.1, 5.6, 2.4], [1], ['Iris-virginica']] ,
  [[6.9, 3.1, 5.1, 2.3], [1], ['Iris-virginica']] ,
  [[5.8, 2.7, 5.1, 1.9], [1], ['Iris-virginica']] ,
  [[6.8, 3.2, 5.9, 2.3], [1], ['Iris-virginica']] ,
  [[6.7, 3.3, 5.7, 2.5], [1], ['Iris-virginica']] ,
  [[6.7, 3.0, 5.2, 2.3], [1], ['Iris-virginica']] ,
  [[6.3, 2.5, 5.0, 1.9], [1], ['Iris-virginica']] ,
  [[6.5, 3.0, 5.2, 2.0], [1], ['Iris-virginica']] ,
  [[6.2, 3.4, 5.4, 2.3], [1], ['Iris-virginica']] ,
  [[5.9, 3.0, 5.1, 1.8], [1], ['Iris-virginica']]
]

1 comment

Robert Fleming 8 years, 6 months ago  # | flag

This doesn't work for me. I get an error in sumError about pat not being defined globally.