Пример #1
0
    def __init__(self, bzrc):
        self.bzrc = bzrc
        self.constants = self.bzrc.get_constants()
        self.commands = []
        self.ALPHA = 0.01
        self.BETA = 0.3
        self.OBS_TOLERANCE = 35.0
        self.S = 50
        self.wroteonce = False
        self.goalradius = 30

        self.tankradius = 5
        self.avoidradius = 50
        self.avoidBETA = 0.05

        self.aimtolerance = math.pi / 20

        self.world_grid = WorldGrid()
        self.bayes = Bayes()

        self.turnprob = 0.05
        self.turndecisionprob = 0.5
        self.turned = False
        self.turniter = 0
        self.TURN_MAX = 50
        self.OCCUPIED = 1
        self.UNOCCUPIED = 0
Пример #2
0
 def __init__(self):
     self.lh = Lighthouse(.25, .75)
     x = np.linspace(0, 1, 99)
     y = np.linspace(0, 1, 99)
     pdf = {(xi, yi): 1 for xi in x for yi in y}
     likelihood = lambda x, (d, m): d / (d**2 + (x - m)**2)
     self.bayes = Bayes(pdf, likelihood)
 def __init__(self):
     if os.path.getsize("bd_path.txt") > 0:
         with open("bd_path.txt", 'rb') as f:
             self._bd_path = f.readline()
         f.close()
     self._genre_features = np.load('genres_features.npy')
     self._knn = KNN(self._bd_path, self._genre_features)
     self._bayes = Bayes(self._bd_path, self._genre_features)
     self._genres = np.load('genres.npy')
 def change_db(self, path):
     self._bd_path = path
     with open("bd_path.txt", 'w') as f:
         f.write(path)
     f.close()
     # self._genres, self._genre_features = extract_funtions.extrakt_features_for_genres(self._bd_path)
     # np.save('genres.npy', self._genres)
     # np.save("genres_features.npy", self._genre_features)
     self._knn = KNN(self._bd_path, self._genre_features)
     self._bayes = Bayes(self._bd_path, self._genre_features)
     #self._knn.train_model()
     self._bayes.train_model()
Пример #5
0
class Finder(object):
    def __init__(self):
        self.lh = Lighthouse(.25, .75)
        x = np.linspace(0, 1, 99)
        y = np.linspace(0, 1, 99)
        pdf = {(xi, yi): 1 for xi in x for yi in y}
        likelihood = lambda x, (d, m): d / (d**2 + (x - m)**2)
        self.bayes = Bayes(pdf, likelihood)

    def solve_stuff(self):
        for datum in self.lh.dataset:
            self.bayes.update(datum)

    def print_distribution(self):
        self.bayes.print_distribution()
Пример #6
0
 def __init__(self):
     self.lh = Lighthouse(.25,.75)
     x = np.linspace(0, 1, 99)
     y = np.linspace(0, 1, 99)
     pdf = {(xi,yi):1 for xi in x for yi in y}
     likelihood = lambda x, (d, m): d / (d**2 + (x-m)**2 )
     self.bayes = Bayes(pdf, likelihood)
Пример #7
0
class Finder(object):

    def __init__(self):
        self.lh = Lighthouse(.25,.75)
        x = np.linspace(0, 1, 99)
        y = np.linspace(0, 1, 99)
        pdf = {(xi,yi):1 for xi in x for yi in y}
        likelihood = lambda x, (d, m): d / (d**2 + (x-m)**2 )
        self.bayes = Bayes(pdf, likelihood)

    def solve_stuff(self):
        for datum in self.lh.dataset:
            self.bayes.update(datum)

    def print_distribution(self):
        self.bayes.print_distribution()
Пример #8
0
	def __init__(self, bzrc):
		self.bzrc = bzrc
		self.constants = self.bzrc.get_constants()
		self.commands = []
		self.ALPHA = 0.01
		self.BETA = 0.3
		self.OBS_TOLERANCE = 35.0
		self.S = 50
		self.wroteonce = False
		self.goalradius = 30
		
		self.tankradius = 5
		self.avoidradius = 50
		self.avoidBETA = 0.05
		
		self.aimtolerance = math.pi/20
		
		self.world_grid = WorldGrid()
		self.bayes = Bayes()
		
		self.turnprob = 0.05
		self.turndecisionprob = 0.5
		self.turned = False
		self.turniter = 0
		self.TURN_MAX = 50
		self.OCCUPIED = 1;
		self.UNOCCUPIED = 0;
Пример #9
0
def prep_model(can_load_model, load_path):
    bayes = None
    if can_load_model:
        with open(load_path, "rb") as f:
            bayes = pickle.load(f)
    else:
        bayes = Bayes()
    return bayes
class Classifier:
    def __init__(self):
        if os.path.getsize("bd_path.txt") > 0:
            with open("bd_path.txt", 'rb') as f:
                self._bd_path = f.readline()
            f.close()
        self._genre_features = np.load('genres_features.npy')
        self._knn = KNN(self._bd_path, self._genre_features)
        self._bayes = Bayes(self._bd_path, self._genre_features)
        self._genres = np.load('genres.npy')

    def change_db(self, path):
        self._bd_path = path
        with open("bd_path.txt", 'w') as f:
            f.write(path)
        f.close()
        # self._genres, self._genre_features = extract_funtions.extrakt_features_for_genres(self._bd_path)
        # np.save('genres.npy', self._genres)
        # np.save("genres_features.npy", self._genre_features)
        self._knn = KNN(self._bd_path, self._genre_features)
        self._bayes = Bayes(self._bd_path, self._genre_features)
        #self._knn.train_model()
        self._bayes.train_model()

    def interpret_approx_array(self, track_path, model: Model):
        genres = []
        probabilities = []
        probs = model.approx_genres(track_path)
        probssort = np.argsort(probs)
        probssort = np.flip(probssort, 0)
        for i in probssort:
            genres.append(self._genres[i])
            probabilities.append(round(probs[i] * 100, 2))
        return genres, probabilities  # od największych do najmniejszych

    def approximate(self, track_path, mode) -> (list, list):
        if mode == "knn":
            return self.interpret_approx_array(track_path, self._knn)
        if mode == "bayes":
            return self.interpret_approx_array(track_path, self._bayes)
        return list(), list()

    def get_db_name(self):
        return self._bd_path
Пример #11
0
def main():
	# Process CLI arguments.
	try:
		execname, host, port = sys.argv
	except ValueError:
		execname = sys.argv[0]
		print >>sys.stderr, '%s: incorrect number of arguments' % execname
		print >>sys.stderr, 'usage: %s hostname port' % sys.argv[0]
		sys.exit(-1)

	# Connect.
	#bzrc = BZRC(host, int(port), debug=True)
	bzrc = BZRC(host, int(port))
	
	bayes = Bayes()
	constants = bzrc.get_constants()
	bayes.self_not_obs_given_not_occ(float(constants['truenegative']))
	bayes.set_obs_given_occ(float(constants['truepositive']))
	
	agent = Agent(bzrc, bayes)

	prev_time = time.time()

	# Run the agent
	try:
		while True:
			time_diff = time.time() - prev_time
			agent.tick(time_diff)
	except KeyboardInterrupt:
		print "Exiting due to keyboard interrupt."
		bzrc.close()
