我有一系列NumPy二进制数组(表示给定时间内的多云(1s)和晴朗(0s)天空像素),我已将它们相加以找到每个像素中存在云的总观测次数。
现在我想找出云的百分比(云数量(1s)/总观测次数*100),但是在Python中使用NumPy数组时,我无法修改原始变量。
迄今为止,我的(简化后的)代码如下:
现在我想找出云的百分比(云数量(1s)/总观测次数*100),但是在Python中使用NumPy数组时,我无法修改原始变量。
迄今为止,我的(简化后的)代码如下:
import numpy as np
arr1 = np.array([1,0,1])
arr2 = np.array([0,0,1])
total1 = np.add(arr1, arr2)
>>> [1 0 2]
total2 = total1 #Purely to make multiple to elaborate my issue
variables = [total1, total2]
for x in variables:
x = x + 100
total1
>>> [1 0 2]
您可以基本上看到total1
变量没有更新。 它与以下内容一起工作:
for x in variables:
x += 100
total1
>>> [201, 200, 202]
我不想将值增加100,而是想计算百分比,例如:
for x in variables:
x = x / 1 * 100 #I have simplified the maths for ease of reading
#This is my desired output - note how total1 is updated, not created
total1
>>> [100, 0, 200]
但是这并不起作用。我以这种方式访问变量,因为我有更多超过2个的变量,并且正在尝试自动运行我的代码以适应n个变量(变量名称从total1 > totaln)。