lstm预测测试版本

  

# - 编码:utf - 8 -
# !/bin/env python
#身份验证:kailkaka
#数据:2019-3-3
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
进口tensorflow从tensorflow进口keras tf

进口numpy np

进口进口警告熊猫从sklearn pd
从sklearn进口预处理
。进口matplotlib预处理进口StandardScaler

matplotlib.use (qt4agg)
# matplotlib进口。pyplot plt
# plt.switch_backend (gg)

  

matplotlib.rcParams['字体。无衬线']=[' SimHei ']
matplotlib.rcParams [' font.family ']='无衬线的
matplotlib.rcParams['轴。unicode_minus ']=False
进口sys
试题:
=系统参数。除了:argv [1]

打印“请输入参数…“
退出(1)
warnings.filterwarnings (“ignore")
重载(sys)
sys.setdefaultencoding (utf - 8)
类ZcSummary:
def read_csv(自我):
张开(“配置”,“r”)作为fileline:
list1=fileline.readlines ()
我的范围(0,len (list1)):
如果list1[我]!=' \ n ':
list1[我]=list1[我].rstrip (“\ n”)
如果list1[我].rstrip(': ')==参数:
打印“——产生绯闻;,我

试题:
功能=list1 (i + 1) .split (“=? [1] .replace (“\”、“) .rstrip (“\ n”)
属性=list1 [i + 2] .split (“=? [1]
标签=list1 [i + 3] .split(“=?[1]除了:

打印,请检查参数…、系统不是support"
退出(2)
v_dataframe=pd.read_csv(功能、编码=皍tf - 8”)
# v_dataframe=v_dataframe.reindex (np.random.permutation (v_dataframe.index))
返回v_dataframe,属性,标记
def preprocess_features(自我,california_housing_dataframe、属性标签):
df=california_housing dataframe
=训练df (0: df。形状[0]-1000]
test_=df (df。形状[0]-1000:]
预测=attribute.rstrip .replace (“\ n”) (“\”、“) .replace (' (',”) .replace (‘)’,”)
预测=predictors.split (" ")=
标签tag.rstrip .replace (“\ n”) (“\”、“) .replace (' (',”) .replace (‘)’,”)
X 训练=(预测)
y 训练=[标记]
X 测试=测试(预测)
y 测试=测试[标记]
num=len(预测)
test_y_disorder=preprocessing.scale (y_test)。重塑(1,1)
train_y_disorder=preprocessing.scale (y_train)。重塑(1,1)
ss_x=preprocessing.StandardScaler ()
train_x_disorder=ss_x.fit_transform (X_train)
test_x_disorder=ss_x.transform (X_test)
返回train_x_disorder train_y_disorder, test_x_disorder, test_y_disorder, num
def主要(自我):
california_housing_dataframe,属性,标签=self.read_csv ()
X, y, X_test, y_test, num=self.preprocess_features (california_housing_dataframe、属性标签)
返回X, y, X_test, y_test, num
t=ZcSummary ()
train_x train_y, X_test y_test, num=t.main ()

  

BATCH_START=0 #建立批处理数据时候的指数
TIME_STEPS=10 #反向传播通过时间的TIME_STEPS
BATCH_SIZE=30
INPUT_SIZE=num #罪数据输入大小
OUTPUT_SIZE=1 #因为数据输出大小
CELL_SIZE=10 # RNN的隐藏单元大小
LR=0.006 #学习速率
def get_batch_boston ():
全球train_x train_y, BATCH_START, TIME_STEPS
x_part1=train_x [BATCH_START: BATCH_START + TIME_STEPS BATCH_SIZE]
y_part1=train_y [BATCH_START: BATCH_START + TIME_STEPS
BATCH_SIZE]
#打印(u的时间段=',BATCH_START, BATCH_START + TIME_STEPS BATCH_SIZE)
seq=x_part1。重塑(BATCH_SIZE、TIME_STEPS INPUT_SIZE)
res=y_part1。重塑(BATCH_SIZE TIME_STEPS 1)
BATCH_START +=TIME_STEPS
#返回seq, res和xs:形状(批、步骤、输入)
# np。newaxis用来增加一个维度变为三个维度,第三个维度将用来存上一批样本的状态
返回(seq, res]
def get_batch ():
全球BATCH_START TIME_STEPS
# xs形状(50批次,20个步骤)
x=np。不等(BATCH_START BATCH_START + TIME_STEPS
BATCH_SIZE)。重塑((BATCH_SIZE TIME_STEPS))/(10 * np.pi)
打印(xs.shape=, xs.shape)
seq=np.sin (x)
res=np.cos (x)
BATCH_START +=TIME_STEPS
# matplotlib进口。pyplot plt
# plt。情节(x [0,:], res (0:),‘r’, x [0,:], seq [0,:], ' b——')
# plt.show ()
#打印(u的增加维度前:“,seq.shape)
#打印(seq [2]):
#打印(u的增加维度后:“,seq [:,:, np.newaxis] .shape)
#打印(seq [2]):
#返回seq, res和xs:形状(批、步骤、输入)
# np。newaxis用来增加一个维度变为三个维度,第三个维度将用来存上一批样本的状态
返回[seq [::, np。newaxis], res [:,:, np。newaxis], x]
类LSTMRNN(对象):
def <强> init (自我、n_steps input_size、output_size cell_size, batch_size):
"
: param n_steps:每批数据总包含多少时间刻度
: param input_size:输入数据的维度

lstm预测测试版本