Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind columns (from vectors) for numpy

The codes are like this:

a = numpy.zeros(3)
b = numpy.ones(3)
bind_by_column((a,b))
=> [[0,1],[0,1],[0,1]]

I checked this but don't find the answer

Does anyone have ideas about this?

like image 369
Hanfei Sun Avatar asked Jan 14 '13 07:01

Hanfei Sun


People also ask

How do I merge columns in NumPy?

In order to join NumPy arrays column-wise, we can also use the concatenate() function. In this case, however, we would use the axis=1 parameter, in order to specify that we want to join the arrays along the column axis.

How do I assign a column in NumPy?

Use a[:,1] = x[:,0] . You need x[:,0] to select the column of x as a single numpy array.

What does .all do in NumPy?

all() in Python. The numpy. all() function tests whether all array elements along the mentioned axis evaluate to True.


2 Answers

np.column_stack

see Numpy: Concatenating multidimensional and unidimensional arrays

>>> import numpy
>>> a = numpy.zeros(3)
>>> b = numpy.ones(3)
>>> numpy.column_stack((a,b))
array([[ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.]])
like image 144
Josef Avatar answered Sep 22 '22 01:09

Josef


You can use numpy.vstack():

>>> import numpy
>>> a = numpy.zeros(3)
>>> b = numpy.ones(3)
>>> numpy.vstack((a,b)).T
array([[ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.]])
like image 34
Andrey Sobolev Avatar answered Sep 20 '22 01:09

Andrey Sobolev