简体   繁体   English

如何使用 sklearn.preprocessing.normalize 规范化 DataFrame 的列?

[英]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.

相关问题 sklearn.preprocessing.normalize 中的规范参数 - norm parameters in sklearn.preprocessing.normalize sklearn.preprocessing.normalize如何对数据进行归一化,并且可以在具有均值和标准差的新数据上进行复制吗? - How does sklearn.preprocessing.normalize normalize data, and can I replicate on new data with mean and standard deviation? scipy.linalg.norm与sklearn.preprocessing.normalize不同吗? - scipy.linalg.norm different from sklearn.preprocessing.normalize? sklearn.preprocessing.normalize考虑哪个L1规范? - Which L1 norm does sklearn.preprocessing.normalize consider? sklearn.preprocessing.normalize中的norm ='l2'对于矩阵归一化有什么作用? - What does norm='l2' in sklearn.preprocessing.normalize do for matrix normalization? python sklearn:“ sklearn.preprocessing.normalize(X,norm ='l2')”和“ sklearn.svm.LinearSVC(penalty ='l2')”之间有什么区别 - python sklearn: what is the different between “sklearn.preprocessing.normalize(X, norm='l2')” and “sklearn.svm.LinearSVC(penalty='l2')” numpy.linalg.norm是否可以将sklearn.preprocessing.normalize(X,norm ='l1',)替换为矩阵的L1-norm? - Can numpy.linalg.norm replace sklearn.preprocessing.normalize(X, norm='l1',) for L1-norm of matrix? 如何使用 sklearn.preprocessing 的 StandardScaler 仅标准化一列 - How to normalize only one column using sklearn.preprocessing's StandardScaler 规范化 pandas dataframe 中的列 - Normalize columns in pandas dataframe 标准化 dataframe 的列 - Normalize columns of a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM