跳到主要内容

03 特征选择(5 法对比)

02 章把 60K 基因压到 5000 HVG,但喂给 LR / SVM 还是太多。这一章用 5 种主流方法进一步压到 50-200 个真正有判别力的基因,并用韦恩图找 5 法共识。

HVG vs 特征选择 — 别搞混

方法看不看 label何时做目的
HVG(02 章)不看02 章 split 前去噪声,降维到 5000
特征选择(本章)必须 split 后,只在 train 上做找判别基因,降到 50-200

混淆这两件事是 signature inflation 的源头。HVG 是"去噪声",纯探索;特征选择是"找信号",看 label 才能做,所以必须在 train 集内做。在全数据集做特征选择再 split,train + test 都看过 label,signature 性能虚高。

三类方法

类别思路代表速度偏好
Filter(独立打分)每特征单独计算与 label 的关联方差、Wilcoxon、ANOVA、IG极快不考虑特征交互
Wrapper(模型驱动)把模型当评估器,反复试不同特征子集RFE、Boruta、forward selection考虑交互,易过拟合
Embedded(模型自带)训模型时自动筛特征LASSO、tree-based importance与最终模型一致

实务建议:5 法各跑一遍取共识 = filter (方差) + filter (Wilcoxon / ANOVA) + embedded (mRMR) + wrapper (Boruta) + wrapper (RFE)。多视角共识基因可信度远高于单方法。

5 种方法逐一拆

1. 方差过滤(filter / 最快)

gene_var <- apply(X_train, 2, var) # 注意是按列
top_var_idx <- order(gene_var, decreasing = TRUE)[1:200]

优点:超快,baseline 必跑。缺点:不看 label,可能选出生物学差异大但与目标无关的基因。

实际上 02 章 HVG 选择就是方差过滤的 5000 维版,本章再压一次到 200。注意:HVG 在全样本做(无 label),特征选择在 train 内做(有 label)

2. 单因素 Wilcoxon(filter / 标配)

pvals <- apply(X_train, 2, function(g) {
wilcox.test(g ~ y_train)$p.value
})
top_wilcox_idx <- order(pvals)[1:200]

优点:对正态假设宽容(比 t-test 稳),生信场景的金牌 baseline。缺点:绝对不能用 padj < 0.05 筛选当 ML 特征!这是 p-hacking,top N 排序才是正确做法(无视显著性,只看排名)。

3. mRMR(filter / embedded 杂交)

minimum Redundancy Maximum Relevance:在最大化与 label 相关的同时,最小化特征间冗余。

library(mRMRe)
dd <- mRMR.data(data = data.frame(label = y_train, X_train))
res <- mRMR.classic(data = dd, target_indices = 1, feature_count = 200)

优点:解决"top 5 都是同一通路上的近邻基因"问题,选出来的更"分散"。缺点:贪心算法,大特征数(> 5000)慢。

4. Boruta(wrapper / 现代选择)

基于 RF 的 wrapper:训 RF + 把每个特征对比"shadow"特征(打乱版),只保留确认显著高于 shadow 的。

library(Boruta)
boruta_res <- Boruta(X_train, y_train, maxRuns = 100)
selected <- getSelectedAttributes(boruta_res, withTentative = FALSE)

优点:严格 + 自动决定 N(不用预设 200),给 "Confirmed / Tentative / Rejected" 三标签。缺点:慢(maxRuns=100 在 1000 特征上 ~5-10 分钟),受 RF 偏好影响。

5. RFE(wrapper / 递归)

Recursive Feature Elimination:训模型 → 删最不重要的 N 个 → 重训 → 再删,直到剩目标数量。

library(caret)
rfe_ctrl <- rfeControl(functions = rfFuncs, method = "cv", number = 5)
rfe_res <- rfe(X_train, y_train, sizes = c(50, 100, 200, 500),
rfeControl = rfe_ctrl)

优点:5-fold CV 内嵌选最优特征数。缺点:最慢,需 RF 作为内层模型(可换 SVM)。

5 法韦恩图共识

每个方法选出 top 200,5 个 200 元素集合做韦恩。共识基因(≥3 法重叠)= 投稿亮点

library(VennDiagram)
sets <- list(Variance = top_var, Wilcoxon = top_wilcox,
mRMR = top_mrmr, Boruta = top_boruta, RFE = top_rfe)
venn.diagram(sets, filename = "venn.png")

