本文主要是介绍DataWhale-202110 树模型与集成学习(组队学习)-Task02,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
DataWhale-202110 树模型与集成学习-Task02
- CART代码实现
- 回归树
- 训练代码
- 代码运行结果
- 分类树
CART代码实现
回归树
import pandas as pd
import numpy as np
def MSE(y):return ((y - y.mean())**2).sum() / y.shape[0]class Node:def __init__(self, depth, idx):self.depth = depthself.idx = idxself.left = Noneself.right = Noneself.feature = Noneself.pivot = None
class Tree:def __init__(self, max_depth):self.max_depth = max_depthself.X = Noneself.y = Noneself.feature_importances_ = Nonedef _able_to_split(self, node):return (node.depth < self.max_depth) & (node.idx.sum() >= 2)def _get_inner_split_score(self, to_left, to_right):total_num = to_left.sum() + to_right.sum()left_val = to_left.sum() / total_num * MSE(self.y[to_left])right_val = to_right.sum() / total_num * MSE(self.y[to_right])return left_val + right_valdef _inner_split(self, col, idx):data = self.X[:, col]best_val = np.inftyfor pivot in data[:-1]:to_left = (idx==1) & (data<=pivot)to_right = (idx==1) & (~to_left)if to_left.sum() == 0 or to_left.sum() == idx.sum():continueHyx = self._get_inner_split_score(to_left, to_right)if best_val > Hyx:best_val, best_pivot = Hyx, pivotbest_to_left, best_to_right = to_left, to_rightreturn best_val, best_to_left, best_to_right, best_pivotdef _get_conditional_entropy(self, idx):best_val = np.inftyfor col in ran
这篇关于DataWhale-202110 树模型与集成学习(组队学习)-Task02的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!