绘制 Numpy Nd 数组(3d 到 2d)

Plotting Numpy Nd array (3d to 2d)

我训练了一个模型并提取了 .h5 模型架构,然后我使用 .h5 作为时间序列数据集的预测。这个过程是通过将 pandas 数据帧转换为 numpy 数组并添加虚拟维度来完成的。然后,在绘图部分,必须有 2D 图而不是 3D 数组,所以我将它重塑为 2D,但在绘图部分,没有什么可显示的。如何绘制预测结果? 完整代码:

from keras.models import load_model
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
model = tf.keras.models.load_model('finaltemp.h5',compile = True)
df = pd.read_excel("new.xls")

#rescaling 
mean = df.mean()
std = df.std()
df_new = (df-mean)/std

#pandas to numpy
numpy_array = df_new.to_numpy()

#add dummy dim
x = np.expand_dims(numpy_array, axis=0)

#predict
predictions = model.predict(x)
print(predictions)

array([[[-0.05154558],
        [-0.01212088],
        [-0.07192875],
        ...,
        [ 0.24430084],
        [-0.04761859],
        [-0.1841197 ]]], dtype=float32)

#get shapes
predictions.shape
(1, 31390, 1)

#reshape to 2D
newarr = predictions.reshape(1,31390*1)
print(newarr)
[[-0.05154558 -0.01212088 -0.07192875 ...  0.24430084 -0.04761859
  -0.1841197 ]]

#plot
plt.plot(newarr)
plt.show()

final result

根据@ShubhamSharma的评论,我把剧情改成了

plt.plot(predictions.squeeze())