【发布时间】:2016-10-27 13:02:38
【问题描述】:
我有一系列二进制 NumPy 数组(在给定时间代表多云 (1s) 和晴朗 (0s) 的天空像素)并将它们加在一起以找到每个像素中存在云的观测总数。
我现在想找出云的百分比(云的数量(1s)/总观察值*100),但由于我想修改原始变量,因此无法使用 NumPy 数组在 Python 中使用它。
到目前为止,我的(简化的)代码是:
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)。
【问题讨论】:
-
看看 numpy 及其数组。这是一个完美的用例。
-
我不明白你的评论?我正在使用 NumPy 数组,但它不起作用?请详细说明?
-
你能澄清你的代码吗?我认为问题在于当你想做一个深拷贝时你正在做一个浅拷贝。见docs.python.org/2/library/copy.html
-
你能给出你想要的示例输出吗?
-
你用什么格式输入系列?因为直接在输入上计算它可能更容易,如果你有 3D 的话。