0

First of all thank you for this amazing space to learn from you all, I have the following question. I created a code that imports data from several APIs and it performs various statistical operations around the data. And it returns 1 result number, which is helpful to see if the model is correct. The code is built around 4 (or more), static variables. What I want is to create a range of number for each of those variables and test all possible combinations in the code, that returns the top 10 highest result numbers. it looks like this:


#I would want to test i.e. variable_1 in a range from (-0.004 to 0.004 at 0.001 increments), the same for the other variables until I can get the top 10 highest result_number
variable_1 = 0.004
variable_2 = 0.005
variable_3 = 30
result_number = 0

def my_code():
global variable_1, variable_2, variable_3
#perform all the operations utilizing the variables
return result_number


Please let me know if it is clear, or if you have any idea how to attack this. Thank YOU!!

I am new to Python so I am still trying to figure out how to loop and record the highest numbers as it goes. The code is fairly long so it will take a few hours for the simmulation

Alex P
  • 1,105
  • 6
  • 18
PythonNoob
  • 15
  • 6
  • Does this answer your question? [How do I use a decimal step value for range()?](https://stackoverflow.com/questions/477486/how-do-i-use-a-decimal-step-value-for-range) – Woodford Nov 01 '22 at 16:58

2 Answers2

1

Extracting the maximal result from a loop

best_result = None
for variable_1 in range(-4, 4, 1):
    result = my_code()
    if best_result is None or best_result > result:
        best_result = result

Iterating over decimals (numpy solution)

import numpy as np

best_result = None
for variable_1 in np.arange(-0.004, 0.004, 0.001):
    result = my_code()
    if best_result is None or best_result > result:
        best_result = result

Iterating over multiple variables (numpy & itertools solution)

import numpy as np
from itertools import product

variable_range = np.arange(-0.004, 0.004, 0.001)

best_result = None
for (variable_1, variable_2, variable_3) in product(variable_range, variable_range, variable_range):
    result = my_code()
    if best_result is None or best_result > result:
        best_result = result

Of course, if your variables have different ranges, you need to define them separately and use them accordingly in product.

Alex P
  • 1,105
  • 6
  • 18
  • This is very helpful!!! How would you record as you go which variables achieved that result. As I mentioned it would have to iterate through all the variables and their possible combinations. – PythonNoob Nov 01 '22 at 17:26
  • @PythonNoob Declare an empty list outside the loop, say `best_results = []` and inside the if-statement store the variable-result combination as a tuple in the list, like `best_results.append((variable_1, variable_2, variable_3, result))` – Alex P Nov 01 '22 at 17:29
  • @PythonNoob consider upvoting the answer and accepting it as the solution if it solved your problem – Alex P Nov 01 '22 at 17:40
  • KeyError has nothing to do with any code you or I posted. This is a new question and thus requires a new thread – Alex P Nov 01 '22 at 18:10
  • You are absolutely right. either way this was incredibly helpful! thank you Alex! – PythonNoob Nov 01 '22 at 18:17
0

I would use numpy to create lists of values with decimals, then you can check if the value is in this list:

import numpy as np

variable_1 = 0.004
variable_2 = 0.005
variable_3 = 30
result_number = 0

def my_code():
    global variable_1, variable_2, variable_3
    result_number = 0
    result_number += variable_1 in  np.arange(-0.004,0.005,0.001)
    result_number += variable_2 in  np.arange(-0.004,0.005,0.001)
    result_number += variable_3 in  np.arange(22,28,1)
    return result_number


my_code()

Something like this. To take in account:

np.arange(start,stop,step) create an array like: [start,start+step,start+2*step,... start+k*step] until start+k*step<stop so, you have to select stop> 'your last value seeked' (in your example stop>0.004 -> stop = 0.005)

Ulises Bussi
  • 1,635
  • 1
  • 2
  • 14