RNN神经网络 python

发布时间:2024年01月21日
import pandas as pd
import numpy as np
data=pd.read_cvs('data.cvs')
data.head()

price=data.loc[:,'close']
#归一化处理
price_norm=price/max(price)
//可视化
%matplotlib inline
from matplotilb import pyplot as plt
fig=plt.figure(figsize=(8,5))
plt.pot(price)
plt.title('data vs price')
plt.xlable('time')
plt.ylable('price')
plt.show()

//x,y赋值
 def extract_data(data,time_step):
        x=[]
        y=[]
        for i in range(len(data)-time_step)
            x.append([a for a in data[i:i+time_step]])
            y.append(data[i+time_step])
        x=np.array(x)
        x=x.reshape(x.shape[0],x.shape[1],1)
        return x,y
//数据提取
x,y=extract_data(price_norm,time_step)

//set up the model
from keras.models import Sequential
from keras.layers import Dense,SimpleRNN
model=()
model.add(SimpleRNN(units=5,input_shape=(time_step,1),activation='relu'))
model.add(Dense(units=1,actication='linear'))
model.comple(optimizer='adam',loss='mean_squared_error')
model.summary()

//模型训练
model.fit(x,y,batch_size=30,epochs=200)

//预测
y_train_price=model.preict(x)*max(price)
y_train=[i*max(price) for i in y]


fig1=plt.figure(figsize=(8,5))
plt.pot(y_train_price)
plt.pot(y_train)
plt.title('data vs price')
plt.xlable('time')
plt.ylable('price')
plt.show()

//测试数据
data_test=pd.read_csv('data_test.csv')
data_test.head()
price_test=data_test.loc[:,'close']
price_test_norm=price_test/max(price)
//提取数据
x_test_norm,y_test_norm=extract_data(price_test_norm,time_step)
y_test_price=model.preict(x_test_norm)*max(price)
y_test=[i* max(price) for i in  y_test_norm]

fig2=plt.figure(figsize=(8,5))
plt.pot(y_test_price,lable='y_test_price')
plt.pot(y_test,lable='y_test')
plt.title('data vs price')
plt.xlable('time')
plt.ylable('price')
plt.show()

//存储数据
result_y_test=np.array(y_test).reshape(-1,1)
result_y_test_predict=y_test_predict
result=np.concatenate((result_y_test,result_y_test_predict),axis=1)
result=pd.DataFrame(result,columns=['real_price_test','predict_price_test'])
result.to_csv("zz.csv")

文章来源:https://blog.csdn.net/zjhzyw/article/details/135722579
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。