import numpy as np
import pandas as pd
from sklearn.metrics import f1_score
from sklearn.model_selection import KFold

from read_data import connect4
from data_label_num import data_label_num, combination
from decision_tree import decision_tree


if __name__ == '__main__':
    dataset = connect4(numerical=True, one_hot=False)
    model_data, model_label, train_dataset_num = data_label_num(dataset.train_x, dataset.train_y)
    test_data, test_label, _ = data_label_num(dataset.test_x, dataset.test_y)

    macro_f1_score = []
    kf = KFold(n_splits=5, shuffle=True, random_state=2020)
    for train_index, valid_index in kf.split(train_dataset_num):
        train_data = pd.DataFrame(train_dataset_num.copy()).drop(valid_index)
        valid_data = pd.DataFrame(train_dataset_num.copy()).drop(train_index)

        train_data_y = train_data.iloc[:, -1]
        valid_data_y = valid_data.iloc[:, -1]
        train_data_x = train_data.drop(axis=1, columns=42)
        valid_data_x = valid_data.drop(axis=1, columns=42)

        train_dataset = combination(train_data_x.values.tolist(), train_data_y.values.tolist())
        dt = decision_tree(max_depth=6)
        dt.train(train_dataset)
        
        valid_dataset = combination(valid_data_x.values.tolist(), valid_data_y.values.tolist())
Exemple #2
0
            result = "win"
        elif feature[20] == "o":
            if feature[21] == "x":
                result = "win"
            else:
                result = "loss"
        else:
            if feature[19] == "o":
                result = "loss"
            else:
                result = "win"
    return result


if __name__ == "__main__":
    dataset = connect4()
    data = dataset.get_all_data()

    # 计算if-else在数据集上的正确率
    right = 0
    res_ifelse = []
    for i in range(len(data[0])):
        feature = data[0][i]
        pred = predict(feature)
        res_ifelse.append(pred)
        if pred == data[1][i]:
            right += 1
    print("if-else正确率:", right / len(data[0]))

    # 计算决策树在数据集上的正确率
    right = 0