[英]Create a three dimensional array based on three columns
我有一個看起來像下面的數組(波士頓住房數據集)..
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列的數組。
這是你想要的嗎?
>>> 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]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.