Exemplo n.º 1
0
def start_battle(browser, tree):
    # Load the poke objects
    poke_dict = load_pokemon()
    move_dict = load_moves()

    wait_load(browser, "chooseTeamPreview")
    battle_string = get_battle_log(browser)

    current, lines, p1_poke, p2_poke = pre_search.get_pre_battle(battle_string)  # Get pre-battle info
    populate_pokes(p1_poke, p2_poke, poke_dict)  # Compute unknown stats from Smogon EVs

    start = -1
    current_mon = [None, None]
    while True:
        state = get_move_options(browser)
        append_string = get_battle_log(browser)
        if start < 1:
            if start == 0:
                current, current_mon = get_starting_mon(0, append_string.split('\n'))
            start += 1
        else:
            p1_poke, p2_poke, current_mon = simulate_turn(append_string, current_mon, p1_poke, p2_poke)
        if state == "selectMove":
            arr, options = get_game_state(current_mon, p1_poke, p2_poke, move_dict, poke_dict, False)
            arr = move_tree.convert_numeric(arr)
            node = dtree_build.classify(arr, tree)  # Classify the row
            l = get_class_list(node)
            move = recommend_move(l, options, p2_poke[current_mon[1]], move_dict, p2_poke, poke_dict)
            print(move)
            if move == p2_poke[current_mon[1]].name or move is None:
                click_button(browser, "selectMove")
                click_button(browser, "chooseMove")
            else:
                use_move(move, browser, move_dict)
        elif state == "chooseSwitch":
            arr, options = get_game_state(current_mon, p1_poke, p2_poke, move_dict, poke_dict, True)
            arr = move_tree.convert_numeric(arr)
            node = dtree_build.classify(arr, tree)  # Classify the row
            l = get_class_list(node)
            move = recommend_move(l, options, p2_poke[current_mon[1]], move_dict, p2_poke, poke_dict)
            print(move)
            use_move(move, browser, move_dict)
        elif state == "chooseTeamPreview":
            click_button(browser, "chooseTeamPreview")
Exemplo n.º 2
0
def main(col_names=None):
    if len(sys.argv) < 2:
        print("Please specify input csv file name")
        return

    csv_file_name = sys.argv[1]
    data = []
    with open(csv_file_name) as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        for row in readCSV:
            data.append(list(row))

    train = resample(data[1:], replace=True, n_samples=int(len(data)))
    test = []
    for i in data[1:]:
        if i not in train:
            test.append(i)
    tree = dtree_build.buildtree(train, min_gain=0.01, min_samples=5)

    dtree_build.printtree(tree, '', col_names)

    result2 = naive_bayes.build(train)
    # print(result2)
    # max_tree_depth = dtree_build.max_depth(tree)
    # print("max number of questions=" + str(max_tree_depth))

    # print(test)
    out_put = [['instance', 'actual', 'predicted', 'probability']]
    total = 0
    correct = 0
    correct2 = 0
    for i in test:
        total += 1
        result = dtree_build.classify(i, tree)
        out = naive_bayes.classifier(result2, i)
        sum_probability = 0
        max_number = 0
        choice = ''
        for n, m in result.items():
            sum_probability += m
            if m >= max_number:
                max_number = m
                choice = n
        if choice == i[-1]:
            correct += 1
        if out == int(i[-1]):
            correct2 += 1
        sublist = [total, i[-1], choice, max_number / sum_probability]
        out_put.append(sublist)

        # print(result)
    with open("predicted.csv", "w") as output:
        writer = csv.writer(output)
        writer.writerows(out_put)
    print("Accuracy for decision tree is", correct / len(test))
    print("Accuracy for naive bayes is", correct2 / len(test))
