Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 3d numpy array into a 2d numpy array (where contents are tuples)

Tags:

python

numpy

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.

like image 756
Newskooler Avatar asked Sep 16 '19 10:09

Newskooler


People also ask

How do you convert a 3D array to a 2D array?

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.

Can a Numpy array contain tuples?

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.

How to reshape a NumPy array to 2D in Python?

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.

How to convert a 3D array to 2D array in Python?

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).

How to convert NumPy array to tuples in Python?

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']])

How to create a 3-dimensional NumPy array in Python?

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.


Video Answer


2 Answers

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')
like image 77
Paul Panzer Avatar answered Oct 10 '22 21:10

Paul Panzer


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,:]
like image 39
AnsFourtyTwo Avatar answered Oct 10 '22 21:10

AnsFourtyTwo