Numpy solver : max value
I`ve got 2 variables :
period and result
How would I tell the Numpy solver ( I assume that's what I would have to use ) to return the period value that gives me the highest result ? Period has to be between 1 and 100 .
Edit :
Right now period at 14, gives me a result of 40 . period at 12 . gives me a result of 42 .
Can I tell numpy to try every period value between 1 and 100 and result only the one with the best result ?
I gu开发者_JS百科ess I don`t really need Numpy for that, I was wondering if it could be a way to go .
Just declare a variable equal to the period value of the first, then do your own loop, changing the initial variable any time the current number is bigger:
biggest = period(0)
index = 0
for i in range (1, 100)
if period(i) > biggest
biggest = period(i)
index = i
return biggest
I am not certain i understand the question, but if i do then NumPy indexing might be best:
period = NP.arange(50)
result = NP.random.randint(1, 101, 50)
q = NP.argmax(result) # gives index of largest value in the result array
result[q] # returns the highest value in the result array
period[q] # returns the value of period corresponding to
# the highest value for result
It's not clear what data you have, but this should work:
for period in periods:
result = func(period)
if result > resultmax:
resultmax = result
or, if results
is a numpy.array
such that results[period]
is the result from period
, then simply
periodmax = results.argmax()
精彩评论