1
$\begingroup$

I have a time series dataset and I would like to normalize the data (diff which is of type list) as below using Min Max technique. But, I get the following error:

Code:

# split data into train and test-sets
train, test = diff[0:1486], diff[1486:2123]
from sklearn.preprocessing import MinMaxScaler
# scale train and test data to [-1, 1]
def scale(train, test):
    # fit scaler
    scaler = MinMaxScaler(feature_range=(-1, 1))
    scaler = scaler.fit(train)
    # transform train
    train = train.reshape(train.shape[0], train.shape[1])
    train_scaled = scaler.transform(train)
    # transform test
    test = test.reshape(test.shape[0], test.shape[1])
    test_scaled = scaler.transform(test)
    return scaler, train_scaled, test_scaled
# transform the scale of the data
scaler, train_scaled, test_scaled = scale(train, test)

Error:

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
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.
$\endgroup$
0
$\begingroup$

To resolve the issue, I used diff() method to remove trends in diff.diff()

$\endgroup$
0
$\begingroup$

Try this:

train, test = diff[0:1486], diff[1486:2123]
from sklearn.preprocessing import MinMaxScaler
# scale train and test data to [-1, 1]
def scale(train, test):
    # fit scaler
    scaler = MinMaxScaler(feature_range=(-1, 1))
    scaler = scaler.fit(train.reshape(-1,1))
    # transform train
    train = train.reshape(-1,1)
    train_scaled = scaler.transform(train)
    # transform test
    test = test.reshape(-1,1)
    test_scaled = scaler.transform(test)
    return scaler, train_scaled, test_scaled

# transform the scale of the data
scaler, train_scaled, test_scaled = scale(train, test)
$\endgroup$
1
  • $\begingroup$ Thanks but diff is a list that doesn't have reshape element $\endgroup$ – Rawia Sammout Apr 29 '19 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.