Calum McNerney
Calum McNerney

Reputation: 11

How to label columns in an array ?How to split an array?(Python, numpy)

How would I label the columns in this array. I want to label the left column input and the right column output.

I would also like to split the columns into separate arrays if that is possible ?

                     ([[[ 100000.,     233.],
                        [ 100010.,     299.],
                        [ 100020.,     253.],

                        [ 199980.,     243.],
                        [ 200000.,     247.]],

                       [[ 100000.,     295.],
                        [ 100010.,     294.],
                        [ 100020.,     317.],

                        [ 199980.,     307.],
                        [ 199990.,     321.],
                        [ 200000.,     308.]],

                       [[ 100000.,     338.],
                        [ 100010.,     362.],
                        [ 100020.,     337.],

                        [ 199980.,     334.],
                        [ 199990.,     317.],
                        [ 200000.,     326.]]])

Upvotes: 1

Views: 1032

Answers (2)

falsetru
falsetru

Reputation: 369334

1. Convert the array to structured array using numpy.core.records.fromarrays:

>>> import numpy as np
>>>
>>> a = np.array(
...   [[[ 100000.,     233.],
...     [ 100010.,     299.],
...     [ 100020.,     253.],
...     [ 199980.,     243.],
...     [ 200000.,     247.]],
...    [[ 100000.,     295.],
...     [ 100010.,     294.],
...     [ 100020.,     317.],
...     [ 199990.,     321.],
...     [ 200000.,     308.]],
...    [[ 100000.,     338.],
...     [ 100010.,     362.],
...     [ 100020.,     337.],
...     [ 199990.,     317.],
...     [ 200000.,     326.]]]
... )
>>> b = np.core.records.fromarrays(
...     a.reshape(-1, 2).T, names='input,output'
... ).reshape(a.shape[:-1])
>>> b[0][0]['input']
100000.0
>>> b[0][0]['output']
233.0

2. Using slicing:

>>> a[..., 0]
array([[ 100000.,  100010.,  100020.,  199980.,  200000.],
       [ 100000.,  100010.,  100020.,  199990.,  200000.],
       [ 100000.,  100010.,  100020.,  199990.,  200000.]])
>>> a[..., 1]
array([[ 233.,  299.,  253.,  243.,  247.],
       [ 295.,  294.,  317.,  321.,  308.],
       [ 338.,  362.,  337.,  317.,  326.]])

Upvotes: 1

greschd
greschd

Reputation: 636

array[0,:,0] gives you the "input" column, array[0,:,1] the "output". (see numpy slicing). As for labelling the columns, that is usually where you should consider using a dict.

Upvotes: 1

Related Questions