知识点:
2.1 预剪枝
预剪枝是在构建决策树的过程中,提前停止使模型性能变差的分支
预剪枝方法:
1.当树的深度达到一定的规模,停止生长。
2.当前节点的样本数量小于某个阈值,停止生长。
3.当信息增益,信息增益率和基尼指数增益小于某个阈值,停止生长。
4.当测试集准确性提升小于某个阈值,或不再提升甚至有所下降时,停止生长。(西瓜书)
预剪枝方法能有效提升模型性能,并减少训练时间和测试时间;但这种方法采用的是贪心本质,存在欠拟合风险。
2.2 后剪枝
后剪枝是在决策树构建完成后,开始自下而上地剪枝
后剪枝方法:
1.悲观剪枝(PEP)
2.最小误差剪枝(MEP)
3.错误率降低剪枝(REP),(西瓜书)
4.代价复杂度剪枝(CCP)
5.OPP (Optimal Pruning)
6.CVP (Critical Value Pruning)
版权声明:本文为CSDN博主「贪心西瓜」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43217427/article/details/109848058
题目描述:
你是一位施工队的工长,根据设计师的要求准备为一套设计风格独特的房子进行室内装修。
房子的客厅大小为 n x m,为保持极简的风格,需要使用尽可能少的正方形 瓷砖来铺盖地面。
假设正方形瓷砖的规格不限,边长都是整数。
请你帮设计师计算一下,最少需要用到多少块方形瓷砖?
示例:
解题思路:
大佬的解题思路是(我直接搬过来了):
1.检查当前答案是否大于等于当前最佳答案,若是,则进行剪枝,回溯
2.检查正方形中是否有空位,若无空位,更新答案并回溯
3.在查找到的第一个空位,进行填充,填充的规则是由大瓷砖到小瓷砖进行尝试(从小到大会导致运行时间大大增加)。 *若尝试失败(部分位置可能已经被其它瓷砖占据),则更换成小瓷砖继续进行尝试。 *若尝试成功,则首先更新状态,并进行递归,同时在回溯时需要注意恢复状态。
作者:Arglone
链接:https://leetcode.cn/problems/tiling-a-rectangle-with-the-fewest-squares/solutions/2291886/java-dfsbao-li-di-gui-by-arglone-alau/
来源:力扣(LeetCode)
代码附上:
class Solution {
private int res; // 答案。会在DFS过程中更新。
public int tilingRectangle(int n, int m) {
boolean[][] tiled = new boolean[n][m];
res = m * n;
process(tiled, 0);
return res;
}
// DFS:当前地上瓷砖状态为tiled,已经铺了count个瓷砖,继续铺下去把地面铺满
private void process(boolean[][] tiled, int count) {
if (count >= res) { // 剪枝
return;
}
int[] emptyPos = checkEmpty(tiled); // 地上没瓷砖的第一个位置
if (emptyPos[0] == -1 && emptyPos[1] == -1) { // 已经铺完了所有地方,收集答案
res = Math.min(res, count);
return;
}
// 从大到小,开始尝试铺瓷砖
int maxLen = Math.min(tiled.length - emptyPos[0], tiled[0].length - emptyPos[1]);
for (int len = maxLen; len >= 1; len--) {
// 尝试用len*len的瓷砖的左上角去对齐地上没瓷砖的这个位置
if (setStatus(tiled, emptyPos[0], emptyPos[1], emptyPos[0] + len - 1, emptyPos[1] + len - 1, false)) {
process(tiled, count + 1);
setStatus(tiled, emptyPos[0], emptyPos[1], emptyPos[0] + len - 1, emptyPos[1] + len - 1, true);
}
}
}
// 尝试反转 (row1, col1) 和 (row2, col2) 之间的铺瓷砖状态
// 必须确保这个区域内初始都是hasTiled状态,否则不会反转
private boolean setStatus(boolean[][] tiled, int row1, int col1, int row2, int col2, boolean hasTiled) {
for (int i = row1; i <= row2; i++) {
for (int j = col1; j <= col2; j++) {
if (tiled[i][j] != hasTiled) {
return false;
}
}
}
for (int i = row1; i <= row2; i++) {
for (int j = col1; j <= col2; j++) {
tiled[i][j] = !tiled[i][j];
}
}
return true;
}
// 顺序遍历寻找第一个没铺瓷砖的位置(从上到下,从左到右)
private int[] checkEmpty(boolean[][] tiled) {
int i = 0, j = 0;
for (; i < tiled.length; i++) {
for (j = 0; j < tiled[0].length; j++) {
if (!tiled[i][j]) {
return new int[]{i, j};
}
}
}
return new int[]{-1, -1};
}
}
作者:Arglone
链接:https://leetcode.cn/problems/tiling-a-rectangle-with-the-fewest-squares/solutions/2291886/java-dfsbao-li-di-gui-by-arglone-alau/
来源:力扣(LeetCode)