TransWikia.com

Time series normalization using min max technique

Data Science Asked on August 24, 2021

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.

2 Answers

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

Answered by Rawia Sammout on August 24, 2021

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)

Answered by Juan Esteban de la Calle on August 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP