I have the following 3d numpy array np.random.rand(6602, 3176, 2)
.
I would like to convert it to a 2d array (numpy
or pandas.DataFrame
), where each value inside is a tuple, such that the shape is (6602, 3176)
.
This questioned helped me see how to decrease the dimensions, but I still struggle with the tuple bit.
Convert a 3D Array to a 2D Array With the numpy. reshape() Function in Python. The numpy. reshape() function changes the shape of an array without changing its data.
A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.
To reshape the NumPy array, we have a built-in function in python called numpy.reshape. We can reshape a one-dimensional to a two-dimensional array, 2d to 3d, 3d to 2d, etc. Here we are only focusing on numpy reshape 3d to 2d array. Changing the shape of the array without changing the data is known as reshaping.
In this tutorial, we will discuss converting a 3D array to a 2D array in Python. The changes the shape of an array without changing its data. numpy.reshape () returns an array with the specified dimensions. For example, if we have a 3D array with dimensions (4, 2, 2) and we want to convert it to a 2D array with dimensions (4, 4).
Given below are various methods to convert numpy array into tuples. Method #1: Using tuple and map import numpy as np ini_array = np.array ([ ['manjeet', 'akshat'], ['nikhil', 'akash']])
A three dimensional means we can use nested levels of array for each dimension. To create a 3-dimensional numpy array we can use simple numpy.array () function to display the 3-d array.
Here is a one-liner which takes a few seconds on the full (6602, 3176, 2) problem
a = np.random.rand(6602, 3176, 2)
b = a.view([(f'f{i}',a.dtype) for i in range(a.shape[-1])])[...,0].astype('O')
The trick here is to viewcast to a compund dtype which spans exactly one row. When such a compound dtype is then cast on to object each compound element is converted to a tuple.
UPDATE (hat tip @hpaulj) there is a library function that does precisely the view casting we do manually: numpy.lib.recfunctions.unstructured_to_structured
Using this we can write a more readable version of the above:
import numpy.lib.recfunctions as nlr
b = nlr.unstructured_to_structured(a).astype('O')
If you really want to do, what you want to do, you have to set dtype
of you array to object
. E.g., if you have the mentioned array:
a = np.random.rand(6602, 3176, 2)
You could create a second empty array with shape (6602, 3176) and set dtype
to object
:
b = np.empty(a[:,:,0].shape, dtype=object)
and fill your array with tuples.
But in the end there is no big advantage! I'd just use slicing to get the tuples from your initial array a
. You can just access the tuples of indexes n
(1st dimension) and m
(2nd dimension) and forget about the third dimension and slice your 3d array:
a[n,m,:]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With