Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create 2 dimensional array with 2 one dimensional array

My function (name CovexHull(point)) accepts argument as 2 dimensional array.

hull = ConvexHull(points)

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 2 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. ]) 

I want to know how can I create a 2 dimensional numpy array effectively (like points mentioned above) with tp and fp.

like image 876
Am1rr3zA Avatar asked Jul 17 '13 21:07

Am1rr3zA


People also ask

How do you convert a one-dimensional array to a two-dimensional array?

Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping. To modify the layout of a NumPy ndarray, we will be using the reshape() method.

Can you create a 2 dimensional array with different types?

You can even create a two-dimensional array where each subarray has a different length or different type, also known as a heterogeneous array in Java.

How do you convert a one-dimensional array to a two-dimensional array in C?

Call the function​ ​ input_array​ to store elements in 1D array. Call the function ​ print_array​ to print the elements of 1D array. Call the function ​ array_to_matrix​ to convert 1D array to 2D array. Call function ​ print_matrix​ to print the elements of the 2D array.


2 Answers

If you wish to combine two 10 element 1-d arrays into a 2-d 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 1-d 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) 
like image 167
ijmarshall Avatar answered Oct 20 '22 09:10

ijmarshall


You can use numpy's column_stack

np.column_stack((tp, fp)) 
like image 37
Aminu Kano Avatar answered Oct 20 '22 10:10

Aminu Kano