Beispiel #1
0
def test_system4a():
  print "Initializing"
  
  from a_machine.system4 import model

  from santa_fe import getData
  data = getData('B1.dat')
  test = getData('B2.dat')
  median = np.median(data[:,:2], axis=0)
  std = np.std(data[:,:2], axis=0)

  train_size = 1000
  sequence_length = 1
  gamma_quantile = 100
  test_size = 200
    
  train = (( data[:train_size,:2] - median ) / std).astype('float32')
  labels = (( data[sequence_length:train_size+sequence_length,0] - median[0] ) / std[0]).astype('float32')
  
  
  #svm = SVR( nu=.1, C=50)
  m1 = model( dimension=0, sequence_length=sequence_length )
  m2 = model( dimension=0, sequence_length=sequence_length )
  #svm.train( kernel_matrix(np.expand_dims(train,1), np.expand_dims(train,1), .5), labels)
  m1.train(train[:,:2], train_size, [ [0,[0,1]], [0,[0]] ])
  m2.train(train[:,:2], train_size, [ [0,[0,1]], ])

  xN = 20
  yN = 20
  xrange = [train[:,0].min(), train[:,0].max()]
  yrange = [train[:,1].min(), train[:,1].max()]
  xstep = ((xrange[1]-xrange[0] ) / xN )
  ystep = ((yrange[1]-yrange[0] ) / yN )
  X = np.dstack(np.mgrid[xrange[0]:xrange[1]:xstep,yrange[0]:yrange[1]:ystep]).reshape([ xN *yN,2]).astype('float32')
  x = np.arange(xrange[0],xrange[1],xstep)
  y = np.arange(yrange[0],yrange[1],ystep)

  #Z = svm.predict( kernel_matrix( np.expand_dims(X,1), np.expand_dims(train,1), .5) )
  Z1 = m1.predict( np.expand_dims(X,1) )
  Z2 = m2.predict( np.expand_dims(X,1) )

  #ax = plt.subplot(111, projection='3d')
  #ax.plot( x[:,0], x[:,1], y, 'k,' )
  #plt.plot( train[:,0], labels, 'k,', alpha=.2 )
  #plt.plot( train[svm.support_,0], labels[svm.support_], 'o', alpha=.15 )
  #plt.plot(x[:,0],y, 'r', lw=2)
  #plt.show()
  
  mlab.points3d(train[:,0], train[:,1], labels, scale_factor=.05, opacity=.2)
  #mlab.points3d(train[m.svm.SV_indices,0], train[m.svm.SV_indices,1], labels[m.svm.SV_indices], scale_factor=.05)
  mlab.surf( x,y,Z1.reshape(xN,yN), color=(1,0,0) ) #red, multiple slices
  mlab.surf( x,y,Z2.reshape(xN,yN)+.1, color=(0,1,0) ) #green, single slice

  print m1.svm.SV_indices
  print m2.svm.SV_indices
  
  print "%s SV of %s" % (len(m1.svm.SV_indices), train.shape[0])
  print "%s SV of %s" % (len(m2.svm.SV_indices), train.shape[0])
  
  mlab.show()
Beispiel #2
0
def test_system5():
  from a_machine.system5 import model

  from santa_fe import getData
  data = getData('B1.dat')
  test = getData('B2.dat')
  median = np.median(data[:,:3], axis=0)
  std = np.std(data[:,:3], axis=0)

  train_size = 1000
  sequence_length = 1
  gamma_quantile = 100
  test_size = 200

  train = (( data[:train_size] - median ) / std).astype('float32')

  m = model( dimension=0 )
  m.train(train)

  xN = test_size
  #yN = 20
  xrange = [train.min(), train.max()]
  #yrange = [train[:-1,1].min(), train[:-1,1].max()]
  xstep = ((xrange[1]-xrange[0] ) / xN )
  #ystep = ((yrange[1]-yrange[0] ) / yN )
  #X = np.dstack(np.mgrid[xrange[0]:xrange[1]:xstep,yrange[0]:yrange[1]:ystep]).reshape([ xN *yN,2]).astype('float32')
  x = np.arange(xrange[0],xrange[1],xstep)
  #y = np.arange(yrange[0],yrange[1],ystep)

  Y = m.predict( X )
  
  plt.scatter(train[:-1], train[1:] )
  plt.plot( x, Y, 'k', lw=2 )
  plt.show()
Beispiel #3
0
def run():
  from a_machine.system7 import model

  from santa_fe import getData
  data = getData('B1.dat')[:,1]
  test = getData('B2.dat')
  median = np.median(data)
  std = np.std(data)

  train_size = 100
  sequence_length = 1
  gamma_quantile = 100
  test_size = 201

  train = (( data[:train_size] - median ) / std).astype('float32')
  #train = data[:train_size].astype('float32')

  m = model( dimension=0 )
  m.train(train)

  xN = test_size
  #yN = 20
  xrange = [train.min(), train.max()]
  #yrange = [train[:-1,1].min(), train[:-1,1].max()]
  xstep = ((xrange[1]-xrange[0] ) / xN )
  #ystep = ((yrange[1]-yrange[0] ) / yN )
  #X = np.dstack(np.mgrid[xrange[0]:xrange[1]:xstep,yrange[0]:yrange[1]:ystep]).reshape([ xN *yN,2]).astype('float32')
  X = np.arange(xrange[0],xrange[1],xstep)
  #y = np.arange(yrange[0],yrange[1],ystep)

  print X.shape
  print xN
  Y = m.predict( X.reshape(xN,1,1) )
  
  plt.plot(train[:-1], train[1:], 'k,')
  plt.scatter( m.SVx, m.SVy, train_size*10*(m.beta**2), alpha=.15 )
  plt.plot(X, Y, 'r', lw=2)
  #plt.axhline(y=0)
  
  plt.show()