0

Any flag for this? Please, see the intended.

>>> numpy.column_stack([[1], [1,2]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/numpy/lib/shape_base.py", line 296, in column_stack
    return _nx.concatenate(arrays,1)
ValueError: array dimensions must agree except for d_0

Input

[[1],[1,2]]

Intended Output

[[NA,1], [1,2]]

In general

[[1],[2,2],[3,3,3],...,[n,n,n,n,n...,n]]

to

[[NA, NA, NA,..., NA,1], [NA, NA, ..., 2, 2], ...[n,n,n,n,n]]

where the columns may be a triangluar zero matrix initially. Yes you can understand the term NA as None. I got the triangular matrix almost below.

>>> a=[[1],[2,2],[3,3,3]]
>>> a
[[1], [2, 2], [3, 3, 3]]
>>> len(a)
3
>>> [aa+['']*(N-len(aa)) for
... 
KeyboardInterrupt
>>> N=len(a)
>>> [aa+['']*(N-len(aa)) for aa in a]
[[1, '', ''], [2, 2, ''], [3, 3, 3]]
>>> transpose([aa+['']*(N-len(aa)) for aa in a])
array([['1', '2', '3'],
       ['', '2', '3'],
       ['', '', '3']], 
      dtype='|S4')
hhh
  • 50,788
  • 62
  • 179
  • 282
  • 4
    How would NumPy know where to put the `NA`? It could be in `output[0][1]` as well. – Fred Foo Sep 30 '11 at 15:39
  • 1
    Does [this other answer](http://stackoverflow.com/questions/3438756/some-built-in-to-pad-a-list-in-python/3438818#3438818) help? – sdleihssirhc Sep 30 '11 at 15:41
  • @sdleihssirhc: `None`, what is the difference between `NA` and `None`? Zero values, nothing there. Yes the thread may become useful but I want to solve this problem this way, trying to create a triangular bottom zero matrix (zeros on the right corner). – hhh Oct 04 '11 at 19:49
  • @sdleihssirhc: actually that answer solved this question! Voted to close. – hhh Oct 04 '11 at 19:55

2 Answers2

1

a pure numpy solution:

>>> lili = [[1],[2,2],[3,3,3],[4,4,4,4]]
>>> y = np.nan*np.ones((4,4))
>>> y[np.tril_indices(4)] = np.concatenate(lili)
>>> y
array([[  1.,  nan,  nan,  nan],
       [  2.,   2.,  nan,  nan],
       [  3.,   3.,   3.,  nan],
       [  4.,   4.,   4.,   4.]])

>>> y[:,::-1]
array([[ nan,  nan,  nan,   1.],
       [ nan,  nan,   2.,   2.],
       [ nan,   3.,   3.,   3.],
       [  4.,   4.,   4.,   4.]])

I'm not sure which triangular array you want, there is also np.triu_indices

(maybe not always faster, but easy to read)

Josef
  • 21,998
  • 3
  • 54
  • 67
0

column_stack adds a column to an array. That column is supposed to be a smalled (1D) array.

When I try :

from numpy import *
x = array([0])
z = array([1, 2])

if you do this :

r = column_stack ((x,z))

You'll get this :

>>> array([0,1,2])

So, in order to add a column to your first array, maybe this :

n = array([9])

arr = ([column_stack((n, x))], z)

It shows up this :

>>> arr
([array([[9, 0]])], array([[1, 2]]))

It has the same look as your "intended output"

Hope this was helpful !

Louis
  • 2,854
  • 2
  • 19
  • 24
  • not `"array([0,1,2])"` but error: `>>> x array([0]) >>> z array([1, 2]) >>> r = numpy.column_stack((x,z)) Traceback (most recent call last): File "", line 1, in File "/usr/lib/pymodules/python2.7/numpy/lib/shape_base.py", line 296, in column_stack return _nx.concatenate(arrays,1) ValueError: array dimensions must agree except for d_0` – hhh Oct 04 '11 at 19:40
  • Newest numpy from apt-get -- same err `>>> x = numpy.array([0]) >>> z = numpy.array([1,2] ... ) >>> r = numpy.column_stack((x,z)) Traceback (most recent call last): File "", line 1, in File "/usr/lib/pymodules/python2.7/numpy/lib/shape_base.py", line 296, in column_stack return _nx.concatenate(arrays,1) ValueError: array dimensions must agree except for d_0` – hhh Oct 04 '11 at 20:00