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

示例

复制代码
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
相关推荐
试剂界的爱马仕2 分钟前
α-Cyano-4-hydroxycinnamic acid(CHCA)用作 MALDI-TOF 基质的配制与使用
大数据·人工智能·深度学习·学习
强哥聊算力4 分钟前
《算力江湖·Intel帝国往事》第三集:安迪·格鲁夫的铁血时代:Intel为什么能把对手逼到墙角?
运维·人工智能·科技·算法·ai·gpu算力
Raymond16889 分钟前
零样本TTS长视频配音的工程取舍:自回归、非自回归与韵律边界
人工智能·数据挖掘·回归·音视频·音频
大熊背14 分钟前
人工智能中的数学之美---从向量角度理解深度学习中的感知机
人工智能·深度学习
杭州华望MBSE16 分钟前
AI将载着MBSE驶向哪里?从前台工具转向工程语义与验证层
人工智能·大语言模型·mbse·sysml·国产工业软件
我喜欢就喜欢17 分钟前
AI与教育:一场双向塑造的相互性变革
人工智能
指针向南21 分钟前
PDF 转 HTML 免费5种方法,体积0.5%到368%
图像处理·人工智能·计算机视觉·pdf
星核0penstarry24 分钟前
企业 API 向后兼容实战:版本控制、废弃字段通知与灰度发布策略
人工智能·后端·api·接口隔离原则·版本控制·架构设计·后端架构
Light6025 分钟前
每晚 1000 万条警报:望远镜的瓶颈已经变成算法
机器学习·ai for science·超新星·引力透镜·vera rubin 天文台·事件警报
Dawson Zhu29 分钟前
【AI编译器入门⑥】端到端编译流程实战:ONNX → TVM → TensorRT全链路
人工智能·语言模型·架构·aigc·agi