【发布时间】:2018-07-26 14:34:05
【问题描述】:
我有具有数字和分类特征的数据;我只想标准化数字特征。数值列在X_num_cols 中捕获,但是我不确定如何将其实现到管道代码中,例如make_pipeline(preprocessing.StandardScaler(columns=X_num_cols) 不起作用。我在 stackoverflow 上找到了this,但答案不符合我的代码布局/目的。
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
【问题讨论】:
标签: python pandas machine-learning scikit-learn pipeline