Exemplo n.º 1
0
class NaiveBayesClassifier(object):

    def __init__(self):
        self.label_prob = ProbDist()
        self.label_feat_prob = defaultdict(ProbDist)
        self._fmap = FeatureMap()

    def print_features(self, label):
        for fid in self._fmap.keys():
            if (label, fid) in self.label_feat_prob:
                pdist = self.label_feat_prob[label, fid]
                for val in pdist.keys():
                    print "{0} {1} {2} {3}".format(label, fid, val,
                                                   pdist.prob(val))

    def train(self, label_features_set):
        label_freq_map = FrequencyMap()
        label_fid_freq_map = defaultdict(FrequencyMap)
        for label, features in label_features_set:
            # Count how often the label occurs.
            label_freq_map.inc(label)
            # Count how often a value for a given feature and label occurs.
            for f, val in features.iteritems():
                label_fid_freq_map[label, f].inc(val)
                # Record that we have seen that feature (for some label).
                self._fmap.get(f)

        for label in label_freq_map.keys():
            num_count = label_freq_map.freq(label)
            for f in self._fmap.keys():
                count = label_fid_freq_map[label, f].total()
                label_fid_freq_map[label, f].inc(None, num_count - count)

        # Compute the probabilities.
        for label in label_freq_map.keys():
            # P(label)
            probability = float(label_freq_map.freq(label))/label_freq_map.total()
            self.label_prob.set(label, probability)

        for ((label, f), freqmap) in label_fid_freq_map.items():
             for val in freqmap.keys():
                 p = float(freqmap.freq(val)) / float(freqmap.total())
                 self.label_feat_prob[label, f].set(val, p)

    def classify(self, features):
        features_copy = features.copy()
        for f in features_copy.keys():
            if f not in self._fmap.keys():
                del features_copy[f]
        prob_dist = ProbDist()

        for label in self.label_prob.keys():
            prob_dist.set(label, self.label_prob.logprob(label))
            for feat, val in features_copy.iteritems():
                if (label, feat) not in self.label_feat_prob:
                    print "ERROR"
                p_dist = self.label_feat_prob[label, feat]
                prob_dist.inc(label, p_dist.logprob(val))
        return prob_dist
def load_dataset():

    print "start loading go dataset files"

    # sgf에서 plays 추출
    file_names = get_file_names(GO_FOLDER, POSTFIX)
    for name in file_names:
        plays = Plays()
        plays.load_from_sgf(GO_FOLDER + "/" + name)
        splitted_name = name.split("_")
        play_num = int(splitted_name[1])
        features = FeatureMap(plays, play_num)
        f = open(
            OTHER_TRAIN_FOLDER + "/" + name + "_" + str(play_num) + "_x" +
            ".csv", 'w')
        txt = ""
        for feature in features.input_planes_valuenet:
            for rows in feature:
                for r in range(0, len(rows)):
                    if r == len(rows) - 1:
                        txt += str(rows[r])
                    else:
                        txt += str(rows[r]) + ","
                txt += "\n"
            txt += "\n"
        f.write(txt)
        f.close()
        f = open(
            OTHER_TRAIN_FOLDER + "/" + name + "_" + str(play_num) + "_y" +
            ".csv", 'w')
        cur_label = splitted_name[2][0]
        f.write(cur_label)
        f.close()

    print "finish.."
Exemplo n.º 3
0
    def getargumenttopredict(self):
        #print 'getargumenttopredict'

        #print 'getargumenttopredict : len(self.move_history) : ' + str(len(self.move_history))

        if len(self.move_history) == 0:
            #print 'len(self.move_history) == 0'
            return self.first_state()

        s = ""
        i = 1
        playlist = []
        for move in self.move_history:
            if i % 2 == 1:
                playlist.append(('b', (move[0] - 1, move[1] - 1)))
            else:
                playlist.append(('w', (move[0] - 1, move[1] - 1)))
            i = i + 1

        s = None
        try:
            plays = Plays(playlist)
            features = FeatureMap(plays, len(plays))
            #s = features.input_planes  # <-- use this value
            s = features.input_planes_policynet  # <-- use this value
        except Exception as e:
            print "Unexpected error in getargumenttopredict : ", sys.exc_info(
            )[0]
            print e
            pass

        return s
