1

我有一个 (10,24) 的二维数组和一个 (10,) 形状的一维数组。我想使用一维数组对二维数组进行切片,这样我的结果数组将是 (10,24) 但值是从一维数组中的索引开始切片的。

import numpy as np
x1 = np.random.randint(1,20,10)

print(x1)
[ 8, 13, 13, 13, 14,  3, 14, 14, 11, 16]

y1 = np.random.randint(low = 1, high = 999, size = 240).reshape(10,24)

print(y1)

[[152 128 251 282 334 776 650 247 990 803 700 323 250 262 552 220 744  50
  684 695 600 293 138   5]
 [830 917 148 612 801 746 623 794 435 469 610 598  29 452 188 688 364  56
  246 991 554  33 716 712]
 [603  16 838  65 312 764 676 392 187 476 878 229 555 558  58 194 565 764
   48 579 447 202  81 300]
 [315 562 276 993 859 145  82 484 134  59 397 566 573 263 340 465 728 406
  767 408 294 115 394 941]
 [422 891 475 174 720 672 526  52 938 347 114 613 186 151 925 482 315 373
  856 155   5  60  65 746]
 [978 621 543 785 663  32 817 497 615 897 713 459 396 154 220 221 171 589
  571 587 248 668 413 553]
 [227 188   4 874 975 586  93 179 356 740 645 723 558 814  64 922 748 457
  249 688 799 239 708 516]
 [230 556 563  55 390 666 304 661 218 744 502 720 418 581 839 772 818 278
  190 997 553  71 897 909]
 [631 928 606 111 927 912  81  38 529 956 759   6 725 325 944 174  62 804
   82 358 305 291 454  34]
 [193 661 452  54 816 251 750 183  60 563 787 283 599 182 823 546 629 527
  667 614 615   3 790 124]]

我希望我的结果数组是:


[[990 803 700 323 250 262 552 220 744 50 684 695 600 293 138 5 0 0 0 0 0 0 0 0]
[452 188 688 364 56 246 991 554 33 716 712 0 0 0 0 0 0 0 0 0 0 0 0 0]
[558 58 194 565 764 48 579 447 202 81 300 0 0 0 0 0 0 0 0 0 0 0 0 0]
[263 340 465 728 406 767 408 294 115 394 941 0 0 0 0 0 0 0 0 0 0 0 0 0]
[925 482 315 373 856 155 5 60 65 746 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[785 663 32 817 497 615 897 713 459 396 154 220 221 171 589 571 587 248 668 413 553 0 0 0]
[64 922 748 457 249 688 799 239 708 516 0 0 0 0 0 0 0 0 0 0 0 0 0 0    ]
[839 772 818 278 190 997 553 71 897 909 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[6 725 325 944 174 62 804 82 358 305 291 454 34 0 0 0 0 0 0 0 0 0 0 0    ]
[546 629 527 667 614 615 3 790 124 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]

4

2 回答 2

1

我不认为你可以在用 0 填充时对数组进行切片。例如,您可以创建一个空的 zeros 数组并填充它

y1_result = np.zeros(y1.shape)
for row, x_i in enumerate(x):
    for j, element in enumerate(y1[row, x_i:]):
        y1_result[row, j] = element
于 2019-05-24T12:15:23.200 回答
1

这是一个矢量化的masking,它还利用了broadcasting-

def select_gt_indices(a, idx):
    r = np.arange(a.shape[1])
    select_mask = idx[:,None] <= r
    put_mask = (a.shape[1]-idx-1)[:,None] >= r
    # or put_mask = np.sort(select_mask,axis=1)[:,::-1]
    out = np.zeros_like(a)
    out[put_mask] = a[select_mask]
    return out

样品运行 -

In [92]: np.random.seed(0)
    ...: a = np.random.randint(0,999,(4,5))
    ...: idx = np.array([2,4,3,0])

In [93]: a
Out[93]: 
array([[684, 559, 629, 192, 835],
       [763, 707, 359,   9, 723],
       [277, 754, 804, 599,  70],
       [472, 600, 396, 314, 705]])

In [94]: idx
Out[94]: array([2, 4, 3, 0])

In [95]: select_gt_indices(a, idx)
Out[95]: 
array([[629, 192, 835,   0,   0],
       [723,   0,   0,   0,   0],
       [599,  70,   0,   0,   0],
       [472, 600, 396, 314, 705]])
于 2019-05-24T14:43:42.233 回答