Scikit-learn Pipeline:构建可复用的 ML 流水线

Scikit-learn Pipeline:构建可复用的 ML 流水线

1. Pipeline 基础

python 复制代码
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestClassifier

# 创建流水线
pipe = Pipeline([
    ('scaler', StandardScaler()),
    ('pca', PCA(n_components=10)),
    ('clf', RandomForestClassifier(n_estimators=100))
])

# 训练
pipe.fit(X_train, y_train)

# 预测
y_pred = pipe.predict(X_test)

# 评分
score = pipe.score(X_test, y_test)

2. ColumnTransformer

python 复制代码
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer

numeric_features = ['age', 'income', 'score']
categorical_features = ['city', 'gender']

preprocessor = ColumnTransformer([
    ('num', Pipeline([
        ('imputer', SimpleImputer(strategy='median')),
        ('scaler', StandardScaler()),
    ]), numeric_features),
    ('cat', Pipeline([
        ('imputer', SimpleImputer(strategy='most_frequent')),
        ('encoder', OneHotEncoder(handle_unknown='ignore')),
    ]), categorical_features),
])

# 完整流水线
pipe = Pipeline([
    ('preprocessor', preprocessor),
    ('classifier', RandomForestClassifier())
])

3. 自定义 Transformer

python 复制代码
from sklearn.base import BaseEstimator, TransformerMixin

class FeatureEngineer(BaseEstimator, TransformerMixin):
    def __init__(self, add_interaction=True):
        self.add_interaction = add_interaction
    
    def fit(self, X, y=None):
        return self
    
    def transform(self, X):
        X = X.copy()
        X['price_per_sqft'] = X['price'] / X['area']
        if self.add_interaction:
            X['age_income'] = X['age'] * X['income']
        return X

# 使用
pipe = Pipeline([
    ('feature_eng', FeatureEngineer()),
    ('scaler', StandardScaler()),
    ('clf', RandomForestClassifier())
])

4. GridSearch + Pipeline

python 复制代码
from sklearn.model_selection import GridSearchCV

param_grid = {
    'pca__n_components': [5, 10, 15],
    'clf__n_estimators': [50, 100, 200],
    'clf__max_depth': [5, 10, None],
}

grid = GridSearchCV(pipe, param_grid, cv=5, scoring='accuracy')
grid.fit(X_train, y_train)
print(f"最佳参数: {grid.best_params_}")

总结

组件 作用
Pipeline 串联处理步骤
ColumnTransformer 按列分别处理
FeatureUnion 并行特征提取
自定义 Transformer 封装业务逻辑
相关推荐
sylviiiiiia23 分钟前
leetcode hot100
python·算法·leetcode
千里码aicood33 分钟前
flask-基于注意力机制的异常流量检测与自动防御系统
后端·python·flask
维克兜率天34 分钟前
4.3.2.1 日常监控:上线只是开始
服务器·数据库·python·区块链·php·量化
the局外人42 分钟前
学习 FastAPI 的 Day 2:用异步 ORM 完成增删改查
后端·python·fastapi
智搜广告44 分钟前
GEO优化公司怎么选?智搜广告从三个维度帮你判断
大数据·人工智能·python·elasticsearch·microsoft·geo
wanglei2007081 小时前
软件架构设计中,各个组件之间的通信方式有哪些?
python
会飞的拖把1 小时前
Python 面向对象编程详解:从类与对象到动态属性方法
开发语言·python
微小冷1 小时前
Python CasADi初步教程
python·控制·模型预测·casadi·符号表达式·sx
cpolar技术支持1 小时前
requirements.txt 能安装不等于安全:用 pip-audit 检查 Python 依赖,cpolar 临时分享脱敏报告
python·cpolar·requirements·依赖安全·pip-audit