[英]How to normalize the columns of a DataFrame using sklearn.preprocessing.normalize?
is there a way to normalize the columns of a DataFrame using sklearn's normalize?有没有办法使用 sklearn 的归一化来归一化 DataFrame 的列? I think that by default it normalizes rows我认为默认情况下它会规范化行
For example, if I had df:
A B
1000 10
234 3
500 1.5
I would want to get the following:我想得到以下内容:
A B
1 1
0.234 0.3
0.5 0.15
Why do you need sklearn
?为什么需要sklearn
?
Just use pandas:只需使用熊猫:
>>> df / df.max()
A B
0 1.000 1.00
1 0.234 0.30
2 0.500 0.15
>>>
You can using div
after get the max
您可以在获得max
后使用div
df.div(df.max(),1)
Out[456]:
A B
0 1.000 1.00
1 0.234 0.30
2 0.500 0.15
sklearn
defaults to normalize rows with theL2
normalization . sklearn
默认使用L2
normalization规范化行。 Both of these arguments need to be changed for your desired normalization by the maximum value along columns:这两个参数都需要更改为您想要的标准化沿列的最大值:
from sklearn import preprocessing
preprocessing.normalize(df, axis=0, norm='max')
#array([[1. , 1. ],
# [0.234, 0.3 ],
# [0.5 , 0.15 ]])
From the documentation从文档
axis : 0 or 1, optional (1 by default) axis used to normalize the data along. axis : 0 或 1,可选(默认为 1)轴,用于规范化数据。 If 1, independently normalize each sample, otherwise (if 0) normalize each feature.如果为 1,则独立标准化每个样本,否则(如果为 0)标准化每个特征。
So just change the axis.所以只需改变轴。 Having said that, sklearn
is an overkill for this task.话虽如此, sklearn
对这项任务来说sklearn
过分了。 It can be achieved easily using pandas.使用熊猫可以轻松实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.