Пример #12
0
def modelOutput(trainFile, testFile, modelType):
    """
    output is:
        (naive bayes) variable name | 'class'
        (tan) variable name | name of its parents
    # empty
    followed by:
        predict class | actual class | posterior probability (12 digits after decimal point)
    # empty
    followed by:
        The number of the test-set examples that were correctly classified.
    """

    attributes, labels, instances = data_provider(trainFile)
    if modelType == 'n':
        model = Bayes(attributes, labels, instances)
    elif modelType == 't':
        model = TAN(attributes, labels, instances)
    else:
        import sys
        print >> sys.stderr, 'model type should be [n] or [t] !!!'
        sys.exit()
    attributes, labels, instances = data_provider(testFile)

    # format output part1: attribute name | 'class'
    model.printTree()
    print

    correctClassCnt = 0
    for test in instances:
        result = model.classify(test)
        if result[0] == result[1]:
            correctClassCnt += 1
        # format output part2: predict class | actual class | posterior probability
        print formatOutput(result)
    print

    # format output part3: correctly classified number of test instances
    print correctClassCnt
Пример #13
0
class Sentiment:
    def __init__(self):
        self.classifier = Bayes()
        self.seg = Seg()
        self.seg.load('seg.pickle')

    def save(self, fname):
        self.classifier.save(fname)

    def load(self, fname):
        self.classifier = self.classifier.load(fname)

    def handle(self, doc):
        words = self.seg.seg(doc)
        words = self.filter_stop(words)

        return words

    def train(self, neg_docs, pos_docs):
        datas = []
        for doc in neg_docs:
            datas.append([self.handle(doc), 'neg'])
        for doc in pos_docs:
            datas.append([self.handle(doc), 'pos'])

        self.classifier.train(datas)

    def classify(self, doc):
        ret, prob = self.classifier.classify(self.handle(doc))
        if ret == 'pos':
            return prob
        else:
            return 1 - prob

    @staticmethod
    def filter_stop(words):
        return list(filter(lambda x: x not in stop_words, words))
Пример #14
0
         finalOutput.write(method + "," + line + "\n")
 
 method = "SuperGrep"
 print method
 shutil.rmtree("cleanData/", True)
 grep = Grep(True)
 grep.initalPreprocess(DR, DT, L, TEST)
 grep.testDirToOutput("cleanData/TEST/", "cleanData/" )
 with open("cleanData/output.txt") as methodOutput:
     for line in methodOutput.read().split():
         finalOutput.write(method + "," + line + "\n")
 
 method = "MultivariantBayes"
 print method
 shutil.rmtree("cleanData/", True)
 bayes = Bayes('multivariant')
 bayes.initalPreprocess(DR, DT, L, TEST, "custom", 1, False)                
 validate = CrossValidate("cleanData")
 featureSelector = MIFeatureSelector()
 validate.regularBayes(bayes, featureSelector, DISPLAY_ACCURACY)
 with open("cleanData/output.txt") as methodOutput:
     for line in methodOutput.read().split():
         finalOutput.write(method + "," + line + "\n")
 
 method = "MultinomialBayes"
 print method
 shutil.rmtree("cleanData/", True) 
 bayes = Bayes('multinomial')
 bayes.initalPreprocess(DR, DT, L, TEST, "custom", 1, False)            
 validate = CrossValidate("cleanData")
 featureSelector = FeatureSelector()     # Multinomial does much worse with MIFeatureSelector()
