

LSTMで予測したいと思っており、データを学習データとテストデータに分け、精度を検証しています。ゆくゆくは学習データで学習させ、データ外の範囲を予測したいと考えていますがそのようなことは可能でしょうか。ご回答よろしくお願いします。

[code]

    import numpy as np
    import pandas as pd
    from matplotlib import pyplot as plt
    import seaborn as sns
    import time
    
    df = pd.read_csv("Book99.csv", encoding='windows-1252', parse_dates=['date'])
    print(df)
    df['date'] = pd.to_datetime(df['date'])
    
    look_back = 100
    
    sample_size = len(df) - look_back
    
    past_size = int(sample_size*0.8)
    future_size = sample_size - past_size +1
    
    
    def make_dataset(raw_data, look_back=25):
        _X = []
        _y = []
    
        for i in range(len(raw_data) - look_back):
            _X.append(raw_data[i : i + look_back])
            _y.append(raw_data[i + look_back])
        _X = np.array(_X).reshape(len(_X), look_back, 1)
        _y = np.array(_y).reshape(len(_y), 1)
    
        return _X, _y
    
    from sklearn import preprocessing
    
    columns = list(df.columns)
    del columns[0]
    
    
    Xs = []
    for i in range(len(columns)):
        Xs.append(preprocessing.minmax_scale(df[columns[i]]))
    Xs = np.array(Xs)
    
    
    X_humid, y_humid = make_dataset(Xs[1], look_back=look_back)
    X_dlh, y_dlh = make_dataset(Xs[2], look_back=look_back)
    
    X_con = np.concatenate([X_humid,X_dlh], axis=2)
    
    X = X_humid
    y = y_dlh
    
    
    X_past = X[:past_size]
    X_future = X[past_size-1:]
    y_past = y[:past_size]
    y_future = y[past_size-1:]
    
    
    X_train = X_past
    y_train = y_past
    
    import tensorflow as tf
    from tensorflow.keras.layers import Input, LSTM, Dense, BatchNormalization
    from tensorflow.keras.models import Model
    from tensorflow.keras.optimizers import SGD, Adam
    
    
    def create_LSTM_model():
        input = Input(shape=(np.array(X_train).shape[1], np.array(X_train).shape[2]))
        x = LSTM(64, return_sequences=True)(input)
        x = BatchNormalization()(x)
        x = LSTM(64)(x)
        output = Dense(1, activation='relu')(x)
        model = Model(input, output)
        return model
    
    model = create_LSTM_model()
    model.summary()
    model.compile(optimizer=Adam(learning_rate=0.0001), loss='mean_squared_error')
    
    history = model.fit(X_train, y_train, epochs=20, batch_size=64, verbose=1)
    
    predictions = model.predict(X_past)
    future_predictions = model.predict(X_future)
    
    
    plt.figure(figsize=(18, 9))
    plt.plot(df['date'][look_back:], y, color="b", label="true_cloud_cover")
    plt.plot(df['date'][look_back:look_back + past_size], predictions, color="r", linestyle="dashed", label="prediction")
    plt.plot(df['date'][-future_size:], future_predictions, color="g", linestyle="dashed", label="future_predisction")
    plt.legend()
    plt.show()
    
[/code]

結果  
[![結果](https://i.stack.imgur.com/k1ZKR.png)](https://i.stack.imgur.com/k1ZKR.png)


CC BY-SA 4.0
2023-10-24T11:55:35.623
96568
2023-10-24T16:29:51.197
2023-10-24T16:29:51.197
3060
60167
question
python
LSTMで予測をしたい。