Home>

Reference site
Using this site as a reference, I was trying to predict behavior by deep learning using my own x and y coordinate data (walking trajectory). I don't know if it matches, but when I changed it to my own data and tried to execute it, when I tried to plot it, the following error message came out.

Error message
Length mismatch: Expected axis has 2 elements, new values ​​have 1 elements
Applicable source code
// ---------------
def _load_data (data, n_prev = 20):
    "" "
    data should be pd.DataFrame ()
    "" "
    docX, docY = [], []
    for i in range (len (data) -n_prev):
        docX.append (data.iloc [i: i + n_prev] .as_matrix ())
        docY.append (data.iloc [i + n_prev] .as_matrix ())
    alsX = np.array (docX)
    alsY = np.array (docY)
    return alsX, alsY
def train_test_split (df_posixy, test_size = 0.1, n_prev = 20):
    "" "
    This just splits data to training and testing parts
    "" "
    ntrn = round (len (df_posixy) * (1-test_size))
    ntrn = int (ntrn)
    X_train, y_train = _load_data (df_posixy.iloc [0: ntrn], n_prev)
    X_test, y_test = _load_data (df_posixy.iloc [ntrn:], n_prev)
    return (X_train, y_train), (X_test, y_test)
length_of_sequences = 20
(X_train, y_train), (X_test, y_test) = train_test_split (df_posixy, n_prev = length_of_sequences)
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import LSTM

in_out_neurons = 2
hidden_neurons = 500
model = Sequential ()
model.add (LSTM (hidden_neurons, batch_input_shape = (None, length_of_sequences, in_out_neurons), return_sequences = False))
model.add (Dense (in_out_neurons))
model.add (Activation ("linear"))
model.compile (loss = "mean_squared_error", optimizer = "rmsprop", metrics = ['accuracy'])
model.fit (X_train, y_train, batch_size = 600, nb_epoch = 15, validation_split = 0.05)
// --------- So far we have been able to execute
// The part where the error occurred when trying to plot below
predicted = model.predict (X_test)
dataf = pd.DataFrame (predicted [: 100])
dataf.columns = ["predict"]
dataf ["input"] = y_test [: 100]
dataf.plot (figsize = (15, 5))
Data you prepared

Continues 454 lines

Supplemental information

I don't know if it is made in the first place, so if there is something wrong, please.

  • Answer # 1

    model.save_weights ('mnist_mlp_weights.hdf5') // Save weights
    model.load_weights ('mnist_mlp_weights.hdf5') // load weights

    predicted = model.predict (X_train)

    df_predicted = pd.DataFrame (predicted, columns = list ('xy'))
    plt.plot (df_predicted ["x"], df_predicted ["y"], 'ro')

Related articles