Пример #15
0
class Agent(object):
	"""Class handles all command and control logic for a teams tanks."""

	def __init__(self, bzrc):
		self.bzrc = bzrc
		self.constants = self.bzrc.get_constants()
		self.commands = []
		self.ALPHA = 0.01
		self.BETA = 0.3
		self.OBS_TOLERANCE = 35.0
		self.S = 50
		self.wroteonce = False
		self.goalradius = 30
		
		self.tankradius = 5
		self.avoidradius = 50
		self.avoidBETA = 0.05
		
		self.aimtolerance = math.pi/20
		
		self.world_grid = WorldGrid()
		self.bayes = Bayes()
		
		self.turnprob = 0.05
		self.turndecisionprob = 0.5
		self.turned = False
		self.turniter = 0
		self.TURN_MAX = 50
		self.OCCUPIED = 1;
		self.UNOCCUPIED = 0;

	def tick(self, time_diff):
		"""Some time has passed; decide what to do next."""
		mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff()
		self.mytanks = mytanks
		self.othertanks = othertanks
		self.flags = [flag for flag in flags if flag.color != self.constants['team']]
		self.shots = shots
		self.enemies = [tank for tank in othertanks if tank.color !=
						self.constants['team']]
		#self.obstacles = self.bzrc.get_obstacles()
		self.commands = []
	
		for tank in mytanks:
			pos, grid = self.bzrc.get_occgrid(tank.index)
			self.update_priors(pos, grid)
			
		self.world_grid.draw_obstacle_grid()
		
		for tank in mytanks:
			self.goto_closest_target(tank)

		results = self.bzrc.do_commands(self.commands)
	
	def update_priors(self, position, grid):
		for x in range(len(grid)):
			for y in range(len(grid[x])):
				prior = self.world_grid.get_world_value(x, y, position)
				observation = grid[x][y]
				
				if observation == self.OCCUPIED:
					new_prior = self.bayes.probability_occupied_given_observed(prior)
				else:
					new_prior = self.bayes.probability_occupied_given_not_observed(prior)
					
				self.world_grid.set_world_value(x, y, position, new_prior)		
	
	def move(self, mytanks):
		# it's not turning, so decide to turn or go straight
		if self.turned == False and self.turniter == 0: 
			rand = random.random() # returns a number [0, 1)
			if rand < self.turnprob:
				for tank in mytanks:
					self.turn(tank)
					self.turned = True
			else:
				for tank in mytanks:
					self.go_straight(tank)

		else: # it is turning
			self.turniter = self.turniter + 1
			
			if self.turniter >= self.TURN_MAX:
				self.turned = False
				self.turniter = 0
	
	def turn(self, tank):
		rand = random.random() # returns a number [0, 1)
		if rand < self.turndecisionprob:
			command = Command(tank.index, 1, 1, False) # turn right
			self.commands.append(command)
		else:
			command = Command(tank.index, 0.5, -1, False) # turn left
			self.commands.append(command)
	
	def go_straight(self, tank):
		command = Command(tank.index, 1, 0, False)
		self.commands.append(command)
	
	def get_my_base(self):
		mybases = [base for base in self.bzrc.get_bases() if base.color == self.constants['team']]
		mybase = mybases[0]
		return mybase
	
	def get_base_center(self, base):
		center_x = ((base.corner1_x + base.corner2_x + base.corner3_x + base.corner4_x) / 4)
		center_y = ((base.corner1_y + base.corner2_y + base.corner3_y + base.corner4_y) / 4)
		return center_x, center_y
		
	def goto_closest_target(self, tank):
		best_tar = self.get_best_target(tank.x, tank.y)
		if best_tar is None:
			command = Command(tank.index, 0, 0, False)
			self.commands.append(command)
		else:
			print "Tank :", tank.x, tank.y
			print "Target: ", best_tar
			self.move_to_position(tank, best_tar[0], best_tar[1])

	def get_best_target(self, x, y):
		best_tar = None
		best_dist = 2 * float(self.constants['worldsize'])
		for tar in self.world_grid.getTargetLocations():
			dist = math.sqrt((tar[0] - x)**2 + (tar[1] - y)**2)
			if dist < best_dist:
				best_dist = dist
				best_tar = tar
		return best_tar

	def goto_flags(self, tank):
		best_flag = self.get_best_flag(tank.x, tank.y)
		if best_flag is None:
			command = Command(tank.index, 0, 0, False)
			self.commands.append(command)
		else:
			self.move_to_position(tank, best_flag.x, best_flag.y)

	def get_best_flag(self, x, y):
		best_flag = None
		best_dist = 2 * float(self.constants['worldsize'])
		for flag in self.flags:
			dist = math.sqrt((flag.x - x)**2 + (flag.y - y)**2)
			if dist < best_dist:
				best_dist = dist
				best_flag = flag
		return best_flag
		
	def avoid_target(self, my_x, my_y, target_x, target_y):
		goal_dist = math.sqrt((target_x - my_x)**2 + (target_y - my_y)**2)
		target_angle = math.atan2(target_y - my_y, target_x - my_x)
		
		dx = 0
		dy = 0
		
		s = self.avoidradius
		r = self.tankradius
		
		if goal_dist < self.tankradius:
			dx = -1 * math.cos(target_angle) * 1000 #infinity
			dy = -1 * math.sin(target_angle) * 1000 #infinity
		elif goal_dist >= self.tankradius and goal_dist <= (s + r):
			dx = -1 * self.avoidBETA * (s + r - goal_dist) * math.cos(target_angle)
			dy = -1 * self.avoidBETA * (s + r - goal_dist) * math.sin(target_angle)
		else:
			dx = 0
			dy = 0
		return dx, dy
		
	def calculate_enemies_delta(self, my_x, my_y, enemies):
		delta_x = 0
		delta_y = 0
		
		for tank in enemies:
			dx, dy = self.avoid_target(my_x, my_y, tank.x, tank.y)
			delta_x += dx
			delta_y += dy
		
		sqnorm = math.sqrt(delta_x**2 + delta_y**2)
		
		if sqnorm == 0:
			delta_x = 0
			delta_y = 0
		else:
			delta_x = delta_x / sqnorm
			delta_y = delta_y / sqnorm
		
		return delta_x, delta_y

	def calculate_objective_delta(self, my_x, my_y, target_x, target_y):
		goal_dist = math.sqrt((target_x - my_x)**2 + (target_y - my_y)**2)
		target_angle = math.atan2(target_y - my_y, target_x - my_x)
		
		delta_xG = self.ALPHA * goal_dist * math.cos(target_angle) # r = 0
		delta_yG = self.ALPHA * goal_dist * math.sin(target_angle) # r = 0
		
		sqnorm = math.sqrt(delta_xG**2 + delta_yG**2)
		
		#set magnitude
		magnitude = 0
		if goal_dist > self.goalradius:
			magnitude = 1
		else:
			magnitude = sqnorm
		
		if sqnorm == 0:
			delta_xG = 0
			delta_yG = 0
		else:
			delta_xG = delta_xG / sqnorm
			delta_yG = delta_yG / sqnorm
			
		return delta_xG, delta_yG, magnitude

	"""def calculate_obstacles_delta(self, x, y):
		delta_xO = 0
		delta_yO = 0
		
		for obs in self.obstacles:
			repel_xO, repel_yO = self.get_obstacle_force(obs, x, y)
			delta_xO += repel_xO
			delta_yO += repel_yO
		
		sqnorm = math.sqrt(delta_xO**2 + delta_yO**2)
			
		if sqnorm == 0:
			delta_xO = 0
			delta_yO = 0
		else:
			delta_xO = delta_xO / sqnorm
			delta_yO = delta_yO / sqnorm
		
		'''if delta_xG != 0 or delta_yG != 0:
			print "delta_xO: ", delta_xO
			print "delta_yO: ", delta_yO'''
			
		return delta_xO, delta_yO"""

	def calculate_random_delta(self):
		dx = random.uniform(-.01, .01)
		dy = random.uniform(-.01, .01)
		return dx, dy
		
	def should_fire(self, tank):
		for enemy in self.enemies:
			target_angle = math.atan2(enemy.y - tank.y, enemy.x - tank.x)
			if abs(tank.angle - target_angle) < self.aimtolerance:
				return True
				
		return False

	def move_to_position(self, tank, target_x, target_y):
		"""Set command to move to given coordinates."""
		
		#get deltas
		delta_xG, delta_yG, magnitude = self.calculate_objective_delta(tank.x, tank.y, target_x, target_y)
		#delta_xO, delta_yO = self.calculate_obstacles_delta(tank.x, tank.y)
		delta_xO, delta_yO = 0, 0
		delta_xR, delta_yR = self.calculate_random_delta()
		#delta_xA, delta_yA = self.calculate_enemies_delta(tank.x, tank.y, self.enemies)
		
		#combine
		delta_x = delta_xG + delta_xO + delta_xR #+ delta_xA
		delta_y = delta_yG + delta_yO + delta_yR #+ delta_xA
		
		#calculate angle
		turn_angle = math.atan2(delta_y, delta_x)
		relative_angle = self.normalize_angle(turn_angle - tank.angle)
		
		#put lower bound on speed: no slower than 40%
		if magnitude < 0.4:
			magnitude = 0.4
			
		fire = self.should_fire(tank)
		
		command = Command(tank.index, magnitude, 2 * relative_angle, False)
		self.commands.append(command)
	
	def get_obstacle_force(self, obstacle, x, y):
		
		delta_x = 0 
		delta_y = 0
		
		big_num = 5000
		
		min_x = big_num
		max_x = -big_num
		min_y = big_num
		max_y = -big_num
		
		for p in obstacle:
			repel_x, repel_y = self.get_tangential_field(x, y, p)
			delta_x += repel_x
			delta_y += repel_y
			
			xp, yp = p
			if xp < min_x:
				min_x = xp
			elif xp > max_x:
				max_x = xp
			
			if yp < min_y:
				min_y = yp
			elif yp > max_y:
				max_y = yp
		
		center_point = [(min_x + min_y) / 2,  (min_y + max_y) / 2]
		
		repel_x, repel_y = self.get_repel_field(x, y, center_point[0], center_point[1]) # repel from the center of the box
		delta_x += repel_x
		delta_y += repel_y
				
		return delta_x, delta_y
	
	def get_tangential_field(self, x, y, p):
		my_x = x
		my_y = y
		xO, yO = p
		
		dist = math.sqrt( (my_x - xO)**2 + (my_y - yO)**2 )
		
		if dist < self.OBS_TOLERANCE:
			
			# angle between the tank and the obstacle
			theta = math.atan2(yO - y, xO - x) + math.pi / 2

			delta_x = -self.BETA * (self.avoidradius - dist) * math.cos(theta)
			delta_y = -self.BETA * (self.avoidradius - dist) * math.sin(theta)
			
			return delta_x, delta_y
		else:
			return 0, 0
	
	def get_repel_field(self, x, y, xO, yO):
		my_x = x
		my_y = y

		dist = math.sqrt( (my_x - xO)**2 + (my_y - yO)**2 )
		
		if dist < self.OBS_TOLERANCE:
			
			# angle between the tank and the obstacle
			theta = math.atan2(yO - y, xO - x)

			delta_x = -self.BETA * (self.avoidradius - dist) * math.cos(theta) # math.cos returns in radians
			delta_y = -self.BETA * (self.avoidradius - dist) * math.sin(theta)
			
			return delta_x, delta_y
		else:
			return 0, 0
	
	def normalize_angle(self, angle):
		"""Make any angle be between +/- pi."""
		angle -= 2 * math.pi * int (angle / (2 * math.pi))
		if angle <= -math.pi:
			angle += 2 * math.pi
		elif angle > math.pi:
			angle -= 2 * math.pi
		return angle
