【问题标题】:I want to implement a tree-based regression model using rss我想使用 rss 实现基于树的回归模型
【发布时间】:2019-10-13 16:46:13
【问题描述】:

1.2 RSS 正如教科书第 8 章所述,我们需要一个参数来衡量特定拆分/树的效率。我们在这里选择残差平方和。对拆分列表执行 RSS 计算,知道要预测的值 (Wage(k)) 包含在拆分中元素的元素 [-1] 中。 您可以使用实现下方的代码单元来检查特定拆分的结果。

1.3 拆分 我们将编写一个拆分函数,该函数能够根据特征索引、拆分值和数据将数据拆分为两部分。执行拆分条件,以左为约定

1.4 最佳拆分创建 没有理论上的结果允许在遍历所有可能的拆分之前找到最好的拆分,因此我们在整个拆分上实现了一个 RSS 最小化器。使用以前编码的函数,填写#TODO 部分。您可以在以下单元格中查看您的退货。

1.5 树的构建和预测 聚合代码的所有部分现在允许我们递归地构建整个树。注释给定的代码,尤其是参数 min_size 对模型结构的重要性 使用相同的编码范例允许我们使用我们的模型对测试集进行回归,正如您在下一个代码单元中看到的那样。现在缺少全球模型的哪一部分?解释它在真实机器学习问题中的重要性。 (奖励)实施它

我想使用 rss 实现基于树的回归模型。我想填下面的空白但是太难了

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math

data = pd.read_csv("Wages.csv", sep=";")

training_set = np.array(data[:10])
test_set = np.array(data[10:])


-- RSS --

verbose = False
def RSS(splits):
    """
    Return the RSS of the input splits. The input should be in the form 
of a list of list
    """
    residual = 0
    for split in splits:
        if(len(split) != 0):
            mean = mean(split[:-1])
            if(verbose):print("Mean :" + str(mean))
            residual = ##TODO
    return residual

split_1 = np.array([[[0,2],[0,8]],[[4,5]]])
RSS_value = RSS(split_1)
if (type(RSS_value) not in [int,float,np.float16,np.float32,np.float64]):
    print("TypeError : check your output")
elif(RSS(split_1) == 18.0):
    print("Your calculations are right, at least on this specific 
example")
else:
    print("Your calculations are wrong")


-- Split --

def split(index, value, data):
    """
    Splits the input @data into two parts, based on the feature at @index 
position, using @value as a boundary value
    """
    left_split = #TODO condition
    right_split = #TODO condition
    return [left_split, right_split]


-- optimal split creation

def split_tester(data):
    """
    Find the best possible split possible for the current @data.
    Loops over all the possible features, and all values for the given 
features to test every possible split
    """
optimal_split_ind, optimal_split_value, optimal_residual, optimal_splits = -1,-1,float("inf"),[] #Initialize such that the first split is better than initialization
for curr_ind in range(data.shape[1]-1):
    for curr_val in data:
        if(verbose):print("Curr_split : " + str((curr_ind, curr_val[curr_ind])))
        split_res = #TODO (comments : get the current split)

        if(verbose):print(split_res)
        residual_value = #TODO (comments : get the RSS of the current split)

        if(verbose):print("Residual : " + str(residual_value))
        if residual_value < optimal_residual:
            optimal_split_ind, optimal_split_value, optimal_residual, optimal_splits = curr_ind,\
                                                                curr_val[curr_ind], residual_value, split_res

return optimal_split_ind, optimal_split_value, optimal_splits



-- tree building --


def tree_building(data, min_size):
    """
    Recursively builds a tree using the split tester built before.
    """
    if(data.shape[0] > min_size):
        ind, value, [left, right] = split_tester(data)
        left, right = np.array(left), np.array(right)
        return [tree_building(left, min_size), tree_building(right, 
min_size),ind,value]
    else:
        return data


tree = tree_building(training_set,2)




def predict(tree, input_vector):
    if(type(tree[-1]) != np.int64):
        if(len(tree) == 1):
            return(tree[0][-1])
        else:
            return(np.mean([element[-1] for element in tree]))
    else:
        left_tree, right_tree, split_ind, split_value = tree
        if(input_vector[split_ind]<split_value):
            return predict(left_tree, input_vector)
        else:
            return predict(right_tree, input_vector)



for employee in test_set:
    print("Predicted : " + str(predict(tree,employee)) + ", Actual : " + 
str(employee[-1]))

我正在研究获取#TODO 的代码。我不知道。请帮帮我。

【问题讨论】:

标签: python tree regression decision-tree


【解决方案1】:

如果我理解正确,您只是要求在您发布的代码中标记#TODO 的计算。如果您计算模型预测的误差,这些误差值有时称为“残余误差”。您不能简单地将这些相加,有些是负数,有些是正数,因此它们可能会相互抵消。但是,如果误差都是平方的,那么平方误差都是正值并且可以相加。这就是术语“残差平方和”(RSS)的来源。你可以使用类似“RSS = numpy.sum(numpy.square(errors))”的东西来计算这个值。

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 2016-11-04
    • 2017-07-20
    • 2012-07-20
    • 2014-07-22
    • 2015-01-26
    • 2012-10-03
    • 1970-01-01
    相关资源
    最近更新 更多