2
from sklearn import preprocessing

I have a data which contain positive and negative values as given below :

(Here is the csv file of the data https://drive.google.com/file/d/1-Kc3vGDazAPQ_4I7wVvG6VI9Bd9b4uCW/view?usp=sharing)

ext is: | Index | Values | | -------- | -------------- | | 1 | -5.473753 | | 2 | 54.730399 | | 3 | 0.389353 | | 4 | -4.156109 | | 5 | 65.108997 | | ... | ......... | | 733 | 14.082214 | | 734 | 107.248120 | | 735 | 54.730399 |

I am trying to use MinMaxScaler as given below:

min_max_scaler = preprocessing.MinMaxScaler()
test_scaled = min_max_scaler.fit_transform(ext)
predictions_rescaled=min_max_scaler.inverse_transform(test_scaled)

predictions_rescaled should be same as ext, because i am scaling it and then rescaling it, but surprisingly both are different. Can anyone guide me where i am making mistake in scaling-rescaling process.

Mohd Naved
  • 358
  • 3
  • 12

2 Answers2

2

MinMaxScaler scales the values in range 0 to 1 by default. If you want negative numbers after scaling, you can use StandardScaler.

Also there is no wrong in your code. Inverse_transform() is returning the old dataframe.

s_scaler = MinMaxScaler()
test_scaled = s_scaler.fit_transform(ext)
print(test_scaled)
predictions_rescaled=s_scaler.inverse_transform(test_scaled)
print()
predictions_rescaled = pd.DataFrame(predictions_rescaled)
predictions_rescaled
Prakash Dahal
  • 4,388
  • 2
  • 11
  • 25
1

Check which scikit-version you are using and whether there exists a bug in MinMaxScaler. If that is not the problem, check the way you pass in data to MinMaxScaler.

From scikit-learn documentation here, it should behaviour as expected.

I cannot reproduce your problem. It works fine. I get originally back:


import pandas as pd
from sklearn.preprocessing import MinMaxScaler

df = pd.read_csv('ext.csv', index_col=0)
scaler = MinMaxScaler()

df['minmax'] = scaler.fit_transform(df)
df['inv'] = scaler.inverse_transform(df[['minmax']])

enter image description here

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57
  • On scaling i am ok with positive numbers (in between 0 to 1), but when i rescaled these numbers i should be getting my original ext data (which contain positive and negative numbers), but this is not happening with the my current code. – Mohd Naved Jan 24 '21 at 07:20
  • I get the value back with sckit-learn v 0.23 – Prayson W. Daniel Jan 24 '21 at 07:41