Ожидаемый двумерный массив, вместо него получен одномерный массив: LSTM Прогноз временных рядов - PullRequest
0 голосов
/ 03 апреля 2020

У меня возникла проблема, и было бы неплохо, если бы вы могли помочь мне с этим. Это набор данных временных рядов за месяц, и я пытаюсь обучить модели LSTM. Он говорит

"Ожидаемый двумерный массив, вместо него получен одномерный массив:"

n_input = 12
n_features = 1

def walk_forward_validation(data, n_test):
    #train test split
    train, test = data[:-n_test], data[-n_test:]
    #scaling the data
    scaler = MinMaxScaler()
    scaler.fit(train)
    train = scaler.transform(train)
    test = scaler.transform(test)

    generator = TimeseriesGenerator(train, train, length=n_input, batch_size=6)
    # fit model
    model = Sequential()
    model.add(LSTM(200, activation='relu', input_shape=(n_input, 1)))
    model.add(Dropout(0.15))
    model.add(Dense(1))
    model.compile(optimizer='adam', loss='mse')
    model.fit_generator(generator, epochs=90)
    batch = train[-n_input:].reshape(1, n_input, 1)
    for i in range(n_input):
        pred_list.append(model.predict(batch)[0])
        batch = np.append(batch[:, 1:,:], [[pred_list[i]]], axis=1)
    #Invers Transform - Scaling
    df_predict = pd.DataFrame(scaler.inverse_transform(pred_list), index=data[-n_input:].index, columns=['Predictions'])#new column named "predictions"
    df_test = pd.concat([data, df_predict], axis=1)
    # estimate prediction error
    error = measure_rmse(test, df_test.Predictions)
    print(' > %.3f' % error)
    return error

# repeat evaluation of a config
def repeat_evaluate(data, n_test, n_repeats=10):
    # fit and evaluate the model n times
    scores = [walk_forward_validation(data, n_test) for _ in range(n_repeats)]
    return scores

series = read_csv('master2.csv', parse_dates=True, squeeze=True, index_col=0)
series = series['Product A']
series = series.resample('M').sum()
data = series.values
# data split
n_test = 12

walk_forward_validation(data,n_test)

Это сообщение об ошибке:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-5b6f9b7f1b19> in <module>
      8 config = [14, 50, 100, 100]
      9 
---> 10 walk_forward_validation(data,n_test,config)
     11 
     12 # grid search

<ipython-input-24-d5e0ed690a02> in walk_forward_validation(data, n_test, cfg)
     46     train, test = train_test_split(data, n_test)
     47     # fit model
---> 48     model = model_fit(train, cfg)
     49     # seed history with training dataset
     50     history = [x for x in train]

<ipython-input-24-d5e0ed690a02> in model_fit(train, config)
     13     # prepare data
     14     scaler = MinMaxScaler()
---> 15     scaler.fit(train)
     16     train = scaler.transform(train)
     17     test = scaler.transform(test)

~\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py in fit(self, X, y)
    323         # Reset internal state before fitting
    324         self._reset()
--> 325         return self.partial_fit(X, y)
    326 
    327     def partial_fit(self, X, y=None):

~\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py in partial_fit(self, X, y)
    351         X = check_array(X, copy=self.copy,
    352                         estimator=self, dtype=FLOAT_DTYPES,
--> 353                         force_all_finite="allow-nan")
    354 
    355         data_min = np.nanmin(X, axis=0)

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    519                     "Reshape your data either using array.reshape(-1, 1) if "
    520                     "your data has a single feature or array.reshape(1, -1) "
--> 521                     "if it contains a single sample.".format(array))
    522 
    523         # in the future np.flexible dtypes will be handled like object dtypes

ValueError: Expected 2D array, got 1D array instead:
array=[18453. 20623. 19799. 36075. 41666. 40402. 41361. 39468. 32637. 26803.
 20502. 15567. 13926. 12666. 14943. 17235. 22807. 28891. 37036. 40294.
 37125. 40977. 32417. 19655. 16868. 15205. 10128. 11679. 13381. 14620.
 22849. 28210. 31709. 36288. 36977. 27162. 34388. 26083. 21097. 14910.
 10680.  9058. 10223. 16225. 18788. 24888. 29853. 24968. 39507. 35386.
 26019. 31129. 23560. 15791. 12883. 12322. 13016. 12703. 18037. 16198.].
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.

...