Home > OS >  Plotting Numpy Nd array (3d to 2d)
Plotting Numpy Nd array (3d to 2d)

Time:02-25

I've trained a model and extracted .h5 model architecture, then I used .h5 as prediction of time series datasets. This process was done by converting pandas dataframe to numpy array and adding dummy dimension. Then, on plotting section, there must be 2D plot instead of 3D array, so i reshaped it to 2D but on plotting section, there is nothing to show. How can I plot prediction results? Full code:

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

CodePudding user response:

According to @ShubhamSharma 's comment, I changed the plot to

plt.plot(predictions.squeeze())
  • Related