Convert Array of Single-Element Arrays to a One-Dimensional Array

Convert array of single-element arrays to a one-dimensional array

For your limited use case, this'll do it:

$oneDimensionalArray = array_map('current', $twoDimensionalArray);

This can be more generalized for when the subarrays have many entries to this:

$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);

Convert multidimensional array into single array

Assuming this array may or may not be redundantly nested and you're unsure of how deep it goes, this should flatten it for you:

function array_flatten($array) { 
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[$key] = $value;
}
}
return $result;
}

How to convert list of numpy arrays into single numpy array?

In general you can concatenate a whole sequence of arrays along any axis:

numpy.concatenate( LIST, axis=0 )

but you do have to worry about the shape and dimensionality of each array in the list (for a 2-dimensional 3x5 output, you need to ensure that they are all 2-dimensional n-by-5 arrays already). If you want to concatenate 1-dimensional arrays as the rows of a 2-dimensional output, you need to expand their dimensionality.

As Jorge's answer points out, there is also the function stack, introduced in numpy 1.10:

numpy.stack( LIST, axis=0 )

This takes the complementary approach: it creates a new view of each input array and adds an extra dimension (in this case, on the left, so each n-element 1D array becomes a 1-by-n 2D array) before concatenating. It will only work if all the input arrays have the same shape—even along the axis of concatenation.

vstack (or equivalently row_stack) is often an easier-to-use solution because it will take a sequence of 1- and/or 2-dimensional arrays and expand the dimensionality automatically where necessary and only where necessary, before concatenating the whole list together. Where a new dimension is required, it is added on the left. Again, you can concatenate a whole list at once without needing to iterate:

numpy.vstack( LIST )

This flexible behavior is also exhibited by the syntactic shortcut numpy.r_[ array1, ...., arrayN ] (note the square brackets). This is good for concatenating a few explicitly-named arrays but is no good for your situation because this syntax will not accept a sequence of arrays, like your LIST.

There is also an analogous function column_stack and shortcut c_[...], for horizontal (column-wise) stacking, as well as an almost-analogous function hstack—although for some reason the latter is less flexible (it is stricter about input arrays' dimensionality, and tries to concatenate 1-D arrays end-to-end instead of treating them as columns).

Finally, in the specific case of vertical stacking of 1-D arrays, the following also works:

numpy.array( LIST )

...because arrays can be constructed out of a sequence of other arrays, adding a new dimension to the beginning.

How can I create an array of 1-element arrays from an array?

The operation you describe is very rarely useful. More likely, it would be a better idea to add an extra dimension of length 1 to the end of your array:

a = a[..., np.newaxis]
# or
a = a.reshape(a.shape + (1,))

Then a[0, 1] will be a 1D array, but all the nice NumPy features like broadcasting and ufuncs will work right. Note that this creates a view of the original array; if you need an independent copy, you can call the copy() method.

If you actually want a 2D array whose elements are 1D arrays, NumPy doesn't make that easy for you. (It's almost never a good way to organize your data, so there isn't much reason for the NumPy developers to provide an easy way to do it.) Most of the things you might expect to create such an array will instead create a 3D array. The most straightforward way to do it I know of is to create an empty array of object dtype and fill the cells one by one, using ordinary Python loops:

b = numpy.empty(a.shape, dtype=object)
for i in range(a.shape[0]):
for j in range(a.shape[1]):
b[i, j] = numpy.array([a[i, j]])

Selecting Elements in a one dimensional array in Python Numpy

Try this:

array_example[[2,6,8]]

It is called 'fancy indexing'.

How do I convert an array of arrays into a multi-dimensional array in Python?

You can concatenate the arrays on a new axis. For example:

In [1]: a=np.array([1,2,3],dtype=object)
...: b=np.array([4,5,6],dtype=object)

To make an array of arrays we can't just combine them with array, as the deleted answer did:

In [2]: l=np.array([a,b])
In [3]: l
Out[3]:
array([[1, 2, 3],
[4, 5, 6]], dtype=object)
In [4]: l.shape
Out[4]: (2, 3)

Instead we have to create an empty array of the right shape, and fill it:

In [5]: arr = np.empty((2,), object)
In [6]: arr[:]=[a,b]
In [7]: arr
Out[7]: array([array([1, 2, 3], dtype=object),
array([4, 5, 6], dtype=object)],
dtype=object)

np.stack acts like np.array, but uses concatenate:

In [8]: np.stack(arr)
Out[8]:
array([[1, 2, 3],
[4, 5, 6]], dtype=object)
In [9]: _.astype(float)
Out[9]:
array([[ 1., 2., 3.],
[ 4., 5., 6.]])

We could also use concatenate, hstack or vstack to combine the arrays on different axes. They all treat the array of arrays as a list of arrays.

If arr is 2d (or higher) we have to ravel it first.

PHP Convert multidimensional array in one dimensional array of arrays

array_merge() will merge multiple arrays, but you have an unspecified number in the main array. So use the array as the parameters for call_user_func_array() with array_merge() as the callback:

$result = call_user_func_array('array_merge', $array);


Related Topics



Leave a reply



Submit