[英]argsort() only positive and negative values separately and add a new pandas column
[英]Normalize positive and negative values separately
我有一個矩陣,我希望正值通過除以最大正值來歸一化,使得最大值為 1,負值被最小負值歸一化,使得最大負值為 -1。 例如,
[[ 1. 4. -100.]
[ 3. 10. -8.]]
變成
[[ 0.1 0.4 -1。 ] [ 0.3 1. -0.08]]。
我試過了
def sym_min_max_norm(mat):
res = np.divide(mat, np.max(mat), where = mat > 0)
res = np.divide(res, -np.min(res), where = res < 0)
return res
但這似乎不起作用。 也許我在 np.divide 中使用了錯誤的 where 條件?
假設源數組是a ,您可以運行結果:
result = np.where(a >= 0, a/np.max(a), -a/np.min(a))
只是一個單線。
下面的代碼給了我正確的結果,但我不確定為什么原來的解決方案不起作用。
def sym_min_max_norm(mt):
pos = mt > 0
neg = mt < 0
res = mt * (pos/np.max(mt) - neg/np.min(mt))
return res
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.