实测 TCGA-LIHC 上,5 法 ≥ 3 法共识通常 30-80 个基因。用这 30-80 个去喂 04 章训分类器,既性能稳又论文好讲。

在线一键复现

不想本地跑 5 个包?把 02 章产物上传到 ml-feature-select 工具:

  • features.csv:行=特征,列=样本
  • labels.csv:两列 sample / label

工具自动并行跑 5 法 + 韦恩图 + 5 法 rank 相关性热图,输出 5 张 SCI 双格式图 + 7 张排名表。

ml-feature-select 公开 demo

公开 demo:200 features × 150 samples,5 法跑完 ~30 秒,共识 top 50 基因可直接复制到 04 章 ml-classifier。

用 padj 筛 ML 特征 = p-hacking

⚠️ 这一节单独标出,因为是生信里最常见的错误用法:

很多研究者熟悉差异分析,直觉地用:

# 错误!
deseq_res <- results(dds)
sig_genes <- rownames(deseq_res)[deseq_res$padj < 0.05 & abs(deseq_res$log2FoldChange) > 1]
# 然后用 sig_genes 训 ML 模型 ...

这是错误的:

  1. DESeq2 的 padj 是用全样本计算的 — 包括 train + val + test
  2. test 集"已经看过 label"(因为 padj 用了 test 样本)
  3. 训出的模型在 test 集上 AUC 必然虚高

正确做法:只在 train 集内做特征选择:

# 正确
train_dds <- DESeqDataSetFromMatrix(counts[, train_ids], coldata[train_ids, ], design = ~ tissue)
train_dds <- DESeq(train_dds)
train_res <- results(train_dds)
top_genes <- rownames(train_res)[order(train_res$padj)][1:200]
# 然后用 top_genes 提取 train + test 表达,喂 04 章

差异分析(基因排序用)和特征选择(ML 输入用)是同一类操作,但前者用 padj 筛 + 解释,后者用 ranking 选 + 训模型。用 padj 筛 ML 特征 = 用 padj 数字当训练目标 = p-hacking

常见坑

HVG 与特征选择混用 — HVG 不看 label 在全数据集做 OK,特征选择必须在 train 内做

5 法每法选 N 个不一样 — 公平对比要么都选 top 200,要么都用 padj < 0.05(但都不能 p-hack)。BioF3 ml-feature-select 默认 top 200

Boruta 跑超过 maxRuns 还有 Tentative 不做处理TentativeRoughFix(boruta_res) 用 median importance 强制决定

mRMR 在小样本(<50) 不稳定 — 共识矩阵的稳定性来自重采样投票,样本少投不出共识

韦恩图画了不解释 — 论文里画 venn 不写"≥3 法共识 N 基因"等于浪费图,要明确给共识基因列表 + 后续 04 章分类性能

特征选择后没记录最终特征列表持久化 — 论文 reviewer 让你"附 list",你找不到 — write.csv(consensus_genes, "consensus.csv") 永远在选完之后立刻做

在线工具

ml-feature-select

ml-feature-select 工具的输出 = 本章 5 法 + 韦恩图 + 共识 csv,可一键导给 04 章 ml-classifier。教程 + 工具是 1:1 配套。

Methods 段写法模板

For feature selection within the training set (n=105), we ran five
methods independently: variance filter, Wilcoxon rank-sum test (BH-adjusted),
mRMR (mRMRe v2.1), Boruta (v8.0, maxRuns=100), and RFE (caret v6.0,
nested 5-fold CV with RF as evaluator). Top 200 features were retained from
each method. The consensus gene set was defined as features identified by
≥3/5 methods. This consensus set (n=XX) was used to train downstream
classifiers (Module 04). Test set features were aligned to this consensus
list without re-running selection.

本章状态

✅ Wave 4 正文完成(2026-05-27)。配套 ml03_select_sci.R 在产,工具已上线 https://biof3.com/tools/#ml-feature-select。

AI 陪学

让 AI 陪我学这一篇

AI 会读这篇文章后给你 3-5 步学习计划, 逐步陪你学完,最后出 1-3 道题验证你掌握得怎么样。 登录后 AI 才能记住你的进度。

静态文件

离线资料下载

手册 HTML / PDF 已在后台预生成,点击后直接下载网站静态资源。