有两种方法完成数据预处理,一种是写循环,另一种是生成索引矩阵,然后索引,下面来看下时间
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 13 10:55:19 2021
在神经网络中将序列转换为滑窗矩阵的算法时间对比
@author: user
"""
import time
import numpy as np
import torch
import matplotlib.pyplot as plt
def window_stack(a,stepsize,row,col):
# a:输入的numpy
# stepsize: 滑动步长
# row & col: 输出矩阵的维度[row,col]
# b = torch.squeeze(a, 1) # 输入a的维度,如[500,1],如果是[500],删除这段话
ind = np.arange(col)[None,:] + stepsize*np.arange(row)[:,None]
w = a[ind]
return w
inputSignal=np.random.rand(1000,)
inputSize=10
trainLen=len(inputSignal)-inputSize
startTime1=time.time()
train_data=np.empty((trainLen,inputSize))
for i in range(trainLen):
train_data[i]=inputSignal[i:i+inputSize]
endTime1=time.time()
runTime1=endTime1-startTime1
print(runTime1)
startTime2=time.time()
train_data2=window_stack(inputSignal,1,trainLen,inputSize)
endTime2=time.time()
runTime2=endTime2-startTime2
print(runTime2)
trainLenList=[1000,10000,1e5,1e6,1e7]
time1=[0.0010311603546142578,0.010968685150146484,0.07679605484008789,0.7185516357421875,7.863290548324585]
time2=[0.0009918212890625,0.0009980201721191406,0.006011009216308594,0.08977723121643066,0.7245986461639404]
plt.xscale('log')
plt.yscale('log')
plt.plot(trainLenList,time1)
plt.plot(trainLenList,time2)
plt.title('runtime comparison of two methods')
plt.xlabel('train length')
plt.ylabel('time/s')
索引的方法比循环时间少了10倍的样子