在Numpy函数Python中覆盖数组

在Numpy函数Python中覆盖数组

本文介绍了在Numpy函数Python中覆盖数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 numpy 函数,该函数会对其自身进行迭代以更新其函数的值.例如,如果 Random_numb 等于 [50,74,5,69,50] .因此,对于第一个计算,计算将类似于 10 * 50 = 500 ,方程式为 Starting_val = Starting_val * Random_numb .Starting_Val等于 500 ,因此对于第二次计算,它将为 500 * 74 = 37000 .将 Startin_Val 500 更新为 37000 .在进行计算时,依次遍历 Random_numb ,使用元素1: 50 进行计算1,使用元素2 74 进行计算2,依此类推.计算将一直进行到 Random_numb 数组的末尾.

I am trying to write a numpy function that iterates with itself to update the values of its function. If for example Random_numb was equal to [50, 74, 5, 69, 50]. So the calculations would go like, 10* 50 = 500 for the first calculation, with the equation Starting_val = Starting_val * Random_numb. The Starting_Val would equal to 500 so for the second calculation it would go as 500 * 74 = 37000. Updating the Startin_Val to 37000 from 500. Iterating through the Random_numb as it does the calculations, using element 1: 50 for calculation 1 and element 2 74 for calculation 2 and so on. The calculations would go on until the end of the Random_numb array.

import numpy as np
Starting_val = 10
Random_numb = random.randint(100, size=(5))
Starting_val = Starting_val * Random_numb

推荐答案

IIUC,您正在寻找 产品 :

IIUC you're looking for prod:

import numpy as np

Starting_val = 10
Random_numb = np.array([50, 74, 5, 69, 50])

Random_numb.prod(initial=Starting_val)
#638250000

如果您对数组的乘法值感兴趣,它将为 cumprod :

If you're interested in the multiplied values of the array it'll be cumprod:

Starting_val * Random_numb.cumprod()
# array([      500,     37000,    185000,  12765000, 638250000])

这篇关于在Numpy函数Python中覆盖数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:00