如何将二维Pandas时间序列数组适配到Keras LSTM层?
Let's break down why you're getting that ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4 error and how to fix it:
Root Cause
LSTM layers in Keras expect input data in a 3D format: (number_of_samples, time_steps, number_of_features). Here's what went wrong in your code:
- Your
X_trainafter scaling is 2D ((3180, 8)), but you didn't reshape it to 3D before feeding into the LSTM. - You passed three values to
input_shape((1, len(X_train), x.shape[1])), which tells Keras to expect a 4D input (batch size + those three dimensions). But LSTM only accepts 3D input, hence the mismatch.
Step-by-Step Fix
Reshape your training data to 3D
After scaling, convert your 2D array into the 3D format LSTM needs. If you want to treat each row as a single time step (sequence length of 1), use:sc = StandardScaler() X_train = sc.fit_transform(X_train) print(X_train.shape) # Output: (3180, 8) # Reshape to 3D: (samples, time_steps, features) X_train = X_train.reshape((X_train.shape[0], 1, X_train.shape[1])) print(X_train.shape) # Now outputs: (3180, 1, 8)Correct the LSTM input_shape
Theinput_shapeparameter should only specify(time_steps, features)(omit the sample/batch dimension, which Keras handles automatically). Update your LSTM layer like this:classifier = Sequential() # input_shape is (time_steps, features) = (1, 8) classifier.add(LSTM(units=128, input_shape=(1, X_train.shape[2])))
Bonus: Using Longer Sequences (If Needed)
If you're working with time series and want to use past N time steps to predict future values, you'll need to create sequences first. For example, using sequences of length 10:
import numpy as np def create_sequences(data, seq_length): sequences = [] for i in range(len(data) - seq_length): # Grab consecutive seq_length rows as one sequence sequences.append(data[i:i+seq_length]) return np.array(sequences) seq_length = 10 X_train = create_sequences(X_train, seq_length) print(X_train.shape) # Output: (3170, 10, 8) # Now update input_shape to match the sequence length classifier.add(LSTM(units=128, input_shape=(seq_length, X_train.shape[2])))
内容的提问来源于stack exchange,提问作者Manthan