Пример #16
0
print("_" * 15 + "BAYES CLASSIFIER" + "_" * 15)
print_menu()
classifier = None

while (True):
    command = input("Enter command:")
    command = command.lower()
    #Train clause
    if command.startswith('t'):
        classifier = pp.main()
    #Load training clause
    elif command.startswith('l'):
        print("Loading: ", end='')
        if classifier is None:
            classifier = Bayes(trained=True)
        else:
            classifier.load()
    #Save training clause
    elif command.startswith('s'):
        print("Saving: ", end='')
        if classifier is not None:
            classifier.save()
        else:
            print("Nothing to save")
    #Classify clause
    elif command.startswith('c'):
        if classifier is None:
            print("Load training first")
        else:
            path = command.split(" ")
Пример #17
0
'''数据加载'''
data = pda.read_csv("./iris.csv")
'''标准化'''
data_standard = preprocessing.scale(data.iloc[:, :-1])
'''切分数据集,处理过拟合'''
train_data, test_data, train_labels, test_labels = train_test_split(
    data_standard,
    data.as_matrix()[:, -1],
    test_size=0.2,
    random_state=int(time.time()))
'''
贝叶斯算法识别
'''
print("---------------------------贝叶斯----------------------------------")
start = time.clock()
by = Bayes()
by.train(list(train_data), list(train_labels))
test_data_size = test_data.shape[0]
error_count = 0
for index, td in enumerate(list(test_data)):
    this_label = by.test(td)
    print("预测类别:{0},真实类别:{1}".format(this_label, test_labels[index]))
    if this_label != test_labels[index]:
        error_count += 1
