Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sklearn transform error: Expected 2D array, got 1D array instead

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?

like image 495
user58519 Avatar asked Dec 14 '22 10:12

user58519


2 Answers

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).

like image 197
Aryerez Avatar answered May 15 '23 12:05

Aryerez


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)) 
like image 31
GIRISH kuniyal Avatar answered May 15 '23 13:05

GIRISH kuniyal