Pandas scale multiple columns at once and inverse transform with groupby()

ε祈祈猫儿з 提交于 2019-12-11 15:09:56

问题


I have a dataframe like below.I want to apply two MinMaxscalers on x_data ad y_data on multiple columns and then inverse transform should give me the actual values.Please suggest and help me on this.Thanks in advance

DataFrame:

                 X_data                             y_data       
   Customer     0      1      2      3       Customer      0      1
0    A         855.0  989.0  454.0  574.0        A       395.0  162.0
1    A         989.0  454.0  574.0  395.0        A       162.0  123.0
2    A         454.0  574.0  395.0  162.0        A       123.0  342.0
3    A         574.0  395.0  162.0  123.0        A       342.0  232.0
4    A         395.0  162.0  123.0  342.0        A       232.0  657.0
5    B         875.0  999.0  434.0  564.0        B       345.0  798.0
6    B         999.0  434.0  564.0  345.0        B       798.0  815.0
7    B         434.0  564.0  345.0  798.0        B       815.0  929.0
8    B         564.0  345.0  798.0  815.0        B       929.0  444.0
9    B         345.0  798.0  815.0  929.0        B       444.0  554.0
10   B         798.0  815.0  929.0  444.0        B       554.0  395.0
11   B         815.0  929.0  444.0  554.0        B       395.0  768.0

I can do it for one column using MinMaxScaler with below line but i want to make it for multiple columns

    #to get multilevel to single level
      X_data.columns = list(X_data.columns.levels[1])
      #scaling per user
      scaled_xdata = X_data.groupby('Customer')[0].transform(lambda s: x_scaler.fit_transform(s.values.reshape(-1,1)).ravel())
   #storing into the df
    scaled_xdata =pd.concat([X_data[['Customer']] , scaled_xdata] , axis=1)

I would like to perform inverse transform on the data to get the actual values for multple columns.here is the code which i tried for one column

  scaled_xdata_inv = scaled_xdata.groupby('Customer')[0].transform(lambda s: x_scaler.inverse_transform(s.values.reshape(-1,1)).ravel())
scaled_xdata_inv  =pd.concat([X_data[['Customer']] , scaled_xdata_inv] , axis=1)
scaled_xdata_inv

After inverse_transform , The output for 0 column is wrong for Customer A and got right values for Customer B.can you please help me on this

Output:

Customer    0
0   A   851.464646
1   A   999.000000
2   A   409.959596
3   A   542.080808
4   A   345.000000
5   B   875.000000
6   B   999.000000
7   B   434.000000
8   B   564.000000
9   B   345.000000
10  B   798.000000
11  B   815.000000

回答1:


The MinMaxScalar can accept multi pandas numeric serieses at once, and scales them column-wise, so you can simply do:

x_scaler = MinMaxScaler()
scaled_xdata = x_scaler.fit_transform(df.iloc[:, 1:])
scaled_xdata_inv = x_scaler.inverse_transform(scaled_xdata)

No need for groupbys or lambdas



来源:https://stackoverflow.com/questions/58624544/pandas-scale-multiple-columns-at-once-and-inverse-transform-with-groupby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!