Reshaping a 1-D Array to a Multidimensional Array

Convert a 1D array to a 2D array in numpy

You want to reshape the array.

B = np.reshape(A, (-1, 2))

where -1 infers the size of the new dimension from the size of the input array.

Numpy reshape 1d to 2d array with 1 column

You could do -

ar.reshape(ar.shape[0],-1)

That second input to reshape : -1 takes care of the number of elements for the second axis. Thus, for a 2D input case, it does no change. For a 1D input case, it creates a 2D array with all elements being "pushed" to the first axis because of ar.shape[0], which was the total number of elements.

Sample runs

1D Case :

In [87]: ar
Out[87]: array([ 0.80203158, 0.25762844, 0.67039516, 0.31021513, 0.80701097])

In [88]: ar.reshape(ar.shape[0],-1)
Out[88]:
array([[ 0.80203158],
[ 0.25762844],
[ 0.67039516],
[ 0.31021513],
[ 0.80701097]])

2D Case :

In [82]: ar
Out[82]:
array([[ 0.37684126, 0.16973899, 0.82157815, 0.38958523],
[ 0.39728524, 0.03952238, 0.04153052, 0.82009233],
[ 0.38748174, 0.51377738, 0.40365096, 0.74823535]])

In [83]: ar.reshape(ar.shape[0],-1)
Out[83]:
array([[ 0.37684126, 0.16973899, 0.82157815, 0.38958523],
[ 0.39728524, 0.03952238, 0.04153052, 0.82009233],
[ 0.38748174, 0.51377738, 0.40365096, 0.74823535]])

How to reshape a 1d array to a 3d array with diffrerent size of 2d arrays?

a multidimensional array can't have dimensions with different size.

but if you want a tuple you will need to split the array in 2 parts, the first that match in size with the 3x3 array and the second that match the 3x2, at this point you'll have 2 one dimensional array, then reshape them

arr1 = arr1.reshape((3,3)) 
arr2 = arr2.reshape((3,2))

tuple = arr1, arr2

How do I reshape a NumPy multi dimensional array to another array with the same dimensions, but different shape?

The function you are looking for is np.moveaxis() which lets you move a source axis to its destination.

>>> arr = np.random.random((1,76,76,255))
>>>
>>> arr.shape
(1, 76, 76, 255)
>>> arr2 = np.moveaxis(arr, 3, 1)
>>> arr2.shape
(1, 255, 76, 76)
>>>

Please note that these axes are 0-indexed

Python reshaping 1D array into 2D array by 'rows' or 'columns'

Use np.transpose.

import numpy as np

print(np.arange(9).reshape(3,3).transpose())

Output:

[[0 3 6]
[1 4 7]
[2 5 8]]

How to reshape a flattened array to a multidimensional array in arbitray shape in Javascript?

Here's my solution:


const A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const C = [3, 4];

function unflattenArray(arr, dim) {
let elemIndex = 0;

if (!dim || !arr) return [];

function _nest(dimIndex) {
let result = [];

if (dimIndex === dim.length - 1) {
result = result.concat(arr.slice(elemIndex, elemIndex + dim[dimIndex]));
elemIndex += dim[dimIndex];
} else {
for (let i = 0; i < dim[dimIndex]; i++) {
result.push(_nest(dimIndex + 1));
}
}

return result;
}
return _nest(0);
}

console.log(unflattenArray(A, C));

Reshape 1D array to matrix 2D matrix

Might i suggest this change. it will work as i think you are passing a bad ar into the function:

import numpy as np
np.random.seed(25)
ar = np.random.randn(1000)
ar = ar * 100
ar = ar.astype('int8')
ar

def reshape(my_array):
ar = np.reshape(my_array,(200,5))
return ar

new_ar = reshape(ar)


Related Topics



Leave a reply



Submit