Object()不带参数

时间:2018-11-15 08:52:07

标签: python-3.x

当尝试执行以下代码时,我得到“ TypeError:Question()不带参数”(其中Question是Question类的对象)。

我正在使用jupyter笔记本,并且已经检查了大多数缩进,该类Question具有2个属性,我有一个init方法...

问题与下面的功能find_best_split(rows)相关。

我添加了错误控制台输出here

的图像
    class Question:

    def __init__(self, column, value):
        self.column = column
        self.value = value


    def match(self, example):
        val = example[self.column]
        if is_numeric(val):
            return val >= self.value
        else:
            return val == self.value


    def __repr__(self):
        condition = "=="
        if is_numeric(self.value):
            condition = ">="
        return "Is %s %s %s?" % (header[self.column], condition, str(self.value))


    def partition(rows, question):

        true_rows, false_rows = [], []
        for row in rows:
            if question.match(row):
                true_rows.append(row)
            else:
                false_rows.append(row)
        return true_rows, false_rows

`错误指向此函数,特别是“ question = Question(col,val)”

    def find_best_split(rows):
        best_gain = 0
        best_question = None
        current_uncertainty = gini(rows)
        n_features = len(rows[0]) -1 # number of columns

        for col in range(n_features):
            values = set([row[col] for row in rows]) # unique values in the column
            for val in values: #now for each value
                question = Question(col, val)
                # trying to split the data set
                true_rows, false_rows = partition(rows, question)
                # skips this split if it doesn't divide the data set.
                if len(true_rows) == 0 or len(false_rows) == 0:
                    continue    
                # calculate the information gain from this split
                gain = info_gain(true_rows, false_rows, current_uncertainty)

                # you can use > instead of >= below but I wanted the data set to look a certain way for this example.
                if gain >= best_gain:
                    best_gain, best_question = gain, question
            return best_gain, best_quesiton

`

    class Decision_Node:

    def __init__(self, question, true_branch, false_branch):
            self.question = question
            self.true_branch = true_branch
            self.false_branch = false_branch



    def build_tree(rows):
        gain, question = find_best_split(rows)
        if gain == 0:
            return Leaf(rows)


        true_rows, false_rows = partition(rows, question)

        true_branch = build_tree(true_rows)

        false_branch = build_tree(false_rows)



        return Decision_Node(question, true_branch, false_branch)




    if __name__ == '__main__':   

        my_tree = build_tree(training_data)

        print_tree(my_tree)

        # Evaluate
        testing_data = [
            ["Green", 3, "Mango"],
            ["Yellow", 4, "Mango"],
            ["Red", 2, "Grape"],
            ["Red", 1, "Grape"],
            ["Yellow", 3, "Lemon"],
        ]

        for row in testing_data:
         print ("Actual: %s. Predicted: %s" % (row[-1], print_leaf(classify(row, my_tree))))

0 个答案:

没有答案