使用 sklearn 缩放的 pandas 数据框列 [英] pandas dataframe columns scaling with sklearn
问题描述
我有一个包含混合类型列的 Pandas 数据框,我想将 sklearn 的 min_max_scaler 应用于某些列.理想情况下,我想就地进行这些转换,但还没有想出一种方法来做到这一点.我编写了以下有效的代码:
将pandas导入为pd将 numpy 导入为 np从 sklearn 导入预处理scaler = preprocessing.MinMaxScaler()dfTest = pd.DataFrame({'A':[14.00,90.20,90.95,96.27,91.21],'B':[103.02,107.26,110.35,114.23,114.68],'C':['big'big'','大','小','小']})min_max_scaler = preprocessing.MinMaxScaler()def scaleColumns(df, cols_to_scale):对于 cols_to_scale 中的 col:df[col] = pd.DataFrame(min_max_scaler.fit_transform(pd.DataFrame(dfTest[col])),columns=[col])返回 df测试乙丙0 14.00 103.02 大1 90.20 107.26 小2 90.95 110.35 大3 96.27 114.23 小4 91.21 114.68 小scaled_df = scaleColumns(dfTest,['A','B'])缩放_df乙丙0 0.000000 0.000000 大1 0.926219 0.363636 小2 0.935335 0.628645 大3 1.000000 0.961407 小4 0.938495 1.000000 小
我很好奇这是否是进行此转换的首选/最有效方式.有没有办法可以更好地使用 df.apply ?
我也很惊讶我无法让以下代码工作:
bad_output = min_max_scaler.fit_transform(dfTest['A'])
如果我将整个数据帧传递给缩放器,它会起作用:
dfTest2 = dfTest.drop('C',axis = 1)good_output = min_max_scaler.fit_transform(dfTest2)good_output
我很困惑为什么将系列传递给定标器会失败.在我上面的完整工作代码中,我希望只将一个系列传递给缩放器,然后将数据框列 = 设置为缩放的系列.我在其他几个地方看到过这个问题,但没有找到好的答案.任何帮助理解这里发生的事情将不胜感激!
我不确定以前版本的 pandas
是否阻止了这种情况,但现在以下代码段对我来说非常适合,并且完全符合您的要求无需使用 apply
I have a pandas dataframe with mixed type columns, and I'd like to apply sklearn's min_max_scaler to some of the columns. Ideally, I'd like to do these transformations in place, but haven't figured out a way to do that yet. I've written the following code that works:
import pandas as pd
import numpy as np
from sklearn import preprocessing
scaler = preprocessing.MinMaxScaler()
dfTest = pd.DataFrame({'A':[14.00,90.20,90.95,96.27,91.21],'B':[103.02,107.26,110.35,114.23,114.68], 'C':['big','small','big','small','small']})
min_max_scaler = preprocessing.MinMaxScaler()
def scaleColumns(df, cols_to_scale):
for col in cols_to_scale:
df[col] = pd.DataFrame(min_max_scaler.fit_transform(pd.DataFrame(dfTest[col])),columns=[col])
return df
dfTest
A B C
0 14.00 103.02 big
1 90.20 107.26 small
2 90.95 110.35 big
3 96.27 114.23 small
4 91.21 114.68 small
scaled_df = scaleColumns(dfTest,['A','B'])
scaled_df
A B C
0 0.000000 0.000000 big
1 0.926219 0.363636 small
2 0.935335 0.628645 big
3 1.000000 0.961407 small
4 0.938495 1.000000 small
I'm curious if this is the preferred/most efficient way to do this transformation. Is there a way I could use df.apply that would be better?
I'm also surprised I can't get the following code to work:
bad_output = min_max_scaler.fit_transform(dfTest['A'])
If I pass an entire dataframe to the scaler it works:
dfTest2 = dfTest.drop('C', axis = 1)
good_output = min_max_scaler.fit_transform(dfTest2)
good_output
I'm confused why passing a series to the scaler fails. In my full working code above I had hoped to just pass a series to the scaler then set the dataframe column = to the scaled series. I've seen this question asked a few other places, but haven't found a good answer. Any help understanding what's going on here would be greatly appreciated!
I am not sure if previous versions of pandas
prevented this but now the following snippet works perfectly for me and produces exactly what you want without having to use apply
>>> import pandas as pd
>>> from sklearn.preprocessing import MinMaxScaler
>>> scaler = MinMaxScaler()
>>> dfTest = pd.DataFrame({'A':[14.00,90.20,90.95,96.27,91.21],
'B':[103.02,107.26,110.35,114.23,114.68],
'C':['big','small','big','small','small']})
>>> dfTest[['A', 'B']] = scaler.fit_transform(dfTest[['A', 'B']])
>>> dfTest
A B C
0 0.000000 0.000000 big
1 0.926219 0.363636 small
2 0.935335 0.628645 big
3 1.000000 0.961407 small
4 0.938495 1.000000 small
这篇关于使用 sklearn 缩放的 pandas 数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!