Exemple #1
0
class DecisionTreeAsWeakClassifier(WeakClassifier):
    def __init__(self, model, label_mapping):
        self.classifier = DecisionTreeModel()
        self.classifier.set_max_depth(2)
        self.model = model
        self.label_mapping = label_mapping

    def fit(self, data):
        # print(data)
        self.classifier.fit(data=data, algo_model=self.model)

    def predict(self, X):
        pre = self.classifier.predict(X)
        return np.array([self.label_mapping.get(y) for y in pre])

    def mapping_label(self, Y):
        return np.array([self.label_mapping.get(x) for x in Y])
Exemple #2
0
    ## wine dataset
    # data = np.loadtxt(ROOT_DIR+"/../wine/wine_data.csv", dtype=str, delimiter=",", skiprows=0)

    ## iris dataset
    # data = np.loadtxt(ROOT_DIR+"/../iris/iris_data.csv", dtype=str, delimiter=",", skiprows=0)

    ## car dataset
    data = np.loadtxt(ROOT_DIR + "/../car/car_data.csv",
                      dtype=str,
                      delimiter=",",
                      skiprows=0)

    data = data[:, 1:]

    print("... no-pruning ...")
    model = DecisionTreeModel()
    model.fit(data=data, algo_model="cart", split_ratio="9:1")
    # print(model.root["feature"])
    # print(json.dumps(model.root, indent=4, ensure_ascii=False))
    dot = model.export_graphviz()
    report = model.report_performances()
    print(report)

    ## pre_pruning
    print("... pre-pruning ...")
    pre_pruning_model = DecisionTreeModel()
    pre_pruning_model.fit(data=data,
                          algo_model="cart",
                          split_ratio="9:1",
                          prune="pre")
    pre_pruning_dot = pre_pruning_model.export_graphviz(dot)
Exemple #3
0
import os
ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(ROOT_DIR+"/../Model")
from decision_tree_model import DecisionTreeModel
import numpy as np
import json
import graphviz
from sklearn import tree

if __name__ == "__main__":
  # data = np.loadtxt(ROOT_DIR+"/../watermelon/watermelon_2.0.csv", dtype=str, delimiter=",", skiprows=0)
  data = np.loadtxt(ROOT_DIR+"/../watermelon/watermelon_3.0.csv", dtype=str, delimiter=",", skiprows=0)
  # data = np.loadtxt(ROOT_DIR+"/../watermelon/watermelon_3.0_x.csv", dtype=str, delimiter=",", skiprows=0)
  data = data[:,1:]
  # print(data)
  model = DecisionTreeModel()
  model.fit(data=data, algo_model="id3")
  # print(model.root["feature"])
  # print(json.dumps(model.root, indent=4, ensure_ascii=False))

  ## predict train
  x = data[1:,:-1]
  y = data[1:,-1]
  # x = x[8:9,:]
  # y = y[8:9]

  print("---------------my own.DecisionTreeClassifier----------------")
  predictions = model.predict(x)
  print("Y:{}".format(",".join(y)))
  print("Predictions:{}".format(",".join(predictions)))
  print("train accuracy:{}".format(model.accuracy(y, predictions)))
import json

if __name__ == "__main__":
    data = np.loadtxt(ROOT_DIR + "/../watermelon/watermelon_2.0_x.csv",
                      dtype=str,
                      delimiter=",",
                      skiprows=0)
    # data = np.loadtxt(ROOT_DIR+"/../wine/wine_data.csv", dtype=str, delimiter=",", skiprows=0)
    data = data[:, 1:]
    # print(data)
    # model = DecisionTreeModel()
    # model.fit(data=data, algo_model="id3")
    # model.fit(data=data, algo_model="cart")
    # model.fit(data=data, algo_model="cart", split_ratio="watermelon")
    # model.fit(data=data, algo_model="cart", split_ratio="9:1")
    # print(model.root["feature"])
    # print(json.dumps(model.root, indent=4, ensure_ascii=False))

    # dot = model.export_graphviz()
    # dot.render(view=True, cleanup=True)

    ## post_pruning
    post_pruning_model = DecisionTreeModel()
    post_pruning_model.fit(data=data,
                           algo_model="cart",
                           split_ratio="watermelon",
                           prune="post")

    post_pruning_dot = post_pruning_model.export_graphviz()
    post_pruning_dot.render(view=True, cleanup=True)
Exemple #5
0
ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(ROOT_DIR + "/../Model")
from decision_tree_model import DecisionTreeModel
import numpy as np
import json

if __name__ == "__main__":
    data = np.loadtxt(ROOT_DIR + "/../watermelon/watermelon_2.0_x.csv",
                      dtype=str,
                      delimiter=",",
                      skiprows=0)
    # data = np.loadtxt(ROOT_DIR+"/../watermelon/watermelon_3.0_x.csv", dtype=str, delimiter=",", skiprows=0)
    # data = np.loadtxt(ROOT_DIR+"/../wine/wine_data.csv", dtype=str, delimiter=",", skiprows=0)
    data = data[:, 1:]
    # print(data)
    model = DecisionTreeModel()
    # model.fit(data=data, algo_model="id3")
    # model.fit(data=data, algo_model="cart")
    model.fit(data=data, algo_model="cart", split_ratio="watermelon")
    # model.fit(data=data, algo_model="cart", split_ratio="9:1")
    # print(model.root["feature"])
    print(json.dumps(model.root, indent=4, ensure_ascii=False))

    ## predict train
    x = data[1:, :-1]
    y = data[1:, -1]
    predictions = model.predict(x)
    print(",".join(predictions))
    print(",".join(y))
    print("train accuracy:{}".format(model.accuracy(y, predictions)))
Exemple #6
0
 def __init__(self, model, label_mapping):
     self.classifier = DecisionTreeModel()
     self.classifier.set_max_depth(2)
     self.model = model
     self.label_mapping = label_mapping