np.vectorize np.piecewise 用法小結

np.vectorize

ladders = {1:11, 2:22, 3:33}
ladder_move = np.vectorize(lambda x: ladders[x] if x in ladders else x)
print(ladder_move)
ladder_move(1)
ladder_move(2)
ladder_move(3)
ladder_move(4)
ladder_move(np.arange(50))

result:

<numpy.vectorize object at 0x00000257A1FDCA88>
[ 0 11 22 33  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49]

np.piecewise

#%%
step2 = np.arange(90,110)
step22 = np.piecewise(step2, [step2 > 100, step2 <= 100],
    [lambda x: 200 - x, lambda x: x])
print(step2)
print(step22)

result:

[ 90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107
 108 109]
[ 90  91  92  93  94  95  96  97  98  99 100  99  98  97  96  95  94  93
  92  91]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章