時間序列(多個變數 一步)

2021-09-04 13:57:19 字數 4836 閱讀 5447

'''

part 1:多個x時間變數用於**y的時間

但是y不作為x的乙份子

'''# 創造資料

from numpy import array

from numpy import hstack

from keras.models import sequential

from keras.layers import lstm

from keras.layers import dense

# define input sequence

in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90])

in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95])

out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))])

# convert to [rows, columns] structure

in_seq1 = in_seq1.reshape((len(in_seq1), 1))

in_seq2 = in_seq2.reshape((len(in_seq2), 1))

out_seq = out_seq.reshape((len(out_seq), 1))

# horizontally stack columns

dataset = hstack((in_seq1, in_seq2, out_seq))

print(dataset)

'''[[ 10 15 25]

[ 20 25 45]

[ 30 35 65]

[ 40 45 85]

[ 50 55 105]

[ 60 65 125]

[ 70 75 145]

[ 80 85 165]

[ 90 95 185]]

現在定為時間步長為3

舉個例子,就是用

10 15

20 25

30 35

**65

'''# split a multivariate sequence into samples

def split_sequences(sequences, n_steps):

x, y = list(), list()

for i in range(len(sequences)):

end_ix = i + n_steps

if end_ix > len(sequences):

break

seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1, -1] # 和單變數最大的不同在這裡

return array(x), array(y)

n_steps = 3

x,y = split_sequences(dataset,n_steps)

print(x.shape,y.shape)

for i in range(len(x)):

print(x[i],y[i])

n_features = x.shape[2]

'''[[10 15]

[20 25]

[30 35]] 65

[[20 25]

[30 35]

[40 45]] 85

[[30 35]

[40 45]

[50 55]] 105

[[40 45]

[50 55]

[60 65]] 125

[[50 55]

[60 65]

[70 75]] 145

[[60 65]

[70 75]

[80 85]] 165

[[70 75]

[80 85]

[90 95]] 185

'''model = sequential()

model.add(lstm(50,activation='relu',input_shape=(n_steps,n_features)))

model.add(dense(1))

model.compile(optimizer='adam',loss='mse')

model.fit(x,y,epochs=200,verbose=0)

#**模型

x_input = array([[80, 85], [90, 95], [100, 105]])

x_input = x_input.reshape((1, n_steps, n_features)) # 轉換成樣本量+步長+特徵的格式

yhat = model.predict(x_input, verbose=0)

print(yhat) # 209.00851

'''part 2:多個x時間變數**多個x時間變數的下一步

即平行**

'''from numpy import array

from numpy import hstack

# split a multivariate sequence into samples

def split_sequences(sequences, n_steps):

x, y = list(), list()

for i in range(len(sequences)):

end_ix = i + n_steps

if end_ix > len(sequences)-1:

break

# 最關鍵的不一樣在這一步

seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix, :]

return array(x), array(y)

in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90])

in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95])

out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))])

in_seq1 = in_seq1.reshape((len(in_seq1), 1))

in_seq2 = in_seq2.reshape((len(in_seq2), 1))

out_seq = out_seq.reshape((len(out_seq), 1))

dataset = hstack((in_seq1, in_seq2, out_seq))

# choose a number of time steps

n_steps = 3

# convert into input/output

x, y = split_sequences(dataset, n_steps)

print(x.shape, y.shape)

# summarize the data

for i in range(len(x)):

print(x[i], y[i])

'''[[10 15 25]

[20 25 45]

[30 35 65]] [40 45 85]

[[20 25 45]

[30 35 65]

[40 45 85]] [ 50 55 105]

[[ 30 35 65]

[ 40 45 85]

[ 50 55 105]] [ 60 65 125]

[[ 40 45 85]

[ 50 55 105]

[ 60 65 125]] [ 70 75 145]

[[ 50 55 105]

[ 60 65 125]

[ 70 75 145]] [ 80 85 165]

[[ 60 65 125]

[ 70 75 145]

[ 80 85 165]] [ 90 95 185]

'''n_features = x.shape[2]

# define stack model

model = sequential()

model.add(lstm(100, activation='relu', return_sequences=true, input_shape=(n_steps, n_features)))

model.add(lstm(100, activation='relu'))

# 和多對一不同點在於,這裡多對多的dense的神經元=features數目

model.add(dense(n_features))

model.compile(optimizer='adam', loss='mse')

# fit model

model.fit(x, y, epochs=400, verbose=0)

# demonstrate prediction

x_input = array([[70,75,145], [80,85,165], [90,95,185]])

x_input = x_input.reshape((1, n_steps, n_features))

yhat = model.predict(x_input, verbose=0)

print(yhat) # [[100.58976 107.512665 207.87833 ]]

一步一步學Ruby 二 變數,常量

本文內容 一 變數 1.變數定義無需指定型別 2.只能包含字母 數字 下劃線,但只能以字母或下劃線開頭x,y2,mcount二 變數範圍1.區域性變數以小寫字母或下劃線開頭的識別符號在 ruby 中即為區域性變數 如果引用未被宣告的識別符號則會被解釋成無引數的方法呼叫 區域性變數只在 段類有效2.全...

一步一步學Ruby 二 變數,常量

本文內容 一 變數 1.變數定義無需指定型別 2.只能包含字母 數字 下劃線,但只能以字母或下劃線開頭x,y2,mcount二 變數範圍1.區域性變數以小寫字母或下劃線開頭的識別符號在 ruby 中即為區域性變數 如果引用未被宣告的識別符號則會被解釋成無引數的方法呼叫 區域性變數只在 段類有效2.全...

一步一步 Sql Azure

一步一步 sql azure 1.使用 windowsazure 平台賬號登陸 2.新建sqlazure server 3.新建資料庫 4.為sql azure server 新增防火牆規則,只有將本機新增到規則裡才能從本機連線到該sqlazure server 5.連線到sql azure ser...