python列表可以是变量吗_由于动态变量,可以在Python中高效地创建列表

我正在使用Keras构建LSTM递归神经网络。我的代码运行良好,但是可以进行认真的重构。我正在预测时间序列值,并且根据我要预测的窗口大小,最终我写的代码似乎太过特定于该窗口大小,即很难适应许多不同大小的代码。

我将数据集分为训练和测试集

print "Dataset length: %d" % len(dataset)

train_size = int(len(dataset) * 0.67)

test_size = len(dataset) - train_size

train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]

print "Train length: %d, Test length: %d" % (len(train), len(test))

数据集长度:1826火车长度:1223,测试长度:603

然后对于两者train,test我需要创建一个X输入,一个Y输出(我试图预测的)

def create_dataset(dataset, look_back=1, predict_steps=1):

dataX, dataY = [], []

for i in range(dataset.shape[0] - look_back - predict_steps):

dataX.append(dataset[i:(i + look_back), 0])

dataY.append(dataset[i + look_back:i + look_back + predict_steps, 0])

return np.array(dataX), np.array(dataY)

look_back = 10

predict_steps = 5

input_dim = look_back + 1

trainX, trainY = create_dataset(train, look_back=look_back, predict_steps=predict_steps)

testX, testY = create_dataset(test, look_back=look_back, predict_steps=predict_steps)

print "trainX shape: %s, trainY shape: %s" % (trainX.shape, trainY.shape,)

trainX形状:(1208,10),trainY形状:(1208,5)

如果我要预测5个时间步长,那么存储在变量中的预测trainY将采用形式[[t+6, t+7, t+8, t+9, t+10], [t+7, t+8, t+9, t+10, t+11]],即

prediction 1 [t+6, t+7, t+8, t+9, t+10]

prediction 2 [t+7, t+8, t+9, t+10, t+11]

prediction 3 [t+8, t+9, t+10, t+11, t+12]

prediction 4 [t+9, t+10, t+11, t+12, t+13]

prediction 5 [t+10, t+11, t+12, t+13, t+14]

b89dc54ad956787f69690c6c131c2197.png

现在,如果我想按逻辑顺序返回这些值,即t+6, t+7, t+8,...,t+14我正在使用此代码

output = trainY

output_plot = np.array([])

output_plot = np.append(output_plot, output[0][0])

output_plot = np.append(output_plot, np.mean([output[0][1], output[1][0]]))

output_plot = np.append(output_plot, np.mean([output[0][2], output[1][1], output[2][0]]))

output_plot = np.append(output_plot, np.mean([output[0][3], output[1][2], output[2][1], output[3][0]]))

for i in range (len(output) - predict_steps + 1):

tmp = np.mean([output[i][4], output[i+1][3], output[i+2][2], output[i+3][1], output[i+4][0]])

output_plot = np.append(output_plot, tmp)

当我想将预测窗口扩展到10个时间步长时,就会出现我的问题。然后,我手动扩展前面的代码,如下所示

output = trainY

output_plot = np.array([])

output_plot = np.append(output_plot, output[0][0])

output_plot = np.append(output_plot, np.mean([output[0][1], output[1][0]]))

output_plot = np.append(output_plot, np.mean([output[0][2], output[1][1], output[2][0]]))

output_plot = np.append(output_plot, np.mean([output[0][3], output[1][2], output[2][1], output[3][0]]))

output_plot = np.append(output_plot, np.mean([output[0][4], output[1][3], output[2][2], output[3][1], output[4][0]]))

output_plot = np.append(output_plot, np.mean([output[0][5], output[1][4], output[2][3], output[3][2], output[4][1], output[5][0]]))

output_plot = np.append(output_plot, np.mean([output[0][6], output[1][5], output[2][4], output[3][3], output[4][2], output[5][1], output[6][0]]))

output_plot = np.append(output_plot, np.mean([output[0][7], output[1][6], output[2][5], output[3][4], output[4][3], output[5][2], output[6][1], output[7][0]]))

output_plot = np.append(output_plot, np.mean([output[0][8], output[1][7], output[2][6], output[3][5], output[4][4], output[5][3], output[6][2], output[7][1], output[8][0]]))

for i in range (len(output) - predict_steps + 1):

tmp = np.mean([output[i][9], output[i+1][8], output[i+2][7], output[i+3][6], output[i+4][5], output[i+5][4], output[i+6][3], output[i+7][2], output[i+8][1], output[i+9][0]])

output_plot = np.append(output_plot, tmp)

尽管这样做有效,但效率极低。我如何最好地重构这些步骤,以使代码更适合更广泛的预测窗口?另外,我的问题标题可能有所改进,因此请删除!

解决方案

(注意:我对您的神经网络问题一无所知,我只是在解决您的编码/数组遍历问题)

可以通过循环完成矩阵对角线的处理,您只需将循环的参数调整为适当的尺寸即可。以下代码是纯Python的简化模型,其中包含您根据我对数据形状的了解而尝试完成的工作。

from pprint import pprint

def create_mock_data(n):

return [[100 + i] for i in range(n)]

def create_dataset(dataset, look_back = 1, predict_steps = 1):

X, Y = [], []

for i in range(len(dataset) - look_back - predict_steps):

X.append([row[0] for row in dataset[i : i+look_back]])

Y.append([row[0] for row in dataset[i+look_back : i+look_back+predict_steps]])

return X, Y

def antidiagonals(a):

m, n = len(a), len(a[0])

for k in range(0, n):

yield [a[k-i][i] for i in range(k + 1)]

for k in range(n, m):

yield [a[k-i][i] for i in range(n)]

def pp(label, x):

print('---', label, '---')

pprint(x, width = 108)

print()

def test(n, look_back, predict_steps):

print('=' * 72)

print('n =', n)

print('look_back =', look_back)

print('predict_steps =', predict_steps)

print()

dataset = create_mock_data(n)

pp('dataset', dataset)

X, Y = create_dataset(dataset, look_back, predict_steps)

pp('X', X)

pp('Y', Y)

diagonals = list(antidiagonals(Y))

pp('diagonals of Y', diagonals)

print()

test(50, look_back = 10, predict_steps = 5)

test(50, look_back = 10, predict_steps = 10)

# test(50, look_back = 15, predict_steps = 10)

注意:

I used Python lists instead of numpy arrays, so please correct me if I misinterpreted your array indexing, especially in the create_dataset function.

For simplicity, I skipped the part where you split the original dataset into training and test datasets.

The main code of interest is in the antidiagonals function. For each row of an MxN matrix, it yields the rising diagonal starting from that row's first element. The first loop yields the first N diagonals, which all have differing lengths. The second loop yields the next M-N diagonals, all of which have N elements. When you run the code above, you can inspect the output for the mock Y array and its diagonals.

Assuming that these are the correct series of data you wish to add to your output_plot, you just have to modify the function to operate on numpy arrays and take the mean of each diagonal. Then you should be able to adjust the prediction window without duplicating much code.

Let me know if this is or isn't in line with what you're going for.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值