Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

Overwriting an array in Numpy function Python

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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...