用于在 Pandas 数据框中缩放列的 lambda 函数返回:“'float' 对象没有属性 'min'"; [英] lambda function to scale column in pandas dataframe returns: "'float' object has no attribute 'min'"
问题描述
我刚刚开始使用 Python 和机器学习,遇到了一个我自己或任何其他在线资源都无法解决的问题.我正在尝试使用 lambda 函数按以下方式缩放 Pandas 数据框中的列:
I am just getting started in Python and Machine Learning and have encountered an issue which I haven't been able to fix myself or with any other online resource. I am trying to scale a column in a pandas dataframe using a lambda function in the following way:
X['col1'] = X['col1'].apply(lambda x: (x - x.min()) / (x.max() - x.min()))
并收到以下错误消息:
'float' 对象没有属性 'min'
'float' object has no attribute 'min'
我尝试将数据类型转换为整数并返回以下错误:
I have tried to convert the data type into integer and the following error is returned:
'int' 对象没有属性 'min'
'int' object has no attribute 'min'
我相信我得到了一些非常基本的错误,希望任何人都能指出正确的方向.
I believe I am getting something pretty basic wrong, hope anyone can point me in the right direction.
推荐答案
我认为 apply here 没有必要,因为存在更快的矢量化解决方案 - 将 x
更改为列 X['col1']代码>:
I think apply here is not necessary, because exist faster vectorized solution - change x
to column X['col1']
:
X = pd.DataFrame({'col1': [100,10,1,20,10,-20,200]})
X['col2'] = (X['col1'] - X['col1'].min()) / (X['col1'].max() - X['col1'].min())
print (X)
col1 col2
0 100 0.545455
1 10 0.136364
2 1 0.095455
3 20 0.181818
4 10 0.136364
5 -20 0.000000
6 200 1.000000
就像@meW 在评论中指出的另一个解决方案是使用 <代码>MinMaxScaler:
Like @meW pointed in comments another solution is use MinMaxScaler
:
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
X['col2'] = min_max_scaler.fit_transform(X[['col1']])
print (X)
col1 col2
0 100 0.545455
1 10 0.136364
2 1 0.095455
3 20 0.181818
4 10 0.136364
5 -20 0.000000
6 200 1.000000
这篇关于用于在 Pandas 数据框中缩放列的 lambda 函数返回:“'float' 对象没有属性 'min'";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!