Expanding array dimension

This is the place for queries that don't fit in any of the other categories.

Expanding array dimension

Postby Andreas J » Tue Nov 19, 2013 11:11 am

I am looking for a built-in (preferrably numpy or compatible to numpy arrays) function that can expand array dimensions by transforming array elements to arrays. I'll write in code how I would manually transform an array x from dim(x) = (3,2) to dim(x) = (3,2,4) to better explain:

Code: Select all
In [1]: import numpy as np

In [2]: x = np.array([[1.,2.],[3.,4.],[5.,6.]])

In [3]: x
Out[3]:
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])

In [4]: x = np.transpose(np.array([np.transpose(x)]*4))

In [5]: x
Out[5]:
array([[[ 1.,  1.,  1.,  1.],
        [ 2.,  2.,  2.,  2.]],

       [[ 3.,  3.,  3.,  3.],
        [ 4.,  4.,  4.,  4.]],

       [[ 5.,  5.,  5.,  5.],
        [ 6.,  6.,  6.,  6.]]])


Thankful for help,

Andreas
Last edited by Mekire on Tue Nov 19, 2013 11:19 am, edited 1 time in total.
Reason: First post lock.
Andreas J
 
Posts: 1
Joined: Tue Nov 19, 2013 10:58 am

Re: Expanding array dimension

Postby tnknepp » Mon Dec 02, 2013 4:45 pm

Not sure about anything pre-built, but this seems to work:

Code: Select all
import numpy as np

x = np.array([[1.,2.],[3.,4.],[5.,6.]])

dims = x.shape

b = np.ones( (dims[0],dims[1],4) )

for c in xrange(dims[1]):
    b[:,c] = b[:,c]*x[:,c:c+1]
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests