Loop to test range of variables and find the best ones in Pyhton
Question:
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
Answers:
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
.
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
)
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
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
.
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
)