簡體   English   中英

函數在數組的單個列中的每一行上迭代工作-numpy

[英]Function working iteratively over each row in an individual column of an array - numpy

我正在尋找使用以下公式更改數組中的所有值:

new_value = old_value * elec_space - elec_space

一個復雜的問題是,數組中所有高於48的值都將增加2,因為原始數組中不存在49和50(infile,如下所示)。 這意味着在執行上述計算之前,任何大於48的值都必須減去2。

原始值:

elec_space = 0.5

infile = 
[[41,   42,   43,   44]
 [41,   42,   44,   45]
 [41,   43,   45,   47]
 [44,   45,   46,   47]
 [44,   45,   47,   48]
 [44,   46,   48,   52]
 [47,   48,   51,   52]
 [47,   48,   52,   53]
 [47,   51,   53,   55]]

所需值:

infile =
[[ 20,  20.5,   21,  21.5]
 [ 20,  20.5,  21.5,  22]
 [ 20    21,    22,   23]
 [21.5,  22,   22.5,  23]
 [21.5   22,    23,  23.5]
 [21.5, 22.5,  23.5, 24.5]
 [ 23,  23.5,   24,  24.5]
 [ 23,  23.5,  24.5,  25]
 [ 23,   24,    25,   26]]

我試過了:

def remove_missing(infile):
    if infile > 48:
        return (infile - 2) * elec_space - elec_space
    else:
        return infile * elec_space - elec_space
A = remove_missing(infile[:,0])
B = remove_missing(infile[:,1])
M = remove_missing(infile[:,2])
N = remove_missing(infile[:,3])
infile = np.column_stack((A, B, M, N))

和:

def remove_missing(infile):
    return (infile - 2) * elec_space - elec_space if infile > 50 else infile * elec_space - elec_space
A = remove_missing(infile[:,0])
B = remove_missing(infile[:,1])
M = remove_missing(infile[:,2])
N = remove_missing(infile[:,3])

但是,它們每個都有以下回溯:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-181-dcc8e29a527f> in <module>()
      4     else:
      5         return infile * elec_space - elec_space
----> 6 A = remove_missing(infile[:,0])
      7 B = remove_missing(infile[:,1])
      8 M = remove_missing(infile[:,2])

<ipython-input-181-dcc8e29a527f> in remove_missing(infile)
      1 def remove_missing(infile):
----> 2     if infile > 48:
      3         return (infile - 2) * elec_space - elec_space
      4     else:
      5         return infile * elec_space - elec_space

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 




---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-180-c407ec4fa95d> in <module>()
      2     return (infile - 2) * elec_space - elec_space if infile > 50 else infile * elec_space - elec_space
      3 
----> 4 A = remove_missing(infile[:,0])
      5 B = remove_missing(infile[:,1])
      6 M = remove_missing(infile[:,2])

<ipython-input-180-c407ec4fa95d> in remove_missing(infile)
      1 def remove_missing(infile):
----> 2     return (infile - 2) * elec_space - elec_space if infile > 50 else infile * elec_space - elec_space
      3 
      4 A = remove_missing(infile[:,0])
      5 B = remove_missing(infile[:,1])

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我不認為a.any或a.all是正確的選項,因為我希望函數針對數組列中的每一行迭代運行,而不是基於超過48個值之一來更改所有值。

請問有人對如何最好地解決這個問題有什么建議嗎?

一種替代方法是從結果中減去大於48元素1 ,如下所示:

(infile - 2*(infile>48))* elec_space - elec_space

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM