Python 如何标准化机器学习管道中的数字列?

Python 如何标准化机器学习管道中的数字列?,python,pandas,machine-learning,scikit-learn,pipeline,Python,Pandas,Machine Learning,Scikit Learn,Pipeline,我有数字和分类特征的数据;我只想标准化数字特征。数值列在X\u num\u cols中捕获,但是我不确定如何将其实现到管道代码中,例如,make\u Pipeline(preprocessing.StandardScaler(columns=X\u num\u cols)不起作用。我发现了stackoverflow,但答案不符合我的代码布局/目的 from sklearn import preprocessing from sklearn.pipeline import make_pipelin

我有数字和分类特征的数据;我只想标准化数字特征。数值列在
X\u num\u cols
中捕获,但是我不确定如何将其实现到管道代码中,例如,
make\u Pipeline(preprocessing.StandardScaler(columns=X\u num\u cols)
不起作用。我发现了stackoverflow,但答案不符合我的代码布局/目的

from sklearn import preprocessing
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split,GridSearchCV
import pandas as pd
import numpy as np

# Separate target from training features
y = df['MED']
X = df.drop('MED', axis=1)

# Retain only the needed predictors
X = X.filter(['age', 'gender', 'ccis'])

# Find the numerical columns, exclude categorical columns
X_num_cols = X.columns[X.dtypes.apply(lambda c: np.issubdtype(c, np.number))]

# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                    test_size=0.5, 
                                                    random_state=1234, 
                                                    stratify=y)

# Pipeline
pipeline = make_pipeline(preprocessing.StandardScaler(),
            LogisticRegression(penalty='l2'))

# Declare hyperparameters
hyperparameters = {'logisticregression__C' : [0.01, 0.1, 1.0, 10.0, 100.0],
                  'logisticregression__multi_class': ['ovr'],
                  'logisticregression__class_weight': ['balanced']
                  }

# SKlearn cross-validation with pupeline
clf = GridSearchCV(pipeline, hyperparameters, cv=10)
样本数据如下:

Age    Gender    CCIS
13     M         5
24     F         8

您的管道应如下所示:

from sklearn.preprocessing import StandardScaler,FunctionTransformer
from sklearn.pipeline import Pipeline,FeatureUnion


rg = LogisticRegression(class_weight = { 0:1, 1:10 }, random_state = 42, solver = 'saga',max_iter=100,n_jobs=-1,intercept_scaling=1)


pipeline=Pipeline(steps= [
    ('feature_processing', FeatureUnion(transformer_list = [
            ('categorical', FunctionTransformer(lambda data: data[:, cat_indices])),

            #numeric
            ('numeric', Pipeline(steps = [
                ('select', FunctionTransformer(lambda data: data[:, num_indices])),
                ('scale', StandardScaler())
                        ]))
        ])),
    ('clf', rg)
    ]
)

你能按照你引用的链接中的FeatureUnion添加一个小样本数据吗?你看到Marcus V基于FeatureUnion的答案了吗?是的,但不能完全理解代码的逻辑,因此无法实现。我也试图模仿代码,但数字和分类行给了我错误。@KubiK888我在阅读文章时学到了管道。我认为这些流程图非常清楚地说明了管道和功能联合是如何协同工作和嵌套的。事实上,如果事情变得复杂,我喜欢自己画类似的框。关于数字和分类线:这些是从原始问题中提取的。当然,根据您的问题,它们应该是列名列表。对于实例“X_num_cols”。