示例#1
0
文件: script.py 项目: kerinin/iEngine
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()
示例#2
0
文件: script.py 项目: kerinin/iEngine
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()
示例#3
0
文件: script.py 项目: kerinin/iEngine
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()
示例#4
0
def run():
	print "Starting"
	
	print "Loading Dataset"
	# Retrieve dataset
	data = getData('B1.dat')[:100]
	#data = list()
	#for i in range(100):
	#	data.append( array( [gauss(2.0,.1), gauss(0.0,.1) ]) )
	#for i in range(100):
	#	data.append( array( [gauss(0.0,.1), gauss(2.0,.1) ]) )
		
	print "Constructing Module"
	mod = inference_module(data,gm=2,nu=.1)
	
	print "Parsing Results"
	clusters = [ [] , ]*(len(mod.clusters))
	bsv = list()
	colors = ['b','r','g','c','m','y','w']

	for point in data:
		vector = mod.induce( data_vector(point),False)
		
		if vector.cluster != None:
			if not clusters[vector.cluster]:
				clusters[vector.cluster] = [vector,]
			else:
				clusters[vector.cluster].append(vector)
		else:
			bsv.append(vector)
				
	print "Formatting Results"
	for cluster in clusters:
		if cluster:
			scatter( [point.data[0] for point in cluster], [point.data[1] for point in cluster],s=10,  c=colors[cluster[0].cluster % 7])
		
	for cluster in mod.clusters:
		scatter( [SV.data[0] for SV in cluster],  [SV.data[1] for SV in cluster],  s=30,  c=colors[cluster[0].cluster % 7],  label="Cluster %s" % cluster[0].cluster )
			
	if bsv:
		scatter( [v.data[0] for v in bsv], [v.data[1] for v in bsv], s=10, marker="x" )
			
	title('%s SV in %s clusters from %s points' % (len(mod.SV),len(mod.clusters),len(data)))
	show()
示例#5
0
def run():
	# Retrieve dataset
	data = getData('B1.dat')[:20]
	#data = array([sin(i/4.) for i in range(33)])
	
	# Construct Variables
	K = kernel(data,gamma=.1,sigma_q=.5)
	F = estimate(data[:-1],data[1:],K)
	
	# Objective Function
	print 'constructing objective function...'
	P = mul(K.xx,K.yy)
	q = matrix(0.0,(K.l,1))
	
	# Equality Constraint
	print 'constructing equality constraints...'
	#A = matrix( [ sum( K.xx[ n*K.l:( n+1 )*K.l ] for n in range( K.l ) ) ], ( 1,K.l ) ) / K.l
	A = matrix( [ sum( K.xx[ n::K.l ] for n in range( K.l ) ) ], ( 1,K.l ) ) / K.l
	b = matrix(1.0)
	
	# Inequality Constraint
	print 'constructing inequality constraints...'
	G = matrix(0.0, (K.l,K.l))
	for m in range(K.l):		
		print "Inequality (%s,n) of %s calculated" % (m,K.l)
		k = K.xx[m::K.l]
		
		for n in range(m,K.l):
			if K.n > 1:
				t =array( [min(K.x[n] - K.x[i]) > 0 for i in range(K.l)] )
			else:
				t = array( [K.x[n] - K.x[i] > 0 for i in range(K.l)])
			i = K.intg[m,n]
			
			G[n,m] = sum(k*t*i)/K.l - F.xy(K.x[n],K.y[n])
			G[m,n] = sum(k*t*i)/K.l - F.xy(K.x[n],K.y[n])
	print G
	h = matrix(K.sigma, (K.l,1))
	
	# Optimize
	#f=open('beta.matrix','r')
	#F.beta = fromfile(f)
	#f.close()
	
	print 'starting optimization...'
	optimized = qp(P, q,G=G, h=h, A=A, b=b)
	F.beta = optimized['x']
	print F.beta
	f=open('beta.matrix','w')
	F.beta.tofile(f)
	f.close()
	print 'beta saved to file'
	
	# test on training data
	x_1 = list()
	y_1 = list()
	
	for i in range(K.l):
		x_1.append( F.r(K.x[i])[0] )
		y_1.append( K.y[i][0] )
		
	plot(x_1,label="x'")
	plot(y_1,label="y")
	
	legend()
	show()
示例#6
0
文件: script.py 项目: kerinin/iEngine
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()
示例#7
0
文件: script.py 项目: kerinin/iEngine
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