pandas SKLearn MinMaxScaler - 仅缩放特定列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43834242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SKLearn MinMaxScaler - scale specific columns only
提问by lte__
I'd like to scale some (but not all) of the columns in a Pandas dataFrame using a MinMaxScaler. How can I do it?
我想使用 MinMaxScaler 缩放 Pandas 数据帧中的一些(但不是全部)列。我该怎么做?
采纳答案by Random
Since sklearn >= 0.20 you can do it using Column Transformer
由于 sklearn >= 0.20 你可以使用Column Transformer
standard_transformer = Pipeline(steps=[
('standard', StandardScaler())])
minmax_transformer = Pipeline(steps=[
('minmax', MinMaxScaler())])
preprocessor = ColumnTransformer(
remainder='passthrough', #passthough features not listed
transformers=[
('std', standard_transformer , ['z']),
('mm', minmax_transformer , ['x','y'])
])
回答by MaxU
Demo:
演示:
In [90]: df = pd.DataFrame(np.random.randn(5, 3), index=list('abcde'), columns=list('xyz'))
In [91]: df
Out[91]:
x y z
a -0.325882 -0.299432 -0.182373
b -0.833546 -0.472082 1.158938
c -0.328513 -0.664035 0.789414
d -0.031630 -1.040802 -1.553518
e 0.813328 0.076450 0.022122
In [92]: from sklearn.preprocessing import MinMaxScaler
In [93]: mms = MinMaxScaler()
In [94]: df[['x','z']] = mms.fit_transform(df[['x','z']])
In [95]: df
Out[95]:
x y z
a 0.308259 -0.299432 0.505500
b 0.000000 -0.472082 1.000000
c 0.306662 -0.664035 0.863768
d 0.486932 -1.040802 0.000000
e 1.000000 0.076450 0.580891
the same result can be also achieved using sklearn.preprocessing.minmax_scale
:
同样的结果也可以使用sklearn.preprocessing.minmax_scale
:
from sklearn.preprocessing import minmax_scale
df[['x','z']] = minmax_scale(df[['x','z']])