コード例 #1
0
ファイル: sgp.py プロジェクト: invinciblejha/Machine-Learning
def merging_step(groups):
	representants = get_all_representants(groups)
	for group in groups:
		merge = True
		snd_nearest_group = None
		for pattern in group.get_instances():
			nearests = knn.get_knn(2, pattern, representants)
			snd_tmp = get_group_by_representant(nearests[1], groups)
			if snd_nearest_group == None:
				snd_nearest_group = snd_tmp
				if snd_nearest_group.get_classe() != group.get_classe():
					merge = False
					break
			elif snd_tmp != snd_nearest_group:
				merge = False
				break
		
		if merge == True and snd_nearest_group != None:
			new_group = snd_nearest_group + group
			groups.remove(group)
			groups.remove(snd_nearest_group)
			groups.append(new_group)
			
			merging_step(groups) # RECURSSION
			return True
	return False	
コード例 #2
0
ファイル: sgp.py プロジェクト: invinciblejha/Machine-Learning
def alg(groups):
	exit_count = 0
	for group in groups:

		all_representants = get_all_representants(groups)
	
		instance_and_nearest = []
		new_group = Group()
	
		for sample in group.get_instances():
			[nearest_rep] = knn.get_knn(1, sample, all_representants)

			if nearest_rep != group.get_representant():
				nearest_g = get_group_by_representant(nearest_rep, groups)
				instance_and_nearest.append((sample, nearest_g))
				
		if len(instance_and_nearest) == 0:
			exit_count = exit_count + 1

		elif len(instance_and_nearest) == group.count_instances():
			elements = group.get_instances()
			P, med = hama.pca([e[:-1] for e in elements], 1)
			normal_vector = P
			prototype = group.get_representant()[:-1]
			[d] = hama.proj(P, prototype, med)
			
			for inst in group.get_instances():
				[di] = hama.proj(P, inst[:-1], med)
				if d < di:
					group.remove_instance(inst)
					new_group.add_instance(inst)
		else:
			for tupla in instance_and_nearest:
				bad_instance = tupla[0]
				nearest_g = tupla[1]
				
				# [new_nearest] = knn.get_knn(1, bad_instance, [nearest_g.get_representant(), group.get_representant()])
				# if new_nearest == nearest_g.get_representant():
				if nearest_g.get_representant() != group.get_representant() and nearest_g.get_classe() != group.get_classe():
					group.remove_instance(bad_instance)
					new_group.add_instance(bad_instance)
				elif nearest_g.get_representant() != group.get_representant(): # mas são da mesma classe
					nearest_g.add_instance(bad_instance)
					group.remove_instance(bad_instance)
				#	nearest_g.update_all()
				#	group.update_all()

		if not new_group.is_empty():
			new_group.update_all()
			groups.append(new_group)

		update_all_groups(groups)

	if exit_count != len(groups):
		return alg(groups)
		
	return groups