Я разрабатываю программу прогнозирования акций с использованием tenorflow2.1. Моя переменная predictions
должна быть 2-мерной переменной в конце моей программы, но только одномерной. Возвращенная ошибка:
Traceback (most recent call last):
File "/Users/owner/Desktop/algo/predict.py", line 120, in <module>
valid['Predictions'] = predictions
File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 3487, in __setitem__
self._set_item(key, value)
File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 3563, in _set_item
self._ensure_valid_index(value)
File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 3540, in _ensure_valid_index
value = Series(value)
File "/usr/local/lib/python3.7/site-packages/pandas/core/series.py", line 314, in __init__
data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
File "/usr/local/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 729, in sanitize_array
raise Exception("Data must be 1-dimensional")
Exception: Data must be 1-dimensional
Вот мой код:
import requests
import math
import numpy as np
import pandas as pd
import pandas_datareader as web
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import LSTM, Embedding
from tensorflow.keras.layers import Dense
from sklearn.preprocessing import MinMaxScaler
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
from datetime import datetime, date
plt.style.use('fivethirtyeight')
today = date.today()
df = web.DataReader('GC=F', data_source='yahoo', start='2019-02-14', end=str(today))
#print(df)
plt.figure(figsize=(16,8))
plt.title('GOLD PRICE HISTORY')
plt.plot(df['Close'])
plt.xlabel('Date', fontsize=18)
plt.ylabel('Close price USD($)', fontsize=18)
#plt.show()
#get closing price
data = df.filter(['Close'])
#get closing price values
dataset = data.values
#set training data length to 91% of total data set
training_data_len = math.ceil(len(dataset))
print(training_data_len)
#Scale the data
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(dataset)
#create training data set
#create the scaled training data set
train_data = scaled_data[0: training_data_len , :]
#split the data into x-train and y-train datasets
x_train = []
y_train = []
for i in range(20, len(train_data)):
x_train.append(train_data[i-20:i, 0])
y_train.append(train_data[i, 0])
if i<= 20:
#print(x_train)
#print(y_train)
pass
#convert x-train and y-train to numpy arrays to train models
x_train, y_train = np.array(x_train), np.array(y_train)
#reshape the data, LSTM model expects 3D dataset
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
#Build LSTM MODEL
model = tf.keras.Sequential([
#tf.keras.layers.Embedding(encoder.vocab_size, 64),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
tf.keras.layers.Dense(25, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='mean_squared_error',
optimizer=tf.keras.optimizers.Adam(1e-4),
metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=1, epochs=1)
#create testing data set
#creat new array containing scaled values
test_data = scaled_data[training_data_len - 20: , :]
print(test_data.shape)
#create the datasets x-test and y-test
x_test=[]
y_test=dataset[training_data_len:, :]
for i in range(20, len(test_data)+1):
x_test.append(test_data[i-20:i, 0])
#convert data to numpy array
x_test = np.array(x_test)
#reshape data to 3D
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
print(x_test.shape)
#Get predicted price values
predictions = model.predict(x_test)
predictions = scaler.inverse_transform(predictions)
print(predictions.shape)
#get root mean squared error
rmse = np.sqrt(((predictions - y_test) ** 2).mean())
print(rmse)
#Plot the data
train = data[:training_data_len]
valid = data[training_data_len:]
valid['Predictions'] = predictions
#Visualize the data
plt.figure(figsize=(16,8))
plt.title('Model')
plt.xlabel('Date', fontsize=18)
plt.ylabel('Close Price USD ($)', fontsize=18)
plt.plot(train['Close'])
plt.plot(valid[['Close', 'Predictions']])
plt.legend(['Train', 'Val', 'Predictions'], loc='lower right')
plt.show()
Это ошибка из-за Pandas, Numpy или Tensorflow? И что можно сделать, чтобы удовлетворить эту ошибку? Я искал помощи на GitHub, но они перенаправили меня в Stackoverflow.
Спасибо.