end = time.clock()
error_rate = (error_count / test_data_size) * 100
time_consuming = end - start
print("错误率为:{0:.2f}%".format(error_rate))
print("耗时:{0:.4f}s".format(time_consuming))
'''
k-近邻算法识别
Пример #18
0
 def __init__(self):
     self.classifier = Bayes()
     self.seg = Seg()
     self.seg.load('seg.pickle')
Пример #19
0
class Agent(object):
    """Class handles all command and control logic for a teams tanks."""
    def __init__(self, bzrc):
        self.bzrc = bzrc
        self.constants = self.bzrc.get_constants()
        self.commands = []
        self.ALPHA = 0.01
        self.BETA = 0.3
        self.OBS_TOLERANCE = 35.0
        self.S = 50
        self.wroteonce = False
        self.goalradius = 30

        self.tankradius = 5
        self.avoidradius = 50
        self.avoidBETA = 0.05

        self.aimtolerance = math.pi / 20

        self.world_grid = WorldGrid()
        self.bayes = Bayes()

        self.turnprob = 0.05
        self.turndecisionprob = 0.5
        self.turned = False
        self.turniter = 0
        self.TURN_MAX = 50
        self.OCCUPIED = 1
        self.UNOCCUPIED = 0

    def tick(self, time_diff):
        """Some time has passed; decide what to do next."""
        mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff()
        self.mytanks = mytanks
        self.othertanks = othertanks
        self.flags = [
            flag for flag in flags if flag.color != self.constants['team']
        ]
        self.shots = shots
        self.enemies = [
            tank for tank in othertanks if tank.color != self.constants['team']
        ]
        #self.obstacles = self.bzrc.get_obstacles()
        self.commands = []

        for tank in mytanks:
            pos, grid = self.bzrc.get_occgrid(tank.index)
            self.update_priors(pos, grid)

        self.world_grid.draw_obstacle_grid()

        for tank in mytanks:
            self.goto_closest_target(tank)

        results = self.bzrc.do_commands(self.commands)

    def update_priors(self, position, grid):
        for x in range(len(grid)):
            for y in range(len(grid[x])):
                prior = self.world_grid.get_world_value(x, y, position)
                observation = grid[x][y]

                if observation == self.OCCUPIED:
                    new_prior = self.bayes.probability_occupied_given_observed(
                        prior)
                else:
                    new_prior = self.bayes.probability_occupied_given_not_observed(
                        prior)

                self.world_grid.set_world_value(x, y, position, new_prior)

    def move(self, mytanks):
        # it's not turning, so decide to turn or go straight
        if self.turned == False and self.turniter == 0:
            rand = random.random()  # returns a number [0, 1)
            if rand < self.turnprob:
                for tank in mytanks:
                    self.turn(tank)
                    self.turned = True
            else:
                for tank in mytanks:
                    self.go_straight(tank)

        else:  # it is turning
            self.turniter = self.turniter + 1

            if self.turniter >= self.TURN_MAX:
                self.turned = False
                self.turniter = 0

    def turn(self, tank):
        rand = random.random()  # returns a number [0, 1)
        if rand < self.turndecisionprob:
            command = Command(tank.index, 1, 1, False)  # turn right
            self.commands.append(command)
        else:
            command = Command(tank.index, 0.5, -1, False)  # turn left
            self.commands.append(command)

    def go_straight(self, tank):
        command = Command(tank.index, 1, 0, False)
        self.commands.append(command)

    def get_my_base(self):
        mybases = [
            base for base in self.bzrc.get_bases()
            if base.color == self.constants['team']
        ]
        mybase = mybases[0]
        return mybase

    def get_base_center(self, base):
        center_x = ((base.corner1_x + base.corner2_x + base.corner3_x +
                     base.corner4_x) / 4)
        center_y = ((base.corner1_y + base.corner2_y + base.corner3_y +
                     base.corner4_y) / 4)
        return center_x, center_y

    def goto_closest_target(self, tank):
        best_tar = self.get_best_target(tank.x, tank.y)
        if best_tar is None:
            command = Command(tank.index, 0, 0, False)
            self.commands.append(command)
        else:
            print "Tank :", tank.x, tank.y
            print "Target: ", best_tar
            self.move_to_position(tank, best_tar[0], best_tar[1])

    def get_best_target(self, x, y):
        best_tar = None
        best_dist = 2 * float(self.constants['worldsize'])
        for tar in self.world_grid.getTargetLocations():
            dist = math.sqrt((tar[0] - x)**2 + (tar[1] - y)**2)
            if dist < best_dist:
                best_dist = dist
                best_tar = tar
        return best_tar

    def goto_flags(self, tank):
        best_flag = self.get_best_flag(tank.x, tank.y)
        if best_flag is None:
            command = Command(tank.index, 0, 0, False)
            self.commands.append(command)
        else:
            self.move_to_position(tank, best_flag.x, best_flag.y)

    def get_best_flag(self, x, y):
        best_flag = None
        best_dist = 2 * float(self.constants['worldsize'])
        for flag in self.flags:
            dist = math.sqrt((flag.x - x)**2 + (flag.y - y)**2)
            if dist < best_dist:
                best_dist = dist
                best_flag = flag
        return best_flag

    def avoid_target(self, my_x, my_y, target_x, target_y):
        goal_dist = math.sqrt((target_x - my_x)**2 + (target_y - my_y)**2)
        target_angle = math.atan2(target_y - my_y, target_x - my_x)

        dx = 0
        dy = 0

        s = self.avoidradius
        r = self.tankradius

        if goal_dist < self.tankradius:
            dx = -1 * math.cos(target_angle) * 1000  #infinity
            dy = -1 * math.sin(target_angle) * 1000  #infinity
        elif goal_dist >= self.tankradius and goal_dist <= (s + r):
            dx = -1 * self.avoidBETA * (s + r -
                                        goal_dist) * math.cos(target_angle)
            dy = -1 * self.avoidBETA * (s + r -
                                        goal_dist) * math.sin(target_angle)
        else:
            dx = 0
            dy = 0
        return dx, dy

    def calculate_enemies_delta(self, my_x, my_y, enemies):
        delta_x = 0
        delta_y = 0

        for tank in enemies:
            dx, dy = self.avoid_target(my_x, my_y, tank.x, tank.y)
            delta_x += dx
            delta_y += dy

        sqnorm = math.sqrt(delta_x**2 + delta_y**2)

        if sqnorm == 0:
            delta_x = 0
            delta_y = 0
        else:
            delta_x = delta_x / sqnorm
            delta_y = delta_y / sqnorm

        return delta_x, delta_y

    def calculate_objective_delta(self, my_x, my_y, target_x, target_y):
        goal_dist = math.sqrt((target_x - my_x)**2 + (target_y - my_y)**2)
        target_angle = math.atan2(target_y - my_y, target_x - my_x)

        delta_xG = self.ALPHA * goal_dist * math.cos(target_angle)  # r = 0
        delta_yG = self.ALPHA * goal_dist * math.sin(target_angle)  # r = 0

        sqnorm = math.sqrt(delta_xG**2 + delta_yG**2)

        #set magnitude
        magnitude = 0
        if goal_dist > self.goalradius:
            magnitude = 1
        else:
            magnitude = sqnorm

        if sqnorm == 0:
            delta_xG = 0
            delta_yG = 0
        else:
            delta_xG = delta_xG / sqnorm
            delta_yG = delta_yG / sqnorm

        return delta_xG, delta_yG, magnitude

    """def calculate_obstacles_delta(self, x, y):
		delta_xO = 0
		delta_yO = 0
		
		for obs in self.obstacles:
			repel_xO, repel_yO = self.get_obstacle_force(obs, x, y)
			delta_xO += repel_xO
			delta_yO += repel_yO
		
		sqnorm = math.sqrt(delta_xO**2 + delta_yO**2)
			
		if sqnorm == 0:
			delta_xO = 0
			delta_yO = 0
		else:
			delta_xO = delta_xO / sqnorm
			delta_yO = delta_yO / sqnorm
		
		'''if delta_xG != 0 or delta_yG != 0:
			print "delta_xO: ", delta_xO
			print "delta_yO: ", delta_yO'''
			
		return delta_xO, delta_yO"""

    def calculate_random_delta(self):
        dx = random.uniform(-.01, .01)
        dy = random.uniform(-.01, .01)
        return dx, dy

    def should_fire(self, tank):
        for enemy in self.enemies:
            target_angle = math.atan2(enemy.y - tank.y, enemy.x - tank.x)
            if abs(tank.angle - target_angle) < self.aimtolerance:
                return True

        return False

    def move_to_position(self, tank, target_x, target_y):
        """Set command to move to given coordinates."""

        #get deltas
        delta_xG, delta_yG, magnitude = self.calculate_objective_delta(
            tank.x, tank.y, target_x, target_y)
        #delta_xO, delta_yO = self.calculate_obstacles_delta(tank.x, tank.y)
        delta_xO, delta_yO = 0, 0
        delta_xR, delta_yR = self.calculate_random_delta()
        #delta_xA, delta_yA = self.calculate_enemies_delta(tank.x, tank.y, self.enemies)

        #combine
        delta_x = delta_xG + delta_xO + delta_xR  #+ delta_xA
        delta_y = delta_yG + delta_yO + delta_yR  #+ delta_xA

        #calculate angle
        turn_angle = math.atan2(delta_y, delta_x)
        relative_angle = self.normalize_angle(turn_angle - tank.angle)

        #put lower bound on speed: no slower than 40%
        if magnitude < 0.4:
            magnitude = 0.4

        fire = self.should_fire(tank)

        command = Command(tank.index, magnitude, 2 * relative_angle, False)
        self.commands.append(command)

    def get_obstacle_force(self, obstacle, x, y):

        delta_x = 0
        delta_y = 0

        big_num = 5000

        min_x = big_num
        max_x = -big_num
        min_y = big_num
        max_y = -big_num

        for p in obstacle:
            repel_x, repel_y = self.get_tangential_field(x, y, p)
            delta_x += repel_x
            delta_y += repel_y

            xp, yp = p
            if xp < min_x:
                min_x = xp
            elif xp > max_x:
                max_x = xp

            if yp < min_y:
                min_y = yp
            elif yp > max_y:
                max_y = yp

        center_point = [(min_x + min_y) / 2, (min_y + max_y) / 2]

        repel_x, repel_y = self.get_repel_field(
            x, y, center_point[0],
            center_point[1])  # repel from the center of the box
        delta_x += repel_x
        delta_y += repel_y

        return delta_x, delta_y

    def get_tangential_field(self, x, y, p):
        my_x = x
        my_y = y
        xO, yO = p

        dist = math.sqrt((my_x - xO)**2 + (my_y - yO)**2)

        if dist < self.OBS_TOLERANCE:

            # angle between the tank and the obstacle
            theta = math.atan2(yO - y, xO - x) + math.pi / 2

            delta_x = -self.BETA * (self.avoidradius - dist) * math.cos(theta)
            delta_y = -self.BETA * (self.avoidradius - dist) * math.sin(theta)

            return delta_x, delta_y
        else:
            return 0, 0

    def get_repel_field(self, x, y, xO, yO):
        my_x = x
        my_y = y

        dist = math.sqrt((my_x - xO)**2 + (my_y - yO)**2)

        if dist < self.OBS_TOLERANCE:

            # angle between the tank and the obstacle
            theta = math.atan2(yO - y, xO - x)

            delta_x = -self.BETA * (self.avoidradius - dist) * math.cos(
                theta)  # math.cos returns in radians
            delta_y = -self.BETA * (self.avoidradius - dist) * math.sin(theta)

            return delta_x, delta_y
        else:
            return 0, 0

    def normalize_angle(self, angle):
        """Make any angle be between +/- pi."""
        angle -= 2 * math.pi * int(angle / (2 * math.pi))
        if angle <= -math.pi:
            angle += 2 * math.pi
        elif angle > math.pi:
            angle -= 2 * math.pi
        return angle
Пример #20
0
 def __init__(self, attributes, labels, traindata):
     Bayes.__init__(self, attributes, labels, traindata)
     self.edges = self.initializeTree()
     self.graph = self.findMaxSpanningTree()
Пример #21
0
    config = json.load(f)

# update the configuration for the new version of the data
config["csv"] = "test/weather_v2.csv"
config["inputs"] = data.drop(columns=["city", "date", "avg_temp"]).columns.tolist()

# test features
# config["inputs"] = None
# config["resolution"] = None
config["input_history"] = False

# In[2]: Model the data

# produce a bayesian ridge regression rolling forecast
print("---- Bayesian Ridge Regression ----")
model5 = Bayes(**config)
model5.roll(verbose=True)
print(f"Bayesian Average Error: {np.round(model5._error.mean()[0] * 100, 2)}%")

print("---- PLS ----")
model4 = PLS(**config)
model4.roll(verbose=True)
print(f"PLS Average Error: {np.round(model4._error.mean()[0] * 100, 2)}%")

# produce a neural network rolling forecast
print("---- Neural Network ----")
model3 = MLP(**config)
model3.roll(verbose=True)
print(f"NNet Average Error: {np.round(model3._error.mean()[0] * 100, 2)}%")

# produce a random forest rolling forecast
Пример #22
0
        return p
    elif val == 'T':
        return 1-p

'''Make a graph with 8 subplots that has the posterior for each of the following scenarios.
   Make sure to give each graph a title!
        * You get the data: H
        * You get the data: T
        * You get the data: H, H
        * You get the data: T, H
        * You get the data: H, H, H
        * You get the data: T, H, T
        * You get the data: H, H, H, H
        * You get the data: T, H, T, H'''

bayes_uniform = Bayes(prior_dict.copy(),likelihood_func=likelihood)
bayes_uniform.update('H')

fig, axs = plt.subplots(4,2, figsize=(14,8))
scenarios = ['H','T',['H','H'],['T','H'],['H','H','H'],['T','H','T'],['H','H','H','H'],['T','H','T','H']]
i = 1
for scenario, ax in zip(scenarios, axs.flatten()):
    bayes = Bayes(prior_dict.copy(),likelihood_func=likelihood)
    for flip in scenario:
        bayes.update(flip)
    bayes.plot(ax,title='Scenario #'+str(i)+': '+ ', '.join(scenario))
    i+=1
plt.tight_layout()
plt.show()

'''On a single graph, Use the coin.py random coin generator and overlay the initial uniform prior with the prior after 1, 2, 10, 50 and 250 flips..
Пример #23
0
def main():
    get_data('data/boy82.txt', 'boy')
    get_data('data/boy83.txt', 'boy')
    get_data('data/boynew.txt', 'boy')
    get_data('data/girl35.txt', 'girl')
    get_data('data/girl42.txt', 'girl')
    get_data('data/girlnew.txt', 'girl')
    test3 = []
    testhw = []
    tesths = []
    testws = []
    boys = open('data/boy.txt')
    girls = open('data/girl.txt')
    print("1) Bayes")
    print("2) Fisher")
    print("3) kNN")
    choice = input("Input the algorithm: ")
    if choice == '1':
        for line in boys.readlines():
            height, weight, shoe_size = line.split()
            test3.append([[float(height)], [float(weight)], [float(shoe_size)], 1])
            testhw.append([[float(height)], [float(weight)], 1])
            tesths.append([[float(height)], [float(shoe_size)], 1])
            testws.append([[float(weight)], [float(shoe_size)], 1])
        for line in girls.readlines():
            height, weight, shoe_size = line.split()
            test3.append([[float(height)], [float(weight)], [float(shoe_size)], 0])
            testhw.append([[float(height)], [float(weight)], 0])
            tesths.append([[float(height)], [float(shoe_size)], 0])
            testws.append([[float(weight)], [float(shoe_size)], 0])
        plt.xlim(0, 1.0)
        plt.ylim(0, 1.0)
        plt.plot([0, 1.0], [0, 1.0], color='red')
        b3 = Bayes(trains3)
        b3.paint(test3, 'b')
        bhw = Bayes(trainshw)
        bhw.paint(testhw, 'g')
        bhs = Bayes(trainshs)
        bhs.paint(tesths, 'r')
        bws = Bayes(trainsws)
        bws.paint(testws, 'y')
    elif choice == '2':
        for line in boys.readlines():
            height, weight, shoe_size = line.split()
            test3.append([[float(height)], [float(weight)], [float(shoe_size)], 1])
            testhw.append([[float(height)], [float(weight)], 1])
            tesths.append([[float(height)], [float(shoe_size)], 1])
            testws.append([[float(weight)], [float(shoe_size)], 1])
        for line in girls.readlines():
            height, weight, shoe_size = line.split()
            test3.append([[float(height)], [float(weight)], [float(shoe_size)], 0])
            testhw.append([[float(height)], [float(weight)], 0])
            tesths.append([[float(height)], [float(shoe_size)], 0])
            testws.append([[float(weight)], [float(shoe_size)], 0])
        f3 = Fisher(trains3)
        fhw = Fisher(trainshw)
        fhs = Fisher(trainshs)
        fws = Fisher(trainsws)
        print("1) ROC")
        print("2) Line")
        choice = input("Choose: ")
        if choice == '1':
            f3.paint(test3, 'b')
            fhw.paint(testhw, 'g')
            fhs.paint(tesths, 'r')
            fws.paint(testws, 'y')
        elif choice == '2':
            print("1) Height And Weight")
            print("2) Height And Shoe Size")
            print("3) Weight And Shoe Size")
            choice = input("Choose: ")
            if choice == '1':
                fhw.paint_line(testhw, 'c')
            elif choice == '2':
                fhs.paint_line(tesths, 'r')
            elif choice == '3':
                fws.paint_line(testws, 'y')
    elif choice == '3':
        for line in boys.readlines():
            height, weight, shoe_size = line.split()
            test3.append([float(height), float(weight), float(shoe_size), 1])
            testhw.append([float(height), float(weight), 1])
            tesths.append([float(height), float(shoe_size), 1])
            testws.append([float(weight), float(shoe_size), 1])
        for line in girls.readlines():
            height, weight, shoe_size = line.split()
            test3.append([float(height), float(weight), float(shoe_size), 0])
            testhw.append([float(height), float(weight), 0])
            tesths.append([float(height), float(shoe_size), 0])
            testws.append([float(weight), float(shoe_size), 0])
        choice = input("Input K:")
        if choice == '1':
            k1 = kNN(trainshs, 1)
            print(k1.test(tesths))
            k1.paint()
        elif choice == '3':
            k3 = kNN(trainshs, 3)
            print(k3.test(tesths))
            k3.paint()
        elif choice == '5':
            k5 = kNN(trainshs, 5)
            print(k5.test(tesths))
            k5.paint()
    plt.show()
