Ejemplo n.º 1
0
	def learn (self, data, header=None, gamma=0.001, verbose=False):
		" Estimate parameters from data "
		start = time.time() # mark starting time
		if header is None:
			header = [self.class_variable] + self.attributes
		learner = ParameterLearner(self.bn, data, header)
		# header contains the order of appearance of variables in file (default is attribute variables list passed in __init__)
		learner.learn(gamma) # gamma is the prior probabilities parameter
		end = time.time() # mark ending time
		if verbose:
			print 'Elapsed time: %dm%.3fs' % ((end-start)/60.0,(end-start)%60.0) 
Ejemplo n.º 2
0
def run():
	" Run this example "
	begining = time.time()

	name = "dogproblem"
	header = \
	"""	The Dog Problem Network
		 F     B
		 |\   /
		 | \ /
		 v  v
		 L  D-->H

		Extracted from E. Charniak.
		Bayesian networks without tears.
		AI Magazine, 1991. \n """

	F = RandomVariable('family_out',['yes','no'])
	B = RandomVariable('bowel_problem',['yes','no'])
	L = RandomVariable('lights',['on', 'off'])
	D = RandomVariable('dog_out',['yes', 'no'])
	H = RandomVariable('hear_bark',['yes', 'no'])
	### Graph Nodes
	V = [F,B,L,D,H]
	### Graph Arcs
	E = [(F,L), (F,D),
	     (B,D), (D,H)]

	### Bayesian network
	g = DBN(V,E,name,header)

	markone = time.time() # mark time

	dat = load_csv(data_filename)

	names = [F,B,L,D,H] # variable ordering in datafile

	marktwo = time.time() # mark time

	learner = ParameterLearner(g,dat,names)
	learner.learn(0.001)

	markthree = time.time() # mark time

	#for v in g.V:
	#	print v.cpt
	print g

	end = time.time()

	print 'Elapsed time on network construction: %dm%.3fs' % ((markone-begining)/60.0,(markone-begining)%60.0) 
	print 'Elapsed time on loading data: %dm%.3fs' % ((marktwo-markone)/60.0,(marktwo-markone)%60.0) 
	print 'Elapsed time on learning: %dm%.3fs' % ((markthree-marktwo)/60.0,(markthree-marktwo)%60.0) 
	print 'Total elapsed time: %dm%.3fs' % ((end-begining)/60.0,(end-begining)%60.0)