机器学习特征预处理之删移除无关特征

示例

复制代码
def remove_irrelevant(X_train, y_train, X_test):
    """删除无关特征"""
    rf = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1)
    rf.fit(X_train, y_train)

    importance_df = pd.DataFrame({
        'feature': X_train.columns,
        'importance': rf.feature_importances_
    }).sort_values('importance', ascending=False)

    cols_to_keep = importance_df[importance_df['importance'] > 0]['feature'].tolist()
    dropped_cols = importance_df[importance_df['importance'] == 0]['feature'].tolist()

    print(f"  删除 {len(dropped_cols)} 个无关特征")
    print(f"  保留 {len(cols_to_keep)} 个特征")

    return X_train[cols_to_keep], X_test[cols_to_keep]

改进版

复制代码
def remove_irrelevant(X_train, y_train, X_test, 
                      threshold='cumulative',  # 'zero', 'cumulative', 'percentile'
                      cum_ratio=0.95,
                      min_features=10):
    """
    智能特征选择
    threshold: 'zero' 删除重要性=0; 'cumulative' 保留累积贡献达cum_ratio的特征
    """
    rf = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs=-1)
    rf.fit(X_train, y_train)
    
    importance_df = pd.DataFrame({
        'feature': X_train.columns,
        'importance': rf.feature_importances_
    }).sort_values('importance', ascending=False)
    
    if threshold == 'zero':
        cols_to_keep = importance_df[importance_df['importance'] > 0]['feature'].tolist()
    elif threshold == 'cumulative':
        # 计算累积重要性
        importance_df['cum_importance'] = importance_df['importance'].cumsum()
        cols_to_keep = importance_df[
            importance_df['cum_importance'] <= cum_ratio
        ]['feature'].tolist()
        # 确保至少保留 min_features 个特征
        if len(cols_to_keep) < min_features:
            cols_to_keep = importance_df.head(min_features)['feature'].tolist()
    
    dropped_cols = [col for col in X_train.columns if col not in cols_to_keep]
    
    print(f"  删除 {len(dropped_cols)} 个特征(保留重要性前{len(cols_to_keep)}个)")
    print(f"  保留特征: {cols_to_keep[:5]}..." if len(cols_to_keep)>5 else f"  保留特征: {cols_to_keep}")
    
    # 返回裁剪后的数据 + 被删特征(便于审计)
    return X_train[cols_to_keep], X_test[cols_to_keep], dropped_cols

说明:如果数据特征数量<100且特征间相关性低,当前代码可用。但对于生产环境或高维数据,建议采用累积重要性阈值 结合交叉验证的改进版本。

调用示例

复制代码
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

# 准备数据
X, y = make_classification(
    n_samples=1000,
    n_features=20,
    n_informative=10,
    n_redundant=5,
    n_repeated=0,
    random_state=42
)

# 转为DataFrame便于查看特征名
feature_names = [f'feature_{i}' for i in range(20)]
X_df = pd.DataFrame(X, columns=feature_names)
y_series = pd.Series(y, name='target')

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
    X_df, y_series, test_size=0.2, random_state=42
)

print(f"原始训练集形状: {X_train.shape}")
print(f"原始测试集形状: {X_test.shape}")

# 调用特征选择函数
X_train_selected, X_test_selected,dropped_cols = remove_irrelevant(X_train, y_train, X_test, threshold='cumulative', cum_ratio=0.95,
                                                      min_features=10)

print(f"筛选后训练集形状: {X_train_selected.shape}")
print(f"筛选后测试集形状: {X_test_selected.shape}")
print(f"保留的特征列: {X_train_selected.columns.tolist()}")

输出

复制代码
原始训练集形状: (800, 20)
原始测试集形状: (200, 20)
  删除 4 个特征(保留重要性前16个)
  保留特征: ['feature_11', 'feature_14', 'feature_17', 'feature_15', 'feature_7']...
筛选后训练集形状: (800, 16)
筛选后测试集形状: (200, 16)
保留的特征列: ['feature_11', 'feature_14', 'feature_17', 'feature_15', 'feature_7', 'feature_2', 'feature_18', 'feature_16', 'feature_9', 'feature_1', 'feature_3', 'feature_12', 'feature_4', 'feature_0', 'feature_8', 'feature_10']

Process finished with exit code 0
相关推荐
月光船幽幽10 小时前
检测用户意图复杂性的关键方法
人工智能·python
Henry-SAP10 小时前
AI突破重塑未来科技格局
人工智能·云原生·sap·erp
空堂与归11 小时前
图像识别总是准确率低?用卷积神经网络CNN实现高效分类
人工智能·深度学习·分类·cnn
老猿AI洞察12 小时前
阿里云启用巴西数据中心,AI服务出海落子南半球
人工智能·阿里云·云计算
大金SEO12 小时前
GEO启动的正确顺序:先做信源清理,再谈内容扩张
大数据·人工智能
Bruce_Liuxiaowei12 小时前
从基础理论开始学习人工智能(三):盲目搜索——从状态空间图到生成-测试范式
人工智能·学习
qyz_hr13 小时前
拥抱AI,红海云筑牢企业组织与人力数据底层能力
人工智能
Capricorn198813 小时前
个人知识库接入大模型频现“幻觉引用”?排查 RAG 溯源失效问题,解析知芽构建可信第三大脑的底层架构
大数据·论文阅读·人工智能·笔记·架构·论文笔记
LaughingZhu13 小时前
Product Hunt 每日热榜 | 2026-08-27
人工智能·深度学习·神经网络·搜索引擎·百度