Пример #24
0
 def load_model(self, model_path):
     self.model = Bayes.load(model_path)
Пример #25
0
        die  (int): number of sides of the die that produced the roll

    Returns:
        likelihood (float): the probability of the roll given the die.
    """
    if roll in range(1, die + 1):
        return 1 / die
    else:
        return 0


if __name__ == '__main__':
    uniform_prior = {4: .08, 6: .12, 8: .16, 12: .24, 20: .40}
    unbalanced_prior = {}

    die_bayes_1 = Bayes(uniform_prior.copy(), die_likelihood)
    experiment = [
        8, 2, 1, 2, 5, 8, 2, 4, 3, 7, 6, 5, 1, 6, 2, 5, 8, 8, 5, 3, 4, 2, 4, 3,
        8, 8, 7, 8, 8, 8, 5, 5, 1, 3, 8, 7, 8, 5, 2, 5, 1, 4, 1, 2, 1, 3, 1, 3,
        1, 5
    ]

    experiment2 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    experiment3 = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20]

    for idx, roll in enumerate(experiment3):
        print('roll#={} dic rolled={}'.format(idx + 1, roll))
        die_bayes_1.update(roll)
        die_bayes_1.print_distribution()
        #unbalanced_prior = die_bayes_1.prior
        print('*' * 32)
Пример #26
0
import pandas
from matplotlib import pyplot

from bayes import Bayes
from utils import generate_datasets

data = pandas.read_csv('./datasets/leaf.csv')
labels = data["species"]
data.drop(data.columns[-1], axis=1, inplace=True)

print(data.index)

for dataset in generate_datasets(data, labels):
    print('\n' + dataset.name)
    for training_percent in range(60, 91, 5):
        classifier = Bayes(dataset.data, labels, training_percent)
        classifier.train()
        classifier.test()
        dataset.result.append(classifier.get_accuracy())
        print('Training percent: ' + str(training_percent) + '%, accuracy: ' +
              str(classifier.get_accuracy()))
    pyplot.plot(range(60, 91, 5), dataset.result, label=dataset.name)

pyplot.xlabel('Training percent')
pyplot.ylabel('Accuracy')
pyplot.legend()
pyplot.savefig('plot', dpi=200, bbox_inches='tight')
Пример #27
0
model_now += 1

print("SVM 1训练中...")
svm = Svm(svm_label, svm_images, svm_test_label, svm_test_images)
results[model_now] = svm.train(numToClassfy, numToTrain, "-q -m 1000")
model_now += 1

print("SVM 2训练中...")
svm = Svm(svm_label, svm_images, svm_test_label, svm_test_images)
results[model_now] = svm.train(numToClassfy, numToTrain, "-q -m 1000 -t 3")
model_now += 1

knn = knn(10, images, label)
results[model_now] = knn.start(test_images, test_label, numToClassfy)
model_now += 1

bayes = Bayes(10, 784, images, label)
results[model_now] = bayes.start(test_images, test_label, numToClassfy)
model_now += 1

results = results.T
count = 0
for i in range(numToClassfy):
    progress = Progress(numToClassfy, "正在投票")
    k = np.argmax(np.bincount(results[i]))
    if (k == test_label[i]):
        count += 1
    progress.updata(i + 1)
print("集成识别完成,样本个数: " + str(numToClassfy) + " 识别数: " + str(count) +
      " 正确率: %.2f %%" % ((count / numToClassfy) * 100))
Пример #28
0
 def train_model(ngrams_file, output_file):
     model = Bayes()
     model.train(Ngrams._load_data(ngrams_file))
     model.serialize(output_file)
Пример #29
0
from bayes import Bayes

# First you need to create an instance of this
# algorithm and defined what field/column
# you want to classify
instance = Bayes("Sex")

# Secondly, you will need to learn about a set
# of data to train the algorithm
instance.learn("static/data_test.csv")

# Finally you can use your trained instance to
# classify a set of data (In this example we
# will find the most probable sex)
print(instance.classify([6, 130, 8]))
Пример #30
0
        return 0


uniform_prior = {4: 0.2, 6: 0.2, 8: 0.2, 12: 0.2, 20: 0.2}
unbalanced_prior = {4: 0.08, 6: 0.12, 8: 0.16, 12: 0.24, 20: 0.4}

d = [
    8, 2, 1, 2, 5, 8, 2, 4, 3, 7, 6, 5, 1, 6, 2, 5, 8, 8, 5, 3, 4, 2, 4, 3, 8,
    8, 7, 8, 8, 8, 5, 5, 1, 3, 8, 7, 8, 5, 2, 5, 1, 4, 1, 2, 1, 3, 1, 3, 1, 5
]

set1 = [1, 1, 1, 3, 1, 2]
set2 = [10, 10, 10, 10, 8, 8]

print('What are the posteriors if we started with the uniform prior?')
bayes_uniform = Bayes(uniform_prior.copy(), likelihood_func=likelihood_func)
bayes_uniform.update(8)
bayes_uniform.print_distribution()

print('What are the posteriors if we started with the unbalanced prior?')
bayes_unbalanced = Bayes(unbalanced_prior.copy(),
                         likelihood_func=likelihood_func)
bayes_unbalanced.update(8)
bayes_unbalanced.print_distribution()

print(
    'How different were these two posteriors (the uniform from the unbalanced)?'
)
for k, v in bayes_unbalanced.posterior.items():
    print("{} : {}".format(
        k, bayes_uniform.posterior[k] - bayes_unbalanced.posterior[k]))
Пример #31
0
from bayes import Bayes, URGENCIES
from sys import argv
import json

from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE, SIG_DFL)

f = open(argv[1], 'r')
json_str = f.read()
message_list = json.loads(json_str)
bae = Bayes(message_list)
bae.train()

test_data = open(argv[2], 'r')
test_data = test_data.read()
test_data = json.loads(test_data)


def prob_class(string, clazz):
    S = set(string.split())
    fv = bae.gen_feature_vector(S)
    return bae.prob_class(fv, clazz)


def main():
    tests = test_data

    out = {}
    for test in tests:
        o = {}
        for u in URGENCIES.keys():
Пример #32
0
from estudiante import Atributo,EstudianteBuilder

from bayes import Bayes
from random import shuffle
from setup import Setup

Setup.setup()

atributos = Setup.atributos
estudiantes = Setup.estudiantes
valoresPosibles = Setup.valoresPosibles

# Se genera una instancia del algoritmo pasandole el atributo objetivo
bayes = Bayes("G3", atributos, valoresPosibles)



# ------------------
# VALIDACION DE BAYES
# ------------------


# Se realizaran multiples pruebas para obtener una estimacion de la efectividad del algoritmo
# para cualquier eleccion de casos de entrenamiento / prueba
CANTIDAD_PRUEBAS = 100

# Separo 1/5 de los casos de prueba
cantEstTest = len(estudiantes)/5

shuffle(estudiantes)
from bayes import Bayes


# calculate validation accuracy
def accuracy(type, result):
    if type == 'ham':
        num = len(result) - sum(result)
        return float(num / len(result))
    elif type == 'spam':
        num = sum(result)
        return float(num / len(result))
    return 0

# now do the prediction, when you run the code change the file path to train_1, train_2, train_3, train_4, train_5, test_1....
ham_file = './ham/train_1.txt'
spam_file = './spam/train_1.txt'
test_file = './ham/test_1.txt'
train_matrix, class_labels, test_doc = Bayes.process_include_test_data(ham_file, spam_file, test_file)
test_file_2 = './spam/test_1.txt'
train_matrix_2, class_labels_2, test_doc_2 = Bayes.process_include_test_data(ham_file, spam_file, test_file_2)
result = Bayes.MyMultinomialNB(train_matrix, class_labels, test_doc)
result2 = Bayes.MyMultinomialNB(train_matrix_2, class_labels_2, test_doc_2)
print('accuracy of ham: ' + str(accuracy('ham', result)))
print('accuracy of spam: ' + str(accuracy('spam', result2)))
precision=len(test_doc_2)*accuracy('spam', result2)/(len(test_doc_2)*accuracy('spam', result2)+len(test_doc)*(1-accuracy('ham', result)))
recall = accuracy('spam', result2)
F_1=2*precision*recall/(precision+recall)
print('precision '+str(precision))
print('recall ' +str(recall))
print('F_1 '+str(F_1))
Пример #34
0
def main():
    #Making list of .txt-files (per sentiment)
    print("\tLOADING FILES")

    path = Path('..').joinpath('Data')
    test_ = path.joinpath('test')
    train = path.joinpath('train')

    tp_reviews = txtToList(test_.joinpath('pos'))
    tn_reviews = txtToList(test_.joinpath("neg"))
    pos_reviews = txtToList(train.joinpath("pos"))
    neg_reviews = txtToList(train.joinpath("neg"))
    print("\tFILES LOADED")

    #Cleaning reviews
    reviews = [pos_reviews, neg_reviews, tp_reviews, tn_reviews]
    print("\tCLEANING REVIEWS")
    for list_ in reviews:
        for i, review in enumerate(list_):
            list_[i] = clean_text(review)

    #Joining the reviews into one string (per sentiment)
    pos_string = "".join([string for string in pos_reviews])
    neg_string = "".join([string for string in neg_reviews])

    #Counting the frequency of words (per sentiment and total)
    posCounter = Counter(pos_string.split())
    negCounter = Counter(neg_string.split())
    vocabCounter = Counter(pos_string.split() + neg_string.split())

    for term in list(posCounter):
        if (posCounter[term] == 1):
            del posCounter[term]

    for term in list(negCounter):
        if (negCounter[term] == 1):
            del negCounter[term]

    classifier = Bayes(vocab_counts=vocabCounter)
    classifier.train(posCounter, negCounter)

    testSets = [tp_reviews, tn_reviews]
    n_pos_tp, n_neg_tp = 0, 0
    n_pos_tn, n_neg_tn = 0, 0

    for i, testSet in enumerate(testSets):
        print("_" * 15 + "RESULTS" + "_" * 15)
        n_pos, n_neg = 0, 0

        for review in testSet:
            pos, neg = classifier.test(review)
            if (pos >= neg):
                n_pos += 1
            else:
                n_neg += 1

        if (i == 0):
            print("Positive Testset: ")
            n_pos_tp, n_neg_tp = n_pos, n_neg
        else:
            print("Negative Testset: ")
            n_pos_tn, n_neg_tn = n_pos, n_neg

        print("Positive reviews: {}".format(n_pos))
        print("Negative reviews: {}".format(n_neg))

    pos_prec = n_pos_tp / (n_pos_tp + len(tn_reviews) - n_neg_tn)
    pos_rec = n_pos_tp / len(tp_reviews)
    pos_f1 = 2 * ((pos_prec * pos_rec) / (pos_prec + pos_rec))

    neg_prec = n_neg_tn / (n_neg_tn + len(tp_reviews) - n_pos_tp)
    neg_rec = n_neg_tn / len(tn_reviews)
    neg_f1 = 2 * ((neg_prec * neg_rec) / (neg_prec + neg_rec))

    scores = [pos_prec, pos_rec, pos_f1, neg_prec, neg_rec, neg_f1]

    save_stats(scores)
    print_stats(scores)

    return classifier