Home > Software engineering >  Numpy Nested Arrays by reference Inside array element but not a 3 dim arrary
Numpy Nested Arrays by reference Inside array element but not a 3 dim arrary

Time:10-26

** Problem **

How to refence Numpy nested arrays by reference inside array elements? When the elements on not, NOT, as 3 dim array, but the results of a list(map(lambda)) on elements to assign a rank, eg., ['g',e','f','a','b','c']. Perhaps need to convert to structure and allows [1][0][1]...[7][0][1] refencing approach.

** Goal Needed Report on rank ** Need to report on rank, eg., ['g',e','f','a','b','c'] But array([1,2,3,4]), array([.4, .5, .6]) AKA [1][0]...[7][0] works but NOT [1][0][1]...[7][0][1]

** Code effort try **

started with np.array()::

array_things = np.array(data_head[;, 1-8]).sum(axis=1, float)

then next after a np.vectorized() AND and then a list(map) ::

np.vectorized(funA) 
list(map(lambda x: funB(x), array_things)) 

This list(map(labda x: funB(x), array_things)) assigns the ['g',e','f','a','b','c'] associated to the [1][0],...[1][1]...[7][0],...[7][1]. Something like a simple rank but not as a 3 dim array [1][0][1]...[7][0][1]

** Effort **

array_data[1][0]
array_data[1][1]

but need to reference the, say [1][0][1]

** Logical Data ** If the np.array as the basic data output layout as such, eg:

array([1,2,3,4]), array([.4, .5, .6]), (['g','e','f'])

example data :

(array([1,2,3,4,5]), array([.55, .65, .76,.81,.79]),(['g',e','f','a','b','c'])

CodePudding user response:

The data tuple:

In [4]: np.array([1,2,3,4,5]), np.array([.55, .65, .76,.81,.79]),['g','e','f','a','b','c']
Out[4]: 
(array([1, 2, 3, 4, 5]),
 array([0.55, 0.65, 0.76, 0.81, 0.79]),
 ['g', 'e', 'f', 'a', 'b', 'c'])

object dtype array from that:

In [5]: arr = np.array(_, object)    
In [6]: arr
Out[6]: 
array([array([1, 2, 3, 4, 5]), array([0.55, 0.65, 0.76, 0.81, 0.79]),
       list(['g', 'e', 'f', 'a', 'b', 'c'])], dtype=object)    
In [7]: arr.shape
Out[7]: (3,)

If the arrays/lists are all the same size this isn't a reliable constructor.

Access to one of the elements, the last list:

In [8]: arr[2]
Out[8]: ['g', 'e', 'f', 'a', 'b', 'c']

Selecting a element of that list:

In [9]: arr[2][0]
Out[9]: 'g'

Selecting a sublist - normal list slice indexing:

In [10]: arr[2][:3]
Out[10]: ['g', 'e', 'f']

The other elements are arrays, so we can use 'advanced' indexing on those

In [11]: arr[1][[4,3,4,1]]
Out[11]: array([0.79, 0.81, 0.79, 0.65])

All of this would work just as well on the original tuple (or list) of arrays and list. An object dtype array is nearly the same as a list.

CodePudding user response:

Also found a solution using Numpy.atleast_2d(1, [1, 2], [[1, 2]]) which was able to work with the (array([1,2,3,4,5]), array([.55, .65, .76,.81,.79]),(['g',e','f','a','b','c'])

  • Related