21

I use sklearn to transform data with this code.

sc = MinMaxScaler()

test= df['outcome']
y = sc.fit_transform(test) 

It show error like this.

ValueError: Expected 2D array, got 1D array instead:
array=[ 21000. 36000.  5000. ...  7000.  12000.  11000.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

How to fix it?

1
  • try test.reshape(len(test), 1) and then apply fit_transform. Commented Oct 22, 2019 at 6:32

3 Answers 3

31

If I remember correctly, MinMaxScalar can accept a pandas dataframe but not a series, so just do test = df[['outcome']] (dataframe with one column) instead of test = df['outcome'] (a series).

Sign up to request clarification or add additional context in comments.

Comments

5

MinMaxScaler required numpy input shape as (num_sample,1) but you are giving input as shape (num_sample,) Try this code :

sc = MinMaxScaler()
test= df['outcome'].values #convert to numpy array
y = sc.fit_transform(test.reshape(-1,1)) 

Comments

0

Use the error message in your favor. Use this code:

y = sc.fit_transform(np.reshape(test, (-1,1)))

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.