通过算术函数迭代Numpy数组 [英] Iterating a Numpy array through arithmetic functions

查看:66
本文介绍了通过算术函数迭代Numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我上一期的代码中:

In the code I have gotten from my previous issue: issue I could use iterations whilst modifying the a value of multiplication. I want to use the .prod function but with iterations of multiplication, division and addition. The calculations will go as follows, for the first calculation 10 + 10 *50/100 = 15 with the equation (Starting_val + Starting_val * Random_numb/100). The first element in Random_numb is 50 and Starting_val is updated to the value of 15. So for the second calculations it will be 15 + 15 *74/100 = 26.1 The value of the Starting_val is updated from 15 to 26.1 in the second calculation. I do not how to iterate this function with numpy. I wish to not use a for loop for this function.

import numpy as np

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

Random_numb.prod(initial=Starting_val)
Starting_val + Starting_val * Random_numb/100

预期输出:

[15, 26.1, 27.405, 46.314, 69.471 ]

推荐答案

简单的算术转换为您提供

Simple artithmetic transformations give you

Starting_val * np.cumprod(Random_numb / 100 + 1)

结果:

array([15.      , 26.1     , 27.405   , 46.31445 , 69.471675])

这篇关于通过算术函数迭代Numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