コード例 #1
0
	def test_sparse_vector(self):
		maxent = MaxEnt()
		_ = maxent.compute_observed_counts(self.training_set)
		woof_index = maxent.feature_alphabet.get_index('woof') 
		meow_index = maxent.feature_alphabet.get_index('meow') 
		sparse_vector = self.training_set[1].data
		self.assertEqual(meow_index, sparse_vector[0])
		self.assertEqual(woof_index, sparse_vector[1])
コード例 #2
0
	def test_label_loglikelihood_score(self):
		maxent = MaxEnt()
		_ = maxent.compute_observed_counts(self.training_set)
		maxent.parameters = numpy.array([0.1, 0.5, 1, 0, 100, -0.1, -0.5, -1, 0, -100])
		score_vector = maxent.compute_label_unnormalized_loglikelihood_vector(self.training_set[0].data)
		self.assertEqual(score_vector[0], 1.6)
		self.assertEqual(score_vector[1], -1.6)
		posterior_distribution = maxent.compute_posterior_distribution(self.training_set[0])
		self.assertAlmostEqual(posterior_distribution[0], 0.96, 2)
コード例 #3
0
	def test_observed_counts(self):
		maxent = MaxEnt()
		observed_counts = maxent.compute_observed_counts(self.training_set)
		
		cat_index = maxent.label_alphabet.get_index('cat')
		purr_index = maxent.feature_alphabet.get_index('purr') 
		meow_index = maxent.feature_alphabet.get_index('meow') 
		bark_index = maxent.feature_alphabet.get_index('bark') 
		woof_index = maxent.feature_alphabet.get_index('woof') 
		
		cat_related_indices = maxent.get_parameter_indices([purr_index, meow_index, bark_index, woof_index], cat_index)
		cat_count, cat_purr_count, cat_meow_count, cat_bark_count, cat_woof_count = observed_counts[cat_related_indices]
		self.assertEqual(cat_count, 3)
		self.assertEqual(cat_purr_count, 2)
		self.assertEqual(cat_meow_count, 2)
		self.assertEqual(cat_bark_count, 0)
		self.assertEqual(cat_woof_count, 1)