第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何組合兩個 numpy 數(shù)組,以便對于第一個數(shù)組的每一行,我附加第二個數(shù)組中的所有行?

如何組合兩個 numpy 數(shù)組,以便對于第一個數(shù)組的每一行,我附加第二個數(shù)組中的所有行?

夢里花落0921 2023-12-29 16:10:13
我有以下 numpy 數(shù)組:theta_array =array([[ 1, 10],       [ 1, 11],       [ 1, 12],       [ 1, 13],       [ 1, 14],       [ 2, 10],       [ 2, 11],       [ 2, 12],       [ 2, 13],       [ 2, 14],       [ 3, 10],       [ 3, 11],       [ 3, 12],       [ 3, 13],       [ 3, 14],       [ 4, 10],       [ 4, 11],       [ 4, 12],       [ 4, 13],       [ 4, 14]])XY_array  = array([[ 44.0394952 , 505.81099922],       [ 61.03882938, 515.97253226],       [ 26.69851841, 525.18083012],       [ 46.78487831, 533.42309602],       [ 45.77188401, 545.42988355],       [ 81.12969132, 554.78767379],       [ 54.178463  , 565.8716283 ],       [ 41.58952084, 574.76827133],       [ 85.24956815, 585.1355127 ],       [ 80.73726733, 595.49446033],       [ 22.70625059, 605.59017175],       [ 40.66810604, 615.26308629],       [ 47.16694695, 624.39222332],       [ 48.72499541, 633.19846364],       [ 50.68589921, 643.72334885],       [ 38.42731134, 654.68595883],       [ 47.39519707, 666.28232866],       [ 58.07767155, 673.9572227 ],       [ 72.11393347, 683.68307373],       [ 53.70872932, 694.65509894],       [ 82.08237952, 704.5868817 ],       [ 46.64069738, 715.18427515],       [ 40.46032478, 723.91308011],       [ 75.69090892, 733.69595658],       [120.61447884, 745.31322786],       [ 60.17764744, 754.89747186],       [ 87.15961973, 766.24040447],       [ 82.93872713, 773.01518252],       [ 93.56688906, 785.60640153],       [ 70.0474047 , 793.81792947],       [104.3613818 , 805.40234676],       [108.39253837, 814.75002114],       [ 78.97643673, 824.95386427],       [ 85.69096895, 834.44797862],       [ 53.07112931, 844.39555058],       [111.49875807, 855.660508  ],       [ 70.88824958, 865.53417489],       [ 79.55499469, 875.31303945],       [ 60.86941464, 885.85235946],       [101.06017712, 896.69986636],       [ 74.55823544, 905.87417231],       [113.24705653, 915.19350121],       [ 94.21920882, 925.87933273],我試圖將兩者結(jié)合起來,因此對于 theta_array 的每個組合,我從 XY_array 獲取所有組合。在 numpy 中進行這種組合/聚合的方式是什么?
查看完整描述

3 回答

?
浮云間

TA貢獻1829條經(jīng)驗 獲得超4個贊

meshgrid您可以在使用with后對“列”重新排序[:,[0,2,1,3]],如果由于列數(shù)較多而需要使列表動態(tài)化,那么您可以看到我答案的結(jié)尾:


np.array(np.meshgrid(theta_array, XY_array)).T.reshape(-1,4)[:,[0,2,1,3]]

輸出:


array([[  1.        ,   1.        ,  44.0394952 , 505.81099922]],

       [[  1.        ,   1.        ,  61.03882938, 515.97253226]],

       [[  1.        ,   1.        ,  26.69851841, 525.18083012]],

       ...,

       [[ 14.        ,  14.        ,  73.86032436, 973.91032818]],

       [[ 14.        ,  14.        , 103.96923524, 984.24366761]],

       [[ 14.        ,  14.        ,  93.20663129, 995.44618851]])

如果您有很多列,您可以動態(tài)創(chuàng)建此列表:[0,2,1,3]使用列表理解。例如:


n = new_arr.shape[1]*2

lst = [x for x in range(n) if x % 2 == 0]

[lst.append(z) for z in [y for y in range(n) if y % 2 == 1]]

lst


[0, 2, 4, 6, 1, 3, 5, 7]

然后,您可以重寫為:


np.array(np.meshgrid(theta_array, XY_array)).T.reshape(-1,4)[:,lst]


查看完整回答
反對 回復(fù) 2023-12-29
?
哆啦的時光機

TA貢獻1779條經(jīng)驗 獲得超6個贊

您可以使用itertools.product:


out = np.array([*product(theta_array, XY_array)])

out = out.reshape(out.shape[0],-1)

輸出:


array([[  1.        ,  10.        ,  44.0394952 , 505.81099922],

       [  1.        ,  10.        ,  61.03882938, 515.97253226],

       [  1.        ,  10.        ,  26.69851841, 525.18083012],

       ...,

       [  4.        ,  14.        ,  73.86032436, 973.91032818],

       [  4.        ,  14.        , 103.96923524, 984.24366761],

       [  4.        ,  14.        ,  93.20663129, 995.44618851]])

也就是說,這看起來非常像XY 問題。你想用這個數(shù)組做什么?


查看完整回答
反對 回復(fù) 2023-12-29
?
鴻蒙傳說

TA貢獻1865條經(jīng)驗 獲得超7個贊

正如此處的側(cè)面/補充參考一樣,我們對兩種解決方案的執(zhí)行時間進行了比較。完成此特定操作itertools所需的時間比同等操作多 10 倍numpy。


%%time


for i in range(1000):    

    z = np.array(np.meshgrid(theta_array, XY_array)).T.reshape(-1,4)[:,[0,2,1,3]]


CPU times: user 299 ms, sys: 0 ns, total: 299 ms

Wall time: 328 ms

%%time


for i in range(1000):    

    z = np.array([*product(theta_array, XY_array)])    

    z = z.reshape(z.shape[0],-1)


CPU times: user 2.79 s, sys: 474 μs, total: 2.79 s

Wall time: 2.84 s


查看完整回答
反對 回復(fù) 2023-12-29
  • 3 回答
  • 0 關(guān)注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號