-1

I need convert many array to one matrix. One array must become one column i use np.column_stack but dos't work for me

[1 0 0 ... 0 0 1]
[1 0 0 ... 0 0 1]
[1 0 0 ... 0 0 1]
[1 0 0 ... 0 0 1]
[1 0 0 ... 0 0 1]
[1 0 0 ... 0 0 1]

to this

[1 1 1 1 1 1
 0 0 0 0 0 0
 0 0 0 0 0 0 
 . . . . . .
 . . . . . .
 . . . . . .
 0 0 0 0 0 0
 0 0 0 0 0 0 
 1 1 1 1 1 1 ]
hpaulj
  • 221,503
  • 14
  • 230
  • 353
karl52
  • 11
  • 5

4 Answers4

1

So you have a list of arrays:

In [3]: alist = [np.array([1,0,0,1]) for i in range(3)]                                          
In [4]: alist                                                                                    
Out[4]: [array([1, 0, 0, 1]), array([1, 0, 0, 1]), array([1, 0, 0, 1])]

Join them to become rows of a 2d array:

In [5]: np.vstack(alist)                                                                         
Out[5]: 
array([[1, 0, 0, 1],
       [1, 0, 0, 1],
       [1, 0, 0, 1]])

to become columns:

In [6]: np.column_stack(alist)                                                                   
Out[6]: 
array([[1, 1, 1],
       [0, 0, 0],
       [0, 0, 0],
       [1, 1, 1]])

You comment code is unclear, but:

for i in range(6): 
     np.column_stack((arrays[i]))

doesn't make sense, nor does it follow column_stack docs. column_stack makes a new array; it does not operate in-place. List append does operate inplace, and is a good choice when building a list iteratively, but it should not be taken as a model for building arrays itertively.

All the concatenate and stack functions takes a list of arrays as input. Take advantage of that. And remember, they return a new array on each call. (that applies for np.append as well, but I discourage using that).

Another option in the stack family:

In [7]: np.stack(alist, axis=1)                                                                  
Out[7]: 
array([[1, 1, 1],
       [0, 0, 0],
       [0, 0, 0],
       [1, 1, 1]])
hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

You can use numpy.asmatrix like the following below. The last steps convert the matrix to a one column matrix like requested.

EDIT As hpaulj pointed out, np.array (ndarray) is typically used more now, but if you are using a matrix type, the solution below works for this example.

import numpy as np

a1 = [ 1,  2,  3,  4, 5]
a2 = [ 6,  7,  8,  9, 10]
a3 = [11, 12, 13, 14, 15]

mat = np.asmatrix([a1, a2, a3])
mat
## matrix([[ 1,  2,  3,  4,  5],
##         [ 6,  7,  8,  9, 10],
##         [11, 12, 13, 14, 15]])

mat.shape
## (3, 5)

### If you want to reshape the final matrix
mat2 = mat.reshape(1, 15)
mat2.shape
## (1, 15)

### Convert to 1 column: You can also transpose it.
mat2.transpose().shape
## (15, 1)

steveb
  • 5,382
  • 2
  • 27
  • 36
  • These days the use of `np.matrix` is discouraged. In your example what does `np.matrix` do that `np.array` doesn't? – hpaulj Jan 13 '20 at 23:08
  • @hpaulj It is `np.asmatrix` not `np.matrix`. It is does what `np.array` does and also converts to a matrix type. – steveb Jan 13 '20 at 23:13
  • @hpaulj I will leave the answer as it is still valid if you use the `matrix` type (i.e. you only care about 2D arrays, or have legacy code that is already a matrix). However, here is an SO link comparing the differences and leaning towards `ndarray` : [What are the differences between numpy arrays and matrices? Which one should I use?](https://stackoverflow.com/questions/4151128/what-are-the-differences-between-numpy-arrays-and-matrices-which-one-should-i-u) – steveb Jan 13 '20 at 23:22
0

I would put all the arrays in one list and then reshape it

import numpy as np
l=[[1,1,1,0,1,1],[1,0,0,1,0,1]]
l=np.reshape(l,len(l)*len(l[0]),1)
0

Since what you want is basically vertical stacking of 1D arrays, it makes sense to use np.vstack and then transpose the result using .T:

my_array = np.array([1,0,0,0,0,0,1])
result = np.vstack([my_array] * 6).T

Here I assume you just copy the 1D array 6 times, but alternatively you can pass a list of 1D arrays as an argument to np.vstack.