基于三列创建三维数组

时间:2014-01-10 16:16:11

标签: python numpy

我有一个类似下面的数组(波士顿住房数据集)..

array = array([[  6.32000000e-03,   1.80000000e+01,   2.31000000e+00, ...,
          1.53000000e+01,   3.96900000e+02,   4.98000000e+00],
       [  2.73100000e-02,   0.00000000e+00,   7.07000000e+00, ...,
          1.78000000e+01,   3.96900000e+02,   9.14000000e+00],
       [  2.72900000e-02,   0.00000000e+00,   7.07000000e+00, ...,
          1.78000000e+01,   3.92830000e+02,   4.03000000e+00],

我可以选择这样一个列:

column_zero = [:, np.newaxis][:, :, 5]

这给了我类似的东西

[[ 6.575]
 [ 6.421]
 [ 7.185]
 [ 6.998]
 [ 7.147]
 [ 6.43 ]
  ...

但是,如果我想根据三列(如5,2和0)制作三维数组,那该怎么办呢?

[[ 6.575, item_0_column_2, item_0_column_0]
 [ 6.421, item_1_column_2, item_1_column_0]
 [ 7.185, item_2_column_2, item_2_column_0]
 [ 6.998, item_3_column_2, item_3_column_0]
 [ 7.147, item_4_column_2, item_4_column_0]
 [ 6.43 , item_5_column_2, item_5_column_0]
  ...

所以基本上要澄清,我想构建第5列,第2列和第0列的数组。

1 个答案:

答案 0 :(得分:2)

这是你想要的吗?

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

>>> a[:, [5, 2, 0]]
array([[0, 7, 7],
       [5, 7, 7],
       [5, 5, 3]])