Reputation: 378
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.
Upvotes: 2
Views: 4715
Reputation: 15568
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']])
Upvotes: 1
Reputation: 4875
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
Upvotes: 2