python - 函数在数组的单个列中的每一行上迭代工作 - numpy

标签 python arrays function numpy boolean-operations

我希望使用以下公式更改数组中的所有值:

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

关于python - 函数在数组的单个列中的每一行上迭代工作 - numpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39980081/

相关文章:

python - 如何将元组从 Python 传递给 Matlab 函数

python - 如何导入自己的模块进行模拟? (导入错误 : no module named my_module!)

python - GraphQL - 'either/or' 用于多个必填输入字段

python - 如何使用 ElementTree 获取元素的完整 XML 或 HTML 内容?

ruby - 如何对日期数组进行排序,使其不连续?

php函数参数

python - 以前的产量操作 - python

javascript - 将范围为 {n} (length prop) 的数组中的值分组到下一项

php - 如何使用数组中的值通过2行比较来选择mysql中的表?

python - 可以将输入字符串转换为 Python 中的可调用函数对象吗?