我正在处理的问题的上下文是尝试将使用 matplotlib.plotly 绘制的时间序列预测的结果转换回数据框,以便我可以使用 cufflinks 库来获得更具交互性的图表。我可以将鼠标悬停在数据点上以更详细地查看预测。
所以在训练和创建模拟之后,代码如下:
date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()
for i in range(test_size):
date_ori.append(date_ori[-1] + timedelta(days = 1))
date_ori = pd.Series(date_ori).dt.strftime(date_format = '%Y-%m-%d').tolist()
date_ori[-5:]
accepted_results = []
for r in results:
if (np.array(r[-test_size:]) < np.min(df['Close'])).sum() == 0 and \
(np.array(r[-test_size:]) > np.max(df['Close']) * 2).sum() == 0:
accepted_results.append(r)
len(accepted_results)
accuracies = [calculate_accuracy(df['Close'].values, r[:-test_size]) for r in accepted_results]
plt.figure(figsize = (15, 5))
for no, r in enumerate(accepted_results):
plt.plot(r, label = 'forecast %d'%(no + 1))
plt.plot(df['Close'], label = 'true trend', c = 'black')
plt.legend()
plt.title('average accuracy: %.4f'%(np.mean(accuracies)))
x_range_future = np.arange(len(results[0]))
plt.xticks(x_range_future[::30], date_ori[::30])
plt.show()
我已经开始剖析最后一个绘图部分以尝试将数据转换为数据框,以便使用袖扣进行绘图,因为袖扣的格式如下:
import cufflinks as cf
# data from FXCM Forex Capital Markets Ltd.
raw = pd.read_csv('http://hilpisch.com/fxcm_eur_usd_eod_data.csv',
index_col=0, parse_dates=True)
quotes = raw[['AskOpen', 'AskHigh', 'AskLow', 'AskClose']]
quotes = quotes.iloc[-60:]
quotes.tail()
AskOpen AskHigh AskLow AskClose
2017-12-25 22:00:00 1.18667 1.18791 1.18467 1.18587
2017-12-26 22:00:00 1.18587 1.19104 1.18552 1.18885
2017-12-27 22:00:00 1.18885 1.19592 1.18885 1.19426
2017-12-28 22:00:00 1.19426 1.20256 1.19369 1.20092
2017-12-31 22:00:00 1.20092 1.20144 1.19994 1.20147
qf = cf.QuantFig(
quotes,
title='EUR/USD Exchange Rate',
legend='top',
name='EUR/USD'
)
qf.iplot()
到目前为止,我试图将绘图分解成一个数据框,这些是预测结果:
df = accepted_results
rd = pd.DataFrame(df)
rd.T
0 1 2 3 4 5 6 7
0 768.699985 768.699985 768.699985 768.699985 768.699985 768.699985 768.699985 768.699985
1 775.319656 775.891012 772.283885 737.763376 773.811344 785.021571 770.438252 770.464180
2 772.387081 787.562968 764.858772 737.837558 775.712162 770.660990 768.103724 770.786379
3 786.316425 779.248516 765.839603 760.195678 783.410054 789.610540 765.924561 773.466415
4 796.039144 803.113903 790.219174 770.508252 795.110376 793.371152 774.331197 786.772606
... ... ... ... ... ... ... ... ...
277 1042.788063 977.462670 1057.189696 1262.203613 1057.900621 1042.329811 1053.378352 1171.416597
278 1026.857102 975.473725 1061.585063 1307.540754 1061.490772 1049.696547 1054.122795 1117.779434
279 1029.388746 977.097765 1069.265953 1192.250498 1064.540056 1049.169295 1045.126807 1242.474584
280 1030.373147 983.650686 1070.628785 1103.139889 1053.571269 1030.669091 1047.641127 1168.965372
281 1023.118504 984.660763 1071.661590 1068.445156 1080.461617 1035.736879 1035.599867 1231.714340
然后将x轴从
plt.xticks(x_range_future[::30], date_ori[::30])
至
df1 = pd.DataFrame((x_range_future[::30], date_ori[::30]))
df1.T
0 1
0 0 2016-11-02
1 30 2016-12-15
2 60 2017-01-31
3 90 2017-03-15
4 120 2017-04-27
5 150 2017-06-09
6 180 2017-07-24
7 210 2017-09-05
8 240 2017-10-17
9 270 2017-11-20
最后我有关闭的专栏,这是我到目前为止所能想到的
len(df['Close'].values)
252
当我使用
df['Close'].values
我得到了一个数组,我在把这一切放在一起时遇到了问题,袖扣 iplot 图表要好得多,如果我能以某种方式获得这样做的直觉,那就太棒了,如果我没有尝试,我提前道歉已经够难了,但我正在尽力而为,无论我搜索谷歌多少次,我似乎都找不到答案,所以我想我会在这里问。