[英]Function working iteratively over each row in an individual column of an array - numpy
I'm looking to change all the values in an array using the following formula: 我正在寻找使用以下公式更改数组中的所有值:
new_value = old_value * elec_space - elec_space
A complicating issue is that all values above 48 within the array will be increased by two, as 49 & 50 will never exist in the original array (infile, shown below). 一个复杂的问题是,数组中所有高于48的值都将增加2,因为原始数组中不存在49和50(infile,如下所示)。 This means that any value above 48 will have to have 2 subtracted from it before performing the above calculation. 这意味着在执行上述计算之前,任何大于48的值都必须减去2。
Original values: 原始值:
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]]
Desired values: 所需值:
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]]
I've tried: 我试过了:
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))
And: 和:
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])
But got the following traceback for each of them: 但是,它们每个都有以下回溯:
---------------------------------------------------------------------------
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()
I don't think a.any or a.all are the right options, as I want the function to run iteratively for each row in the column of the array, not to alter all values based on one of the values being over 48. 我不认为a.any或a.all是正确的选项,因为我希望函数针对数组列中的每一行迭代运行,而不是基于超过48个值之一来更改所有值。
Has anyone got any pointers on how best to tackle this, please? 请问有人对如何最好地解决这个问题有什么建议吗?
一种替代方法是从结果中减去大于48
元素1
,如下所示:
(infile - 2*(infile>48))* elec_space - elec_space
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.