Python创建垂直Numpy数组

问题描述 投票:0回答:1

我创建了一个代码,在该代码中,我从列表中创建了一个数组,该数组必须像矢量一样是垂直的,问题是,使用reshape方法我什么也没得到。

import numpy as np

data = [[ 28,    29,    30,    19,    20,    21],
        [ 31,    32,    33,    22,    23,      24],
        [  1,    34,    35,    36,    25,    26],
        [  2,    19,    20,    21,    10,    11],
        [  3,     4,     5,     6,     7,    8 ]]

index = []
for i in range(len(data)):
    index.append([data[i][0], data[i][1], data[i][2],
                    data[i][3], data[i][4], data[i][5]])   
    y = np.array([index[i]])
#    y.reshape(6,1)

这些情况有什么解决方案吗?谢谢。

我正在寻找类似的东西:

enter image description here

python python-3.x numpy-ndarray
1个回答
0
投票

您可以使用numpy flatten()中的https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.html

import numpy as np

data = [[ 28,    29,    30,    19,    20,    21],
        [ 31,    32,    33,    22,    23,      24],
        [  1,    34,    35,    36,    25,    26],
        [  2,    19,    20,    21,    10,    11],
        [  3,     4,     5,     6,     7,    8 ]]

data = np.array(data).flatten()

print(data.shape)
(30,)
© www.soinside.com 2019 - 2024. All rights reserved.