Ejemplo n.º 1
0
def main():
	"""
	Run a simple test case
	"""
	n=100

	stdr.seed(0)
	nr.seed(0)

	#sample from the first normal
	sample = list(normal(0, size=n))
	clusterId = [0] * n

	#add to the sample
	sample += list(normal(10, size=n))
	clusterId += [1]*n

	#shuffle everything
	mixed = zip(sample, clusterId)

	shuffle(mixed)

	sample = first(mixed)
	clusterId = second(mixed)

	answers = {s:cId for s,cId in zip(sample,clusterId)}

	def adj(p):
		return [q for q in sample if abs(q-p) < .5]

	#cluster the sample
	results = cluster(sample, adj, 5)

	#count up the results
	raw = [(pId, answers[p]) for p,pId in results.items()]
	counts = Counter(raw)

	print("Predicted\tCluster Id\tCounts")
	print("-----------------------------------------")

	#print the results
	for (predict, clusterId), count in counts.items():
		print("{}\t\t{}\t\t{}".format(predict, clusterId, count))

	print("---------------Metrics------------------")
	#true labels, predicted labels
	print("h**o {}".format(homogeneity_score(second(raw), first(raw))))
	print("complete {}".format(completeness_score(second(raw), first(raw))))
	print("v {}".format(v_measure_score(second(raw), first(raw))))
Ejemplo n.º 2
0
	def forward(self, sequence, graph):
		"""
		Applies each self-attention head to the sequence
		"""
		out = [head(sequence, graph) for head in self.heads]	
		
		return t.cat(first(out), 2), t.mean(t.stack(second(out)), 0)
Ejemplo n.º 3
0
def witholdTestItems(conn, datasetId, sequences, debug=False):
	"""
	Read the indexes of items that are withheld from test sequences
	"""
	withheld = first(conn.execute("""select sindex 
	from withheld
	where ds_id = ?
	order by sequence_id;""", [datasetId]).fetchall())

	assert len(seqs) == len(withheld)

	if debug:
		for i, (segment, withheld) in enumerate(zip(seqs, withheld)):
			print("{}: {}: {}".format(i,segment, withheld))
			print("withheld: {}".format(segment[withheld]))

	#build the sequences of segments
	segments = [ (s[:w], s[w:]) for s,w in zip(seqs, withheld) ]

	return segments, [s[w] for s,w in zip(seqs, withheld)]
Ejemplo n.º 4
0
	def allPhrases(self):
		"""
		Returns a list of all the phrases in the database, in alphabetical order
		"""
		return first(self.conn.execute("select name from phrase order by name"))
Ejemplo n.º 5
0
def allExp(conn):
    """
	Returns a list of all the experiments
	"""
    return set(first(conn.execute("select UUID from experiment").fetchall()))