将从函数返回的多个1D数组组合成2D数组python

时间:2022-12-29 18:49:44

I m a little new to python. I have a function named featureExtraction which returns a 1-D array for an image. I need to stack all such 1-d arrays row wise to form a 2-d array. I have the following equivalent code in MATLAB.

我对python有点新意。我有一个名为featureExtraction的函数,它返回一个图像的一维数组。我需要逐行堆叠所有这样的1-d数组以形成2-d数组。我在MATLAB中有以下等效代码。

    I1=imresize(I,[256 256]);
    Features(k,:) = featureextraction(I1);

featureextraction returns a 1-d row vector which is stacked row-wise to form a 2-d array. What is the equivalent code snippet in python?

featureextraction返回1-d行向量,该向量逐行堆叠以形成2-d数组。 python中的等效代码片段是什么?

Thank You in advance.

先谢谢你。

2 个解决方案

#1


6  

Not sure what you're looking for, but maybe vstack or column_stack?

不确定你在寻找什么,但也许是vstack或column_stack?

>>> np.vstack((a,a,a))
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> np.column_stack((a,a,a))
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2],
       [3, 3, 3],
       [4, 4, 4],
       [5, 5, 5],
       [6, 6, 6],
       [7, 7, 7],
       [8, 8, 8],
       [9, 9, 9]])

Or even just np.array:

甚至只是np.array:

>>> np.array([a,a,a])
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

#2


4  

You can use numpy.vstack():

你可以使用numpy.vstack():

a = np.array([1,2,3])

np.vstack((a,a,a))
#array([[1, 2, 3],
#       [1, 2, 3],
#       [1, 2, 3]])

#1


6  

Not sure what you're looking for, but maybe vstack or column_stack?

不确定你在寻找什么,但也许是vstack或column_stack?

>>> np.vstack((a,a,a))
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> np.column_stack((a,a,a))
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2],
       [3, 3, 3],
       [4, 4, 4],
       [5, 5, 5],
       [6, 6, 6],
       [7, 7, 7],
       [8, 8, 8],
       [9, 9, 9]])

Or even just np.array:

甚至只是np.array:

>>> np.array([a,a,a])
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

#2


4  

You can use numpy.vstack():

你可以使用numpy.vstack():

a = np.array([1,2,3])

np.vstack((a,a,a))
#array([[1, 2, 3],
#       [1, 2, 3],
#       [1, 2, 3]])