您的数据示例看起来像 Excel,所以我尝试使用 Excel 表格并使用了 pandas 的 read_excel(CSV 有类似的命令):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df0 = pd.read_excel('testdata.xlsx',header=None)
df0.head()
给予
0 1 2 3 4 5 6 7 8 9 10 11 12
0 sim1 time 1 2 3 4 5 6 7 8 9 10 11
1 NaN feat1 1 0 -1 0 1 0 -1 0 1 0 -1
2 NaN feat2 2 0 -2 0 2 0 -2 0 2 0 -2
3 NaN feat3 3 0 -3 0 3 0 -3 0 3 0 -3
4 sim2 time 1 2 3 4 5 6 7 8 9 10 11
您可以将 1 个模型的数据提取为 pandas 数据框或 numpy 数组:
def get_data_numpy(df,j):
i = j * (nFeats+1)
t = np.array(df.iloc[i,2:])
y0 = np.array(df.iloc[i+1,2:])
y1 = np.array(df.iloc[i+2,2:])
y2 = np.array(df.iloc[i+3,2:])
return t,y0,y1,y2
def get_data_pandas(df,j):
i = j * (nFeats+1)
t = np.array(df.iloc[i,2:])
dfy = df.iloc[i+1:i+nFeats+1,2:]
return t,dfy
nModels = 1 # run for 1 model
nFeats = 3
for jModel in range(nModels):
tn,y0,y1,y2 = get_data_numpy(df0,jModel)
tp,dfy = get_data_pandas(df0,jModel)
#--- graphics ---
plt.style.use('fast')
fig, ax0 = plt.subplots(figsize=(20,4))
plt.plot(tp,dfy.T, lw=4, alpha=0.4); # plot pandas dfy with 1 command
plt.plot(tn,-y0,lw=6,ls='--') # plot each numpy time series
plt.plot(tn,-y1,lw=6,ls=':')
plt.plot(tn,-y2,lw=6,ls='-')
plt.show()
fig.savefig('plot_model_1.png', transparency=True)
给予
在数据显示 (df0.head()) 和图中仅显示第一个模型。为nModels设置一个大于1的数字就可以跑遍所有模型了。