The*_*tor 0 python arrays iteration numpy
我有9个数组,每个数组包含19个值.
让我们说它们a1,a2,a3,a4,a5,a6,a7,a8,a9(每个a1,a2 ... a9每个包含19个值)并让我们称它们为a数组.
我还有9个数组,每个数组包含19个值.
假设它们是b1,b2,b3,b4,b5,b6,b7,b8,b9(每个b1,b2 ... b9各包含19个值),让我们称它们为b数组.
我现在想采取每个的第一个值 a数组和每个的第一个值 b阵列,分化他们(a/b),这将给我一个新的阵列,假设a/b有19个值.然后我使用计算这19个值的标准差numpy.std.
然后我想再次遍历这些数组,但这次是每个数组的第二个值,依此类推,直到最后一个(第19个)值并执行上述操作.
如果我只用了2门阵列(比如a1和b1)我可以用zip这样的:
div_array = [] # The empty array that will have the divided values
for a,b in zip(a1,b1):
div = a/b
div_array.append(div)
std = np.std(div_array)
Run Code Online (Sandbox Code Playgroud)
如何在我冗长的情况下重复上述内容?
编辑:
我最终需要19个不同的标准偏差,即我为第一个值计算它,然后计算第二个值,依此类推.
numpy如果您使用它,为什么不使用分裂的力量std?
>>> # You can create these array in a loop if you want
>>> a = np.array([a1, a2, a3, ..., a9])
>>> b = np.array([b1, b2, b3, ..., b9])
>>> c = np.std(a / b, 0)
Run Code Online (Sandbox Code Playgroud)
示例(详细信息np.std):
>>> a1 = np.array([1, 2, 3])
>>> a2 = np.array([2, 3, 4])
>>> a = np.array([a1, a2])
>>> a
array([[1, 2, 3],
[2, 3, 4]])
>>> b1 = np.array([10, 100, 1000])
>>> b2 = np.array([20, 200, 2000])
>>> b = np.array([b1, b2])
>>> b
array([[10, 100, 1000],
[20, 200, 2000]])
>>> a/b
array([[0.1, 0.02, 0.003],
[0.1, 0.015, 0.002]])
>>> np.std(a/b) # The standard deviation of the whole matrix
0.04289...
>>> np.std(a/b, 0) # The standard deviation of each column
array([0, 0.0025, 0.0005])
>>> np.std(a/b, 1) # The standard deviation of each row
array([0.04229263, 0.04345879])
Run Code Online (Sandbox Code Playgroud)