简体   繁体   中英

Create a three dimensional array based on three columns

I have an array that looks like the one below (boston housing dataset)..

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],

I can pick out a column like this:

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

Which gives me something like

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

Thats cool however what if I wanted to make a three dimensional array based on three columns such as 5, 2 and 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]
  ...

So basically to clarify, I want to construct an array of the columns 5, 2 and 0.

Is this what you want?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM