我有一个时间序列的numpy数组X。像这样的东西:

[[0.05, -0.021, 0.003, 0.025, -0.001, -0.023, 0.095, 0.001, -0.018]
[0.015, 0.011, -0.032, -0.044, -0.002, 0.032, -0.051, -0.03, -0.020]
[0.04, 0.081, -0.02, 0.014, 0.063, -0.077, 0.059, 0.031, 0.025]]

我可以用
fig, axes = plt.subplots(3, 1)
for i in range(3):
    axes[i].plot(X[i])
plt.show()

然后出现类似以下的内容(这些图显示而不是显示了我在上面编写的演示值,但显示了具有类似结构的其他值)。因此,X中的每一行都是一个时间序列。
python - 将时间序列转换为图像矩阵-LMLPHP

但是我想拥有一个将每个时间序列描述为灰度图像的numpy数组(因为稍后我想将其用于cnn)。所以我认为我需要的是这样的:
[[[0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 1]
[0, 0, 1, 0, 0, 0]]
[[0, 0, 1, 0, 0, 0]
[0, 0, 0, 1, 0, 0]
[0, 1, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0]]...]

如何(如果可能:有效)将每个时间序列转换成矩阵,该矩阵将时间序列描述为图像。因此,旧数组中的每一行(例如:
[0.05, -0.021, 0.003, 0.025, -0.001, -0.023, 0.095, 0.001, -0.018])

应该转换为2D矩阵(例如,如下所示:
[[0, 0, 0, 0, 0, 1][0, 0, 0, 0, 1, 0][0, 0, 0, 0, 0, 1][0, 0, 1, 0, 0, 0]]
替代说明:
X中的每一行都描述一个时间序列。对于X中的每一行,我都需要一个2D矩阵,以图像形式描述时间序列(如上面的图所示)

“解决方案” :似乎没有很好的解决方案。我现在使用了这种解决方法:
fig = plt.figure()
fig.add_subplot(111)
fig.tight_layout(pad=0)
plt.axis('off')
plt.plot(X[0], linewidth=3)
fig.canvas.draw()
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
data现在包含2D矩阵,并且可以再次使用plt.imshow(data)进行绘制,但质量会有所下降。

最佳答案

看看这些kaggle challange。它认为您还希望像他们一样实现this paper的某些部分。

也许您还可以使用他们从另一个SO问题中采用的功能:

#modified from https://stackoverflow.com/questions/33650371/recurrence-plot-in-python
def recurrence_plot(s, eps=None, steps=None):
    if eps==None: eps=0.1
    if steps==None: steps=10
    d = sk.metrics.pairwise.pairwise_distances(s)
    d = np.floor(d / eps)
    d[d > steps] = steps
    #Z = squareform(d)
    return d

10-01 09:28
查看更多