Nat*_*son 9 python scikit-learn
我正在尝试使用两个步骤创建一个sklearn管道:
但是,我的数据包含数字和分类变量,我已使用它转换为虚拟变量pd.get_dummies.我想标准化数值变量,但保留虚拟对象.我这样做是这样的:
X = dataframe containing both numeric and categorical columns
numeric = [list of numeric column names]
categorical = [list of categorical column names]
scaler = StandardScaler()
X_numeric_std = pd.DataFrame(data=scaler.fit_transform(X[numeric]), columns=numeric)
X_std = pd.merge(X_numeric_std, X[categorical], left_index=True, right_index=True)
Run Code Online (Sandbox Code Playgroud)
但是,如果我要创建一个管道,如:
pipe = sklearn.pipeline.make_pipeline(StandardScaler(), KNeighborsClassifier())
Run Code Online (Sandbox Code Playgroud)
它会标准化我的DataFrame中的所有列.有没有办法在仅标准化数字列时执行此操作?
Max*_*axU 11
假设你有以下DF:
In [163]: df
Out[163]:
a b c d
0 aaa 1.01 xxx 111
1 bbb 2.02 yyy 222
2 ccc 3.03 zzz 333
In [164]: df.dtypes
Out[164]:
a object
b float64
c object
d int64
dtype: object
Run Code Online (Sandbox Code Playgroud)
你可以找到所有数字列:
In [165]: num_cols = df.columns[df.dtypes.apply(lambda c: np.issubdtype(c, np.number))]
In [166]: num_cols
Out[166]: Index(['b', 'd'], dtype='object')
In [167]: df[num_cols]
Out[167]:
b d
0 1.01 111
1 2.02 222
2 3.03 333
Run Code Online (Sandbox Code Playgroud)
并StandardScaler仅适用于那些数字列:
In [168]: scaler = StandardScaler()
In [169]: df[num_cols] = scaler.fit_transform(df[num_cols])
In [170]: df
Out[170]:
a b c d
0 aaa -1.224745 xxx -1.224745
1 bbb 0.000000 yyy 0.000000
2 ccc 1.224745 zzz 1.224745
Run Code Online (Sandbox Code Playgroud)
现在你可以"一个热编码"分类(非数字)列...
我会使用FeatureUnion.然后,我通常会做类似的事情,假设您在管道中对分类变量进行虚拟编码,而不是之前使用Pandas:
from sklearn.pipeline import Pipeline, FeatureUnion, make_pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.neighbors import KNeighborsClassifier
class Columns(BaseEstimator, TransformerMixin):
def __init__(self, names=None):
self.names = names
def fit(self, X, y=None, **fit_params):
return self
def transform(self, X):
return X[self.names]
numeric = [list of numeric column names]
categorical = [list of categorical column names]
pipe = Pipeline([
("features", FeatureUnion([
('numeric', make_pipeline(Columns(names=numeric),StandardScaler())),
('categorical', make_pipeline(Columns(names=categorical),OneHotEncoder(sparse=False)))
])),
('model', KNeighborsClassifier())
])
Run Code Online (Sandbox Code Playgroud)
你可以进一步查看Sklearn Pandas,这也很有趣.