Exemplo n.º 4
0
def load_dataset():
    
    print "start"
    
    num_saves = 0
    file_names = get_file_names(SGF_FOLDER, POSTFIX)
    for name in file_names:
        plays = Plays()
        plays.load_from_sgf(SGF_FOLDER+"/"+name)
        if plays.total_nonpass_plays < 10:
            continue
        for play_num in range(0, plays.total_plays+1):
            features = FeatureMap(plays, play_num)
            if type(features) is NoneType:
                continue
            f = open(SAVE_FOLDER+"/"+name+"_"+str(play_num)+"_x"+".csv", 'w')
            txt = ""
            inputs = features.input_planes_policynet
            for feature in inputs:
                for rows in feature:
                    for r in range(0, len(rows)):
                        if r == len(rows)-1:
                            txt += str(rows[r])
                        else:
                            txt += str(rows[r]) + ","
                    txt+="\n"
                txt+="\n"
            f.write(txt)
            f.close()
            
            txt = ""
            f = open(SAVE_FOLDER+"/"+name+"_"+str(play_num)+"_y"+".csv", 'w')
            for rows in features.label:
                for r in range(0, len(rows)):
                    r_value = rows[r]
                    if r == len(rows)-1:
                        txt += str(r_value)
                    else:
                        txt += str(r_value) + ","
                txt+="\n"
            f.write(txt)
            f.close()
        num_saves += 1
        if num_saves % 100 == 0:
            print "finish "+str(num_saves)+" sgf files.."

    print "finish all.."
Exemplo n.º 5
0
    def getargumenttopredictvalue(self):
        #print 'getargumenttopredictvalue'

        #print 'getargumenttopredictvalue : len(self.move_history) : ' + str(len(self.move_history))

        if len(self.move_history) == 0:
            #print 'len(self.move_history) == 0'
            return self.first_statevalue()

        #print self.move_history

        s = ""
        i = 1
        playlist = []
        for move in self.move_history:
            if i % 2 == 1:
                playlist.append(('b', (move[0] - 1, move[1] - 1)))
            else:
                playlist.append(('w', (move[0] - 1, move[1] - 1)))
            i = i + 1

        s = None
        try:
            #print 'ZZ01'
            plays = Plays(playlist)
            #print 'ZZ02'
            features = FeatureMap(plays, len(plays))
            #print 'ZZ03'
            s = features.input_planes_valuenet  # <-- use this value
            #print 'ZZ04'

            #print_features(s)
            #pprint.pprint(s)

            #print 'ZZ05'

        except Exception as e:
            print "Unexpected error in getargumenttopredictvalue : ", sys.exc_info(
            )[0]
            print e
            #print "Unexpected error in getargumenttopredictvalue : ", sys.exc_info()[0]
            pass

        return s
Exemplo n.º 6
0
 def __init__(self):
     self.label_prob = ProbDist()
     self.label_feat_prob = defaultdict(ProbDist)
     self._fmap = FeatureMap()
Exemplo n.º 7
0
 game.make_move((2, 2))
 print str(game.current_board)
 
 
 #make argument to call model.predict
 i = 1
 playlist = []
 for move in game.move_history:
     if i%2 == 1:
         playlist.append(('b', (move[0]-1, move[1]-1)))
     else:
         playlist.append(('w', (move[0]-1, move[1]-1)))     
     i = i + 1
  
 plays = Plays(playlist)
 features = FeatureMap(plays, len(plays))
 print "first : "
 print features.input_planes  # <-- use this value
 
 #another way
 print "second : "
 print game.getargumenttopredict()
 
 
 #mcts test. reference only interface. inner logic will be modified.
 root = StateNode(None, GoState(game))
 mcts = MCTS(tree_policy=UCB1(c=1.41), 
             default_policy=evaluation,
             backup=monte_carlo)
 
 best_action = mcts(root)    
Exemplo n.º 8
0
        start = True
        m = 0
        while m < rand_move_number:
            #make argument to call model.predict
            playlist = []
            n = 1
            for move in game.move_history:
                if n % 2 == 1:
                    playlist.append(('b', (move[0] - 1, move[1] - 1)))
                else:
                    playlist.append(('w', (move[0] - 1, move[1] - 1)))
                n += 1

            plays = Plays(playlist)
            features = FeatureMap(plays, len(playlist))

            X = features.input_planes_policynet

            pred_pos = sl_policy.predict(np.asarray([X], dtype=np.float))

            tmp_pred_pos = np.argmax(pred_pos)
            col, row = get_state(tmp_pred_pos)

            while not game.legal_move((col, row)):
                pred_pos = np.delete(pred_pos, tmp_pred_pos)
                if len(pred_pos) == 0:
                    start = False
                    break
                tmp_pred_pos = np.argmax(pred_pos)
                col, row = get_state(tmp_pred_pos)