0
Python Numpy function to resize a 1D array to 2D array with fixed no.of columns and no restrictions to no.of rows
I have a 1D numpy array with 10 elements. I want to resize it into a 2D matrix with 3 columns and as much no.of rows as required to include all elements. And I want to fill the extra spaces with 0s. My array, `ary = [0, 1, 2, 3, 4 , 5, 6, 7, 8, 9]` Required output, ``` ary = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 0, 0]] ``` I know that if I use `ary.resize(3,4)` I will get the answer, but the size of input array varies, so I can't always specify the no.of rows. I want something like `ary.resize(3,n)` ??
1 Réponse
+ 3
Try this:
import math
import numpy as np
ary = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
COLUMN = 3
row = math.ceil(ary.size / COLUMN)
ary.resize(row, COLUMN)
print(ary)