# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
# In[2]:
path = "F:/Anaconda_program/股票预测/data_stocks.csv"
data = pd.read_csv(filepath_or_buffer=path)
data_use = data.drop(['DATE'],axis=1)
# In[3]:
data_use =data_use.iloc[:,0].tolist()
# In[4]:
data_use[:3]
# In[5]:
##对数据标准化处理,对减小loss有极大作用
scaler = MinMaxScaler(feature_range=(-1,1))
scaler.fit(data_use)
data_use = scaler.transform(data_use)
# In[6]:
data.head()
# In[7]:
max_time = 5
# In[8]:生成输入数据
data_x = []
data_y = []
for i in range(len(data_use)-max_time):
data_x.append(data_use[i:i+max_time])
data_y.append(data_use[i+max_time])
# In[9]:
def shuffle_data(data,labels):
assert len(data) == len(labels)
idx = np.arange(len(labels))
np.random.shuffle(idx)
return data[idx],labels[idx]
# In[10]:
data_x = np.array(data_x)
data_y = np.array(data_y)
data_x = data_x[:,:,np.newaxis] ###此处需要加1维度,2维变3维,[-1,5]变为[-1,5,1]
data_y = data_y[:,np.newaxis]
data_shuffle_x,data_shuffle_y = shuffle_data(data_x,data_y)
# ratio = 0.8
# len_data = data_shuffle_x.shape[0]
# index_train = np.int(len_data*ratio)
# train_x = data_shuffle_x[:index_train]
# train_y = data_shuffle_y[:index_train]
#
# test_x = data_shuffle_x[index_train:]
# test_y = data_shuffle_y[index_train:]
#
# In[11]:
ratio = 0.8
len_data = data_shuffle_x.shape[0]
index_train = np.int(len_data*ratio)
train_x = data_x[:index_train]
train_y = data_y[:index_train]
test_x = data_x[index_train:]
test_y = data_y[index_train:]
# In[12]:
print(train_x.sha
LSTM程序练习
最新推荐文章于 2023-12-27 00:48:33 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)