
随机森林(Random Forest)是一种集成学习算法,通过构建多棵决策树并综合投票结果提升模型性能。其核心策略为:
类比:就像让多位医生(决策树)各自独立诊断,再根据多数意见(投票)做出最终判断,降低误诊风险。
import java.util.*;
public class RandomForest {
private List<DecisionTree> trees;
private int numTrees;
private int maxDepth;
private int maxFeatures;
public RandomForest(int numTrees, int maxDepth, int maxFeatures) {
this.numTrees = numTrees;
this.maxDepth = maxDepth;
this.maxFeatures = maxFeatures;
this.trees = new ArrayList<>();
}
// 训练森林
public void train(double[][] X, int[] y) {
for (int i = 0; i < numTrees; i++) {
// 1. Bootstrap采样
int[][] sample = bootstrapSample(X, y);
// 2. 构建决策树
DecisionTree tree = new DecisionTree(maxDepth, maxFeatures);
tree.train(sample[0], sample[1]);
trees.add(tree);
}
}
// 预测(多数投票)
public int predict(double[] x) {
Map<Integer, Integer> votes = new HashMap<>();
for (DecisionTree tree : trees) {
int pred = tree.predict(x);
votes.put(pred, votes.getOrDefault(pred, 0) + 1);
}
return Collections.max(votes.entrySet(), Map.Entry.comparingByValue()).getKey();
}
// 生成Bootstrap样本
private int[][] bootstrapSample(double[][] X, int[] y) {
int n = X.length;
double[][] X_sample = new double[n][];
int[] y_sample = new int[n];
Random rand = new Random();
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
X_sample[i] = X[idx];
y_sample[i] = y[idx];
}
return new int[][]{X_sample, y_sample};
}
public static void main(String[] args) {
// 示例数据:花瓣长度、宽度 -> 鸢尾花种类
double[][] X = {{5.1, 3.5}, {6.2, 2.8}, {4.9, 3.0}};
int[] y = {0, 1, 0}; // 0=setosa, 1=versicolor
RandomForest rf = new RandomForest(100, 5, 1);
rf.train(X, y);
System.out.println(rf.predict(new double[]{5.4, 3.2})); // 输出0
}
}
// 简化的决策树节点
class DecisionTree {
// 实际需实现树结构、特征选择、分裂逻辑等
public int predict(double[] x) { return 0; }
public void train(double[][] X, int[] y) {}
}指标 | 单棵决策树 | 随机森林 |
|---|---|---|
训练时间复杂度 | O(m√n log n) | O(k*m√n log n) |
预测时间复杂度 | O(log n) | O(k*log n) |
空间复杂度 | O(nodes) | O(k*nodes) |
泛化能力 | 易过拟合 | 显著提升(k=树的数量) |
新手入门:
基础概念
参数调优
// 网格搜索寻找最佳参数组合
for (int trees : Arrays.asList(50, 100, 200)) {
for (int depth : Arrays.asList(5, 10)) {
RandomForest model = new RandomForest(trees, depth, 3);
// 交叉验证评估效果
}
}可视化分析
sklearn库绘制特征重要性图
成手进阶:
并行优化
// 多线程构建决策树
ExecutorService executor = Executors.newFixedThreadPool(8);
List<Future<DecisionTree>> futures = new ArrayList<>();
for (int i=0; i<numTrees; i++) {
futures.add(executor.submit(() -> buildTree()));
}增量学习
混合建模
可解释性增强
自动化机器学习
联邦森林
// 跨设备联合训练
public class FederatedForest {
public void aggregateUpdates(Map<Device, Model> localModels) {
// 安全聚合各设备模型更新
}
}量子加速
时空随机森林
随机森林的哲学启示:集体智慧优于个体决策。从Kaggle竞赛到工业级推荐系统,其稳定表现验证了集成学习的强大。正如生态学家所言:“森林的稳固不在于某棵巨树,而在于多样性的共生。” 掌握随机森林,便是掌握了这种群体智慧的建模艺术。