1

I'm running the following code from github, but I'm getting an error. What's wrong?

https://github.com/susanli2016/Machine-Learning-with-Python/blob/master/Time%20Series%20ANN%20%26%20LSTM%20VIX.ipynb

Cell:

# scale train and test data to [-1, 1]
scaler = MinMaxScaler(feature_range=(-1, 1))
train_sc = scaler.fit_transform(train)
test_sc = scaler.transform(test)

Error:

ValueError: Expected 2D array, got 1D array instead:
array=[17.24     18.190001 19.219999 ... 10.47     10.18     11.04    ].
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.
2
  • Try to make your data in proper order. Commented Mar 26, 2019 at 23:40
  • What is train? pd.Series? np.array? If it's a series, just use .to_frame(). If it's np array, reshape it as suggested .reshape(-1,1) Commented Mar 27, 2019 at 0:17

2 Answers 2

2

The person who made that notebook was using a really old version of sklearn. In short, your features were of the form [row_1, row_2...row_n], when they should have been of the form [[row_1], [row_2]...[row_n]].

Accordingly, use this:

new_shape = (len(train), 1)

train_sc = scaler.fit_transform(np.reshape(train, new_shape))
test_sc = scaler.transform(np.reshape(test, new_shape))
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! But I got a new error: TypeError: len() takes exactly one argument (2 given)
@grc probably gmds meant (len(train), 1)
@grc @RafaelC Yup, moved new_shape out into a separate statement and forgot to shift the brackets. Edited.
Thanks. Now it returns this ERROR: Data must be 1-dimensional
If I write like this, it works, but the "test_sc" is not working. scaler = MinMaxScaler(feature_range=(-1, 1)) train = train.reshape(1,-1) train_sc = scaler.fit_transform(train)
1

Solved the problem adding the methods below, which apparently transform train and test objects to numpy arrays. Is that correct?

scaler = MinMaxScaler(feature_range=(-1, 1))
train_sc = scaler.fit_transform(train.values.reshape(-1, 1))
test_sc = scaler.transform(test.values.reshape(-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.