Categories

# List reshaping with Numpy

While working with datasets, it is often times required to change the shape of it. For example, you have one long list with all the data in it. Each data value inside the list reflects a daily record. If you are interested in a weekly analysis, you need to transform it from a one dimensional list into a two dimensional (row/column) matrix with seven entries on each row.

``````import numpy as np

lst = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])``````
``lst.shape``
``(14,)``

For a row wise splitting, use the function np.split with the input variables data and the number of rows.

``np.split(lst, 2)``
``[array([1, 2, 3, 4, 5, 6, 7]), array([ 8,  9, 10, 11, 12, 13, 14])]``

Column wise splitting can be done with the function .hsplit. It follows the same logic as .split.

``````arr = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12]])``````

The example array “arr” can have in maximum four columns.

``np.hsplit(arr, 4)``
``````[array([,
,
]),
array([[ 2],
[ 6],
]),
array([[ 3],
[ 7],
]),
array([[ 4],
[ 8],
])]``````
``np.hsplit(arr, 4)``
``````array([,
,
])``````

It is also possible to split the columns in different amounts. Every number with an equal output is possible. You can test it by applying a modulo operation.

``````for i in range(arr.shape):
if ((arr.shape) % (i + 1) == 0): # 4 modulo (1 to 4)
print(i + 1) # If modulo operation equals 0, print the number``````
``````1
2
4``````

So on this example 1, 2 and 4 would be possible column splits.