Пример #1
0
def test_system3():
  print "Initializing"
  
  train_size = 500
  sequence_length = 2
  gamma_quantile = 50
  test_size = 500

  import a_machine.system3 as system
  
  print "Importing & Normalizing Data"
  
  from santa_fe import getData
  data = getData('B1.dat')
  test = getData('B2.dat')
  median = np.median(data, axis=0)
  std = np.std(data, axis=0)

  # normalizing to median 0, std deviation 1
  data = ( data - median ) / std
  
  
  print "Initializing Models"
  
  model = system.model(gamma_samples=1000, gamma_quantile=gamma_quantile, sequence_length=sequence_length) 
  model.train( data, train_size )   
  
  print "Generating Predictions"
  
  # [test_point][dimension]
  #normed_test = (test[:test_size,:] - median) / std
  normed_test = data[:test_size]
  predictions, risks = model.predict(normed_test)
  hybrid = ( predictions * risks ).sum(1) / risks.sum(1)
  
  # denormalize
  predictions = ( std.reshape(1,1,3) * predictions ) + median.reshape(1,1,3)
  hybrid = ( std.reshape(1,3) * hybrid ) + median.reshape(1,3)
  
  print "Results!"
  
  errors = np.abs( np.expand_dims( test[sequence_length : test_size], 1) - predictions )
  hybrid_error = np.abs( test[sequence_length : test_size] - hybrid )
  print hybrid.shape
  print hybrid_error.shape
  
  print ( hybrid_error.sum(0) / test_size )
  print ( errors.sum(0) / test_size )
  print std.astype('int')
  
  x = np.arange(test_size-sequence_length)
  
  for i in range(data.shape[1]):
    fig = plt.subplot(data.shape[1],1,i+1)
    
    fig.plot(x, test[sequence_length : test_size,i], 'k--')
    for j in range(predictions.shape[1]):
      fig.plot(x, predictions[:,j,i] )

    fig.plot(x, hybrid[:,i], 'r', lw=2)
  plt.show()
Пример #2
0
def test_system4():
  print "Initializing"
  
  train_size = 1000
  sequence_length = 1
  gamma_quantile = 100
  test_size = 200

  import a_machine.system4 as system
  
  print "Importing & Normalizing Data"
  
  from santa_fe import getData
  data = getData('B1.dat')
  test = getData('B2.dat')
  median = np.median(data, axis=0)
  std = np.std(data, axis=0)

  # normalizing to median 0, std deviation 1
  normed_data = ( data - median ) / std
  
  print "Initializing Models"
  
  model = system.model(dimension=0, gamma_samples=1000, gamma_quantile=gamma_quantile, sequence_length=sequence_length) 
  model.train( normed_data, train_size )   
  
  print "Generating Predictions"
  
  # [test_point][dimension]
  test = test[:test_size+sequence_length,:]
  #test = data[:test_size+sequence_length,:]
  normed_test = (test - median) / std
  predictions = model.predict(normed_test)
  
  # denormalize
  #predictions = ( std[0] * predictions ) + median[0]

  errors = np.abs( normed_test[sequence_length : predictions.shape[0]+sequence_length, 0] - predictions )
  
  print "Results!  Loss/point: %s (in normed space)" % ( errors.sum(0) / test_size )
  
  x = np.arange( predictions.shape[0] )
  
  x_pred = np.dstack( [
    np.arange(model.sequences[:,0,0].min(), model.sequences[:,0,0].max(), ( model.sequences[:,0,0].max() - model.sequences[:,0,0].min() ) / 100 ),
    np.arange(model.sequences[:,0,1].min(), model.sequences[:,0,1].max(), ( model.sequences[:,0,1].max() - model.sequences[:,0,1].min() ) / 100 ),
    np.arange(model.sequences[:,0,2].min(), model.sequences[:,0,2].max(), ( model.sequences[:,0,2].max() - model.sequences[:,0,2].min() ) / 100 ),
  ]).astype('float32').reshape(100,1,3)

  y_pred = model.svm.predict( kernel_matrix(x_pred, model.sequences, model.gammas[-1] ) )
  print x_pred[0]
  print x_pred[1]
  
  pr = plt.subplot(2,2,1)
  pr.plot(x,normed_test[sequence_length : predictions.shape[0]+sequence_length, 0], 'k', alpha=.4)
  #for i in range(predictions.shape[1]):
  #  for j in range(predictions.shape[2]):
  #    plt.plot(x,predictions[:,i,j])
  pr.plot(x,predictions)
  
  
  reg0 = plt.subplot(2,2,2)
  reg0.plot(model.sequences[:,0,0], model.labels, 'k,', alpha=.5) 
  reg0.plot(model.sequences[model.svm.SV_indices,0,0], model.labels[model.svm.SV_indices], 'o', alpha=.15 )
  reg0.plot(x_pred[:,0,0], y_pred, 'r', lw=2)

  reg1 = plt.subplot(2,2,3)
  reg1.plot(model.sequences[:,0,1], model.labels, 'k,', alpha=.5) 
  reg1.plot(model.sequences[model.svm.SV_indices,0,1], model.labels[model.svm.SV_indices], 'o', alpha=.15 )
  reg1.plot(x_pred[:,0,1], y_pred, 'r',lw=2)

  reg2 = plt.subplot(2,2,4)
  reg2.plot(model.sequences[:,0,2], model.labels, 'k,', alpha=.5) 
  reg2.plot(model.sequences[model.svm.SV_indices,0,2], model.labels[model.svm.SV_indices], 'o', alpha=.15 )
  reg2.plot(x_pred[:,0,2], y_pred, 'r',lw=2)
    
  plt.show()

  return