用二维数组创建二维数组

时间:2022-02-18 21:41:07

I used numpy and scipy and there some function really care about the dimension of the array I have a function name CovexHull(point) which accept the point as 2 dimensional array.

我使用了numpy和scipy,还有一些函数真的关心数组的维数我有一个函数名cohull (point)它接受点为二维数组。

hull = ConvexHull(points)

船体= ConvexHull(分)

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

As you can see above the points is a numpy with ndim 2.

你可以看到上面的点是一个带ndim 2的numpy。

Now I have 2 different numpy array (tp and fp) like this (for example fp)

现在我有两个不同的numpy数组(tp和fp)

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

My question is how can I create a 2 dimensional numpy array effectively like points with tp and fp

我的问题是如何像使用tp和fp的点一样有效地创建一个二维的numpy数组

1 个解决方案

#1


22  

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

如果您希望将两个10元素的一维数组合并到二维数组np中。vstack((tp,fp))。T将这样做。np。vstack(tp, fp)将返回一个形状数组(2,10),T属性将返回带有形状(10,2)的转置数组(即两个一维数组组成列而不是行)。

>>> 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)

#1


22  

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

如果您希望将两个10元素的一维数组合并到二维数组np中。vstack((tp,fp))。T将这样做。np。vstack(tp, fp)将返回一个形状数组(2,10),T属性将返回带有形状(10,2)的转置数组(即两个一维数组组成列而不是行)。

>>> 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)