经典算法对比:SVM/随机森林/XGBoost 选型指南 1. 算法速查表 主流算法对比: ┌──────────────┬──────────┬──────────┬──────────┬──────────┬──────────┐ │ 算法 │ 适用任务 │ 可解释性 │ 训练速度 │ 预测速度 │ 处理高维 │ ├──────────────┼──────────┼──────────┼──────────┼──────────┼──────────┤ │ 逻辑回归 │ 分类 │ ★★★★★ │ ★★★★★ │ ★★★★★ │ ★★★ │ │ SVM │ 分类/回归 │ ★★★ │ ★★ │ ★★★★ │ ★★★★★ │ │ 决策树 │ 分类/回归 │ ★★★★★ │ ★★★★ │ ★★★★★ │ ★★★ │ │ 随机森林 │ 分类/回归 │ ★★★★ │ ★★★★ │ ★★★★ │ ★★★★ │ │ KNN │ 分类/回归 │ ★★★★ │ ★★★★★ │ ★★ │ ★★ │ │ 朴素贝叶斯 │ 分类 │ ★★★★ │ ★★★★★ │ ★★★★★ │ ★★★★ │ │ XGBoost │ 分类/回归 │ ★★★ │ ★★★ │ ★★★★ │ ★★★★ │ │ LightGBM │ 分类/回归 │ ★★★ │ ★★★★★ │ ★★★★ │ ★★★★★ │ │ 神经网络 │ 通用 │ ★ │ ★★ │ ★★★★ │ ★★★★★ │ └──────────────┴──────────┴──────────┴──────────┴──────────┴──────────┘2. SVM(支持向量机) from sklearn. svmimport SVC, SVRfrom sklearn. preprocessingimport StandardScalerfrom sklearn. pipelineimport make_pipeline# 分类 svm_clf= make_pipeline( StandardScaler( ) , SVC( kernel= 'rbf' , C= 1.0 , gamma= 'scale' ) ) svm_clf. fit( X_train, y_train) # 回归 svm_reg= make_pipeline( StandardScaler( ) , SVR( kernel= 'rbf' , C= 1.0 , epsilon= 0.1 ) ) svm_reg. fit( X_train, y_train) # 参数说明: # kernel: 'rbf'(高斯), 'linear'(线性), 'poly'(多项式) # C: 正则化强度(越大越不正则化) # gamma: 核函数系数(越大越复杂) 3. 随机森林 from sklearn. ensembleimport RandomForestClassifier, RandomForestRegressor# 分类 rf_clf= RandomForestClassifier( n_estimators= 100 , # 树的数量 max_depth= 10 , # 最大深度 min_samples_split= 5 , # 分裂最小样本数 min_samples_leaf= 2 , # 叶子最小样本数 max_features= 'sqrt' , # 每棵树特征数 random_state= 42 , n_jobs= - 1 ) rf_clf. fit( X_train, y_train) # 特征重要性 importances= rf_clf. feature_importances_ feature_importance= pd. Series( importances, index= feature_names) print ( feature_importance. nlargest( 10 ) ) # 回归 rf_reg= RandomForestRegressor( n_estimators= 100 , random_state= 42 ) rf_reg. fit( X_train, y_train) 4. XGBoost import xgboostas xgbfrom sklearn. model_selectionimport GridSearchCV# 分类 xgb_clf= xgb. XGBClassifier( n_estimators= 100 , max_depth= 6 , learning_rate= 0.1 , subsample= 0.8 , colsample_bytree= 0.8 , reg_alpha= 0.1 , # L1 正则化 reg_lambda= 1.0 , # L2 正则化 random_state= 42 , use_label_encoder= False , eval_metric= 'logloss' ) xgb_clf. fit( X_train, y_train) # 超参数搜索 param_grid= { 'n_estimators' : [ 50 , 100 , 200 ] , 'max_depth' : [ 3 , 6 , 9 ] , 'learning_rate' : [ 0.01 , 0.1 , 0.2 ] , } grid= GridSearchCV( xgb_clf, param_grid, cv= 5 , scoring= 'accuracy' ) grid. fit( X_train, y_train) print ( f"最佳参数: { grid. best_params_} " ) print ( f"最佳分数: { grid. best_score_: .4f } " ) 5. LightGBM import lightgbmas lgb lgb_clf= lgb. LGBMClassifier( n_estimators= 100 , max_depth= 6 , learning_rate= 0.1 , num_leaves= 31 , subsample= 0.8 , colsample_bytree= 0.8 , reg_alpha= 0.1 , reg_lambda= 1.0 , random_state= 42 ) lgb_clf. fit( X_train, y_train) # 特征重要性 lgb. plot_importance( lgb_clf, max_num_features= 10 ) 6. 选型决策树 选型指南: ├── 数据量 < 1 万 │ ├── 高维稀疏 → SVM │ ├── 低维密集 → KNN / 随机森林 │ └── 需要可解释性 → 决策树 / 逻辑回归 ├── 数据量 1-100 万 │ ├── 结构化数据 → XGBoost / LightGBM │ ├── 文本数据 → 朴素贝叶斯 / SVM │ └── 图像数据 → CNN └── 数据量 > 100 万 ├── 结构化数据 → LightGBM(最快) ├── 图像/语音 → 深度学习 └── 实时推理 → 轻量模型总结 场景 推荐算法 原因 小数据高维 SVM 核函数处理高维 大数据结构化 XGBoost/LightGBM 精度高速度快 可解释性要求 决策树/逻辑回归 易于理解 实时推理 逻辑回归/KNN 预测快 竞赛常用 XGBoost + 特征工程 稳定高分