1

This code returns the expected results. But there are 2 pandas methods involved. Can I use only 1 method or remove pandas from fit_transform?

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
data = [-1, 2,1, 18]
scaler.fit_transform(pd.DataFrame(pd.Series(data)))

array([[0.        ],
       [0.15789474],
       [0.10526316],
       [1.        ]])

Update: I tried feeding the list, but got an error:

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
data = [-1, 2,1, 18]
scaler.fit_transform(data)

# ValueError: Expected 2D array, got 1D array instead 

This works, but getting wrong results:

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
data = [-1, 2,1, 18]
scaler.fit_transform([data])

# array([[0., 0., 0., 0.]])
1
  • Perhaps reading the docs would have helped? It does say the function is meant to take lists, arrays, or similar. Not sure where you got the idea to introduce pandas objects here. Commented Dec 29, 2018 at 10:51

2 Answers 2

2

You can directly apply fit_transform like this without pandas

data = np.array(data).reshape(-1,1).astype(np.float32)
scaler.fit_transform(data)
Sign up to request clarification or add additional context in comments.

Comments

1

Option 1:

You can directly create a dataframe from the list and feed it to the scaler, which would require any reshaping.

scaler.fit_transform(pd.DataFrame(data))

Option 2:

If you want to do with numpy,

scaler.fit_transform(np.array(data)[:,np.newaxis])

or you can go with the suggestion given by @Oswald

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.