a['Close'].replace(0, np.nan, inplace=True) a['Close'].fillna(method='ffill', inplace=True) values = a['Close'].values.reshape(-1, 1) values = values.astype('float32') scaler = MinMaxScaler(feature_range=(0, 1)) scaled = scaler.fit_transform(values) train_size = int(len(scaled) * 0.8) valid_size = int(len(scaled) * 0.9) test_size = len(scaled) - train_size train, valid = scaled[0:train_size, :], scaled[train_size:valid_size, :] test = scaled[valid_size:, :] train_dict = create_data_dict(train, 4) test_dict = create_data_dict(valid, 4) model = LSTMRegressor(input_size=1, hidden_size=32, dropout_rate=0.5 ) predictor = Predictor(model=model, train_data=train_dict, test_data=test_dict, batch_size=64, use_cuda=True) predictor.fit(300, 30) results = predictor.predict(predictor.test_loader).reshape(1, -1)[0] test_y = test_dict["y"].numpy().reshape(1, -1)[0] pyplot.plot(results, label='predict') pyplot.plot(test_y, label='true') pyplot.legend() pyplot.show()
train_size:train_size + valid_size, :] test = scaled_features[train_size + valid_size:, :] train_dict = create_data_dict(train, 4) valid_dict = create_data_dict(valid, 4) test_dict = create_data_dict(test, 2) model = LSTMRegressor(input_size=len(columns), hidden_size=32, dropout_rate=0.5) predictor = Predictor(model=model, train_data=train_dict, test_data=valid_dict, batch_size=64, use_cuda=True) predictor.fit(400, 50) results = predictor.predict(predictor.test_loader).reshape(1, -1)[0] test_y = valid_dict["y"].numpy().reshape(1, -1)[0] print(test_y) axes = pyplot.gca() new_results = results * data_range + data_min new_test_y = test_y * data_range + data_min initial_date = datetime(2018, 4, 15) final_result = [] for quote, predict in zip(new_test_y, new_results): initial_date = initial_date + timedelta(days=1) date_str = initial_date.strftime("%Y-%m-%d") final_result.append({ "date": date_str, "quote": float(quote),