79

My function (name CovexHull(point)) accepts the argument as a two-dimensional array:

hull = ConvexHull(points)

Session

In [1]: points.ndim
Out[1]: 2
In [2]: points.shape
Out[2]: (10, 2)
In [3]: points
Out[3]:
array([[ 0. ,  0. ],
       [ 1. ,  0.8],
       [ 0.9,  0.8],
       [ 0.9,  0.7],
       [ 0.9,  0.6],
       [ 0.8,  0.5],
       [ 0.8,  0.5],
       [ 0.7,  0.5],
       [ 0.1,  0. ],
       [ 0. ,  0. ]])

points is a NumPy array with ndim 2.

I have two different NumPy arrays (tp and fp) like below:

In [4]: fp.ndim
Out[4]: 1
In [5]: fp.shape
Out[5]: (10,)
In [6]: fp
Out[6]:
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.4,
        0.5, 0.6,  0.9,  1. ])

How can I create a two-dimensional NumPy array effectively (like points mentioned above) with tp and fp?

3 Answers 3

105

If you wish to combine two 10 element one-dimensional arrays into a two-dimensional array, np.vstack((tp, fp)).T will do it.

np.vstack((tp, fp)) will return an array of shape (2, 10), and the T attribute returns the transposed array with shape (10, 2) (i.e., with the two one-dimensional arrays forming columns rather than rows).

>>> tp = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> tp.ndim
1
>>> tp.shape
(10,)

>>> fp = np.array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
>>> fp.ndim
1
>>> fp.shape
(10,)

>>> combined = np.vstack((tp, fp)).T
>>> combined
array([[ 0, 10],
       [ 1, 11],
       [ 2, 12],
       [ 3, 13],
       [ 4, 14],
       [ 5, 15],
       [ 6, 16],
       [ 7, 17],
       [ 8, 18],
       [ 9, 19]])

>>> combined.ndim
2
>>> combined.shape
(10, 2)
Sign up to request clarification or add additional context in comments.

2 Comments

+1, but you can get the right shape directly with np.column_stack.
This should not be the accepted answer. column_stack is much more straightforward (see Aminu's answer)
54

You can use NumPy's column_stack:

np.column_stack((tp, fp))

2 Comments

This did the trick, accepted answer is less transparent
This is the right way. I'm surprised how this is not the first answer that shows up when googled.
9

Another way is to use np.transpose. It seems to be used occasionally, but it is not readable, so it is a good idea to use Aminu Kano's answer.

import numpy as np

tp = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
fp = np.array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
combined = np.transpose((tp, fp))
combined
# Out[3]:
# array([[ 0, 10],
#        [ 1, 11],
#        [ 2, 12],
#        [ 3, 13],
#        [ 4, 14],
#        [ 5, 15],
#        [ 6, 16],
#        [ 7, 17],
#        [ 8, 18],
#        [ 9, 19]])
combined.ndim
# Out[4]: 2
combined.shape
# Out[5]: (10, 2)

3 Comments

Even this is better than the accepted answer. vstack is not useful for OPs goal.
I think column_stack as in stackoverflow.com/a/55770994/10323798 is better and does what one would expect than vstack which gives the wrong shape.
Thank you for your comment. I changed the text of the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.