Reshaping arrays into 2D matrix

by Maria Jane S. Poncardas, 9/7/2019


I came up with this post since I had been brawling into understanding how this reshaping works. During the past few weeks, I had been working on with the Recurrent Neural Network's Long-Short Term Memory (LSTM) to improve forecasting on generation of National Power Corporation's six (6) hydroelectric power plants in Mindanao. This method is also well-suited to classifying, processing, and making predictions based on time series data such as stock prices, Forex and on household electric consumption. Even a few enthusiasts utilized LSTM for predicting what would be the next word on their 10-word sentence, or next few lyrics of Ed Sheeran's songs. LSTM has quite an edge over the selective remembering of patterns and is relevant in the field of deep learning.

Working with respect on building architectures and fitting up models, it is quite relevant to know how basic reshaping works. I think it helps to visualize. For the simplest example, assuming you have make a 1D array of length 10 (codes through Python),

import numpy as np
import pandas as pd

1D = np.arrange(0,10)
mat = 1D.reshape(5,2)

With this you get 0,1,2,...,9, (exclusive of 10), reshaping should result to having no missing or left out values in a matrix. In this example, variable 'mat' reshapes the 1D array into 2D matrix with five rows and two columns.

However, you cannot reshape it into (3,3), since 9 has no place in this matrix.







Simply put, you can reshape it based on the number of values (length). Then divide it by the number of rows and columns. Say, (5,2) results to 10, unlike (3,3) has 9.


LSTM

Now for the LSTM which expects 3D input layer, it is composed of samples, time steps, and features. One sequence is one sample, one time step is one point of observation in the sample and one feature is one observation at a time step.

When fitting the model and making predictions through LSTM network, it requires us to specify the number of samples provided, number of time steps (time lag) and features. A wonderful and thoroughly explained blog is written by Jason Brownlee through this link, which explains how to setup into different cases such as multiple features and samples.

Comments

Popular posts from this blog

Using QGIS to convert .shp file to .geojson (GADM)

Data Cleaning from Global Terrorism Database (GTD)

LSTM pseudocode