Exemplo n.º 3
0
def classify_pokemon(tree, test_f, output_f):
    entries = tsv.get_list(test_f)
    output = open(output_f, "w")

    for entry in entries:
        poke = entry.pop(0)
        entry = move_tree.convert_numeric(entry)
        node = dtree_build.classify(entry, tree)  # Classify the row

        poke_class = ""
        for k in node:  # There are no non-pure nodes, so this works.
            poke_class = k

        output.write(poke+ '\t' + poke_class + '\n')  # Store as (move name, class) in .tsv
Exemplo n.º 4
0
def classify_moves(tree, test_f, output_f):
    test_file = open(test_f)
    output = open(output_f, "w")

    for line in test_file:
        arr = line.rstrip().split('\t')
        move_name = arr.pop(0)
        entry = convert_numeric(arr)
        node = dtree_build.classify(entry, tree)  # Classify the row

        move_class = ""
        for k in node:  # There are no non-pure nodes, so this works.
            move_class = k

        output.write(move_name + '\t' + move_class + '\n')  # Store as (move name, class) in .tsv

    test_file.close()
    output.close()
def main(col_names=None):
    # parse command-line arguments to read the name of the input csv file
    # and optional 'draw tree' parameter
    if len(sys.argv) < 2:  # input file name should be specified
        print ("Please specify input csv file name")
        return

    csv_file_name = sys.argv[1]

    data = []
    with open(csv_file_name) as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        for row in readCSV:
            list = []
            for attribute in row:
                try:
                    list += [float(attribute)]
                except:
                    list += [attribute]
            data.append(list)

    print("Total number of records = ",len(data))
    tree = regression_tree.buildtree(data, min_gain = 0.005, min_samples = 5)

    regression_tree.printtree(tree, '', col_names)

    max_tree_depth = dtree_build.max_depth(tree)
    print("max number of questions=" + str(max_tree_depth))

    if len(sys.argv) > 2: # draw option specified
        import regression_draw
        regression_draw.drawtree(tree, jpeg=csv_file_name+'.jpg')

    if len(sys.argv) > 3:  # create json file for d3.js visualization
        import json
        import dtree_to_json
        json_tree = dtree_to_json.dtree_to_jsontree(tree, col_names)
        print(json_tree)

        # create json data for d3.js interactive visualization
        with open(csv_file_name + ".json", "w") as write_file:
            json.dump(json_tree, write_file)

    print("course ['teaching', 'not minority', 'female', 'english', 50, 30, 'lower', 7, 4] is: ", dtree_build.classify(['teaching', 'not minority', 'female', 'english', 50, 30, 'lower', 7, 4], tree))
    print("course ['teaching', 'not minority', 'male', 'english', 40, 30, 'lower', 6, 4] is: ", dtree_build.classify(['teaching', 'not minority', 'male', 'english', 40, 30, 'lower', 6, 4], tree))
    print("course ['teaching', 'not minority', 'male', 'english', 70, 30, 'lower', 4, 4] is: ", dtree_build.classify(['teaching', 'not minority', 'male', 'english', 70, 30, 'lower', 4, 4], tree))
import dtree_build
import sys

if __name__ == "__main__":
    # fruits with their size and color
    fruits = [[4, 'red', 'apple'], [4, 'green', 'apple'], [1, 'red', 'cherry'],
              [1, 'green', 'grape'], [5, 'red', 'apple']]

    tree = dtree_build.buildtree(fruits)
    dtree_build.printtree(tree, '', ["size", "color"])
    print("fruit [2, 'red'] is: ", dtree_build.classify([2, 'red'], tree))
    print("fruit [4.5, 'red'] is: ", dtree_build.classify([4.5, 'red'], tree))
    print("fruit [1.4, 'green'] is: ",
          dtree_build.classify([1.4, 'green'], tree))

    max_tree_depth = dtree_build.max_depth(tree)
    print("max number of questions=" + str(max_tree_depth))
    if len(sys.argv) > 1:  # draw option specified
        import dtree_draw
        dtree_draw.drawtree(tree, jpeg='fruits_dt.jpg')