当前位置: 首页 > news >正文

机器学习特征工程:从原始数据到模型输入

机器学习特征工程:从原始数据到模型输入

1. 引言

“数据和特征决定了机器学习的上限。” 好的特征工程可以让简单模型超越复杂模型。

特征工程流程:

原始数据 → 数据清洗 → 特征提取 → 特征变换 → 特征选择 → 模型输入

2. 数值特征处理

2.1 标准化与归一化

fromsklearn.preprocessingimportStandardScaler,MinMaxScaler,RobustScaler# 标准化:均值0,方差1(适合正态分布)scaler=StandardScaler()X_scaled=scaler.fit_transform(X)# 归一化:缩放到 [0, 1]scaler=MinMaxScaler()X_normalized=scaler.fit_transform(X)# 鲁棒标准化:用中位数和四分位距(适合有异常值)scaler=RobustScaler()X_robust=scaler.fit_transform(X)

2.2 对数变换

importnumpyasnp# 处理右偏分布(如收入、房价)X_log=np.log1p(X)# log(1 + x),避免 log(0)# Box-Cox 变换fromscipy.statsimportboxcox X_boxcox,lambda_opt=boxcox(X+1)# Yeo-Johnson 变换(支持负数)fromsklearn.preprocessingimportPowerTransformer pt=PowerTransformer(method='yeo-johnson')X_yeojohnson=pt.fit_transform(X)

3. 类别特征编码

3.1 常用编码

fromsklearn.preprocessingimportLabelEncoder,OrdinalEncoder,OneHotEncoder# 标签编码(有序类别)le=LabelEncoder()encoded=le.fit_transform(["低","中","高","中"])# [0, 1, 2, 1]# One-Hot 编码(无序类别)ohe=OneHotEncoder(sparse=False)encoded=ohe.fit_transform([["红"],["蓝"],["红"],["绿"]])# [[1,0,0], [0,1,0], [1,0,0], [0,0,1]]# 目标编码(高基数类别)fromcategory_encodersimportTargetEncoder te=TargetEncoder()encoded=te.fit_transform(X_categorical,y)

3.2 高基数类别处理

# 频率编码freq=X['city'].value_counts(normalize=True)X['city_freq']=X['city'].map(freq)# 目标编码(带交叉验证防止过拟合)fromcategory_encodersimportTargetEncoder te=TargetEncoder(smoothing=10)X['city_target']=te.fit_transform(X['city'],y)# 嵌入编码(深度学习)importtorch.nnasnn embedding=nn.Embedding(num_categories=100,embedding_dim=8)

4. 时间特征

importpandasaspddefextract_time_features(df,time_col):"""提取时间特征"""df[time_col]=pd.to_datetime(df[time_col])# 基础特征df['year']=df[time_col].dt.year df['month']=df[time_col].dt.month df['day']=df[time_col].dt.day df['hour']=df[time_col].dt.hour df['minute']=df[time_col].dt.minute df['dayofweek']=df[time_col].dt.dayofweek# 0=周一df['dayofyear']=df[time_col].dt.dayofyear df['week']=df[time_col].dt.isocalendar().week# 周期编码(sin/cos)df['month_sin']=np.sin(2*np.pi*df['month']/12)df['month_cos']=np.cos(2*np.pi*df['month']/12)df['hour_sin']=np.sin(2*np.pi*df['hour']/24)df['hour_cos']=np.cos(2*np.pi*df['hour']/24)df['dayofweek_sin']=np.sin(2*np.pi*df['dayofweek']/7)df['dayofweek_cos']=np.cos(2*np.pi*df['dayofweek']/7)# 布尔特征df['is_weekend']=df['dayofweek'].isin([5,6]).astype(int)df['is_month_start']=df[time_col].dt.is_month_start.astype(int)df['is_month_end']=df[time_col].dt.is_month_end.astype(int)returndf

5. 文本特征

fromsklearn.feature_extraction.textimportTfidfVectorizerfromtransformersimportAutoTokenizer,AutoModel# TF-IDFtfidf=TfidfVectorizer(max_features=10000,ngram_range=(1,2))X_tfidf=tfidf.fit_transform(texts)# Sentence-BERT 嵌入tokenizer=AutoTokenizer.from_pretrained("all-MiniLM-L6-v2")model=AutoModel.from_pretrained("all-MiniLM-L6-v2")defget_embedding(text):inputs=tokenizer(text,return_tensors="pt",truncation=True,max_length=512)withtorch.no_grad():output=model(**inputs)returnoutput.last_hidden_state.mean(dim=1).squeeze().numpy()

6. 交互特征

# 数学组合X['ratio']=X['feature_a']/(X['feature_b']+1e-8)X['product']=X['feature_a']*X['feature_b']X['difference']=X['feature_a']-X['feature_b']# 多项式特征fromsklearn.preprocessingimportPolynomialFeatures poly=PolynomialFeatures(degree=2,interaction_only=True)X_poly=poly.fit_transform(X)# 分桶X['age_bin']=pd.cut(X['age'],bins=[0,18,35,50,65,100],labels=['少年','青年','中年','老年','高龄'])

7. 特征选择

7.1 过滤法

fromsklearn.feature_selectionimportSelectKBest,f_classif,mutual_info_classif# 方差过滤:删除方差为 0 的特征fromsklearn.feature_selectionimportVarianceThreshold selector=VarianceThreshold(threshold=0.01)X_filtered=selector.fit_transform(X)# 相关性过滤correlations=X.corrwith(y).abs()selected=correlations[correlations>0.05].index# SelectKBestselector=SelectKBest(f_classif,k=50)X_selected=selector.fit_transform(X,y)

7.2 包装法

fromsklearn.feature_selectionimportRFEfromsklearn.ensembleimportRandomForestClassifier# 递归特征消除model=RandomForestClassifier(n_estimators=100)rfe=RFE(model,n_features_to_select=20,step=5)X_selected=rfe.fit_transform(X,y)print(f"选中的特征:{X.columns[rfe.support_].tolist()}")

7.3 嵌入法

fromsklearn.ensembleimportGradientBoostingClassifier# 基于树模型的特征重要性model=GradientBoostingClassifier()model.fit(X,y)importances=pd.Series(model.feature_importances_,index=X.columns)top_features=importances.nlargest(20)print(top_features)

8. 特征工程 Pipeline

fromsklearn.pipelineimportPipelinefromsklearn.composeimportColumnTransformer# 定义不同列的处理方式numeric_features=['age','income','score']categorical_features=['city','gender','education']preprocessor=ColumnTransformer([('num',Pipeline([('scaler',StandardScaler()),]),numeric_features),('cat',Pipeline([('encoder',OneHotEncoder(handle_unknown='ignore')),]),categorical_features),])# 完整 Pipelinepipeline=Pipeline([('preprocessor',preprocessor),('feature_selection',SelectKBest(f_classif,k=50)),('classifier',GradientBoostingClassifier()),])pipeline.fit(X_train,y_train)score=pipeline.score(X_test,y_test)

9. 总结

特征工程的核心:

  1. 数值特征:标准化/归一化是基础,对数变换处理偏态
  2. 类别特征:低基数用 One-Hot,高基数用目标编码
  3. 时间特征:周期编码(sin/cos)比直接用数值更好
  4. 特征选择:先过滤(快速),再包装(精确),最后嵌入(模型驱动)
  5. Pipeline:把所有步骤封装为可复现的流水线
http://www.jsqmd.com/news/1072040/

相关文章:

  • 终极指南:如何使用League-Toolkit的OP.GG数据功能提升英雄联盟游戏表现
  • 如何用5分钟将单张图片转换为专业PSD分层文件:Layerdivider完全指南
  • jQuery:那个改变前端的库,现在活到了 4.0
  • 3大核心技术突破:解密Bodymovin插件的高效动画转换机制
  • 3步掌握kohya_ss可视化训练监控:从新手到专家的终极指南
  • OpenRocket火箭设计软件:从零开始掌握专业级火箭仿真
  • 如何用Super IO实现Blender高效导入导出:新手也能掌握的完整指南
  • 数字音乐跨平台播放终极解决方案:一站式解决格式兼容性问题
  • 生产环境采样策略:如何平衡数据完整性与存储成本?
  • 怎样快速提升Windows性能:Windows10Debloater系统清理完整教程
  • Linux“一切皆文件接口”的真相:那些“假文件”到底是什么?VFS和接口
  • 3分钟快速上手:BiliTools哔哩哔哩工具箱终极安装配置指南
  • 如何设置huawei的AP?
  • Loop Engineering 与 Spec-Driven Development 结合下的 token 收敛
  • Real-ESRGAN-ncnn-vulkan终极指南:快速实现图像超分辨率修复
  • NANDO开源NAND编程器:从入门到精通的完整指南
  • 靠谱的无外机厨房空调生产厂家哪个好
  • 引线长度按直线算,现场绕一圈不够用
  • Navicat密码解密工具:轻松恢复数据库连接凭证
  • 【AISMM Level 3权威落地指南】:SITS 2026定义级流程规范的5大实操陷阱与避坑清单(2024年唯一经CNITSEC验证的实施路径)
  • 三步免费下载百度文库文档:开源工具的完整使用指南
  • 从零到一:部署基于 FastAPI + ChromaDB + DeepSeek 的 RAG 知识库问答小程序
  • Sign Language Transformers:突破性端到端手语识别与翻译技术
  • 零代码经验,我用Claude Code搓出的生产力工具
  • ImageStrike:一站式图像隐写分析工具,CTF选手的秘密武器
  • ROFL-Player:免费英雄联盟回放播放器完整使用指南
  • PortSwigger SQL注入LAB3
  • 猫抓浏览器扩展:你的网页视频资源一站式下载解决方案
  • 音乐格式转换指南:3步解锁你的加密音乐收藏
  • 7th [Learn biology with math thinking] 2026.06.23