I'm trying to use a function to iteratively return several machine learning models (pickles) with a function based on the accuracy cutoff I specify.
My issue is that I'm trying to load the pickles with eval, as their names correspond to the number given by sdf['number']. The eval function is not loading my pickles, and beyond that, I want them to be loaded and returned by my function. I have tested this by attempting to directly run data through each model after loading it before it moves on to the next one, but it is returning "learn0 not defined" for example.
Any thoughts on how to better do this iteratively?
Variables Explained:
jar = A list of the different variable names (learner names) that I expected it to load. For example, learn0, learn1, etc.
cutoff = Accuracy Cutoff
sdf_temp = Temporary Study DataFrame
def piklJar(sdf,cutoff):
sdf_temp = sdf[sdf['value'] <= cutoff]
jar = []
i=0
for pklNum in sdf_temp['number']:
eval('"learn{} = load_learner({}/Models/Pkl {}.pkl)".format(i,datapath,pklNum)')
jar.append('learn{}'.format(i))
i+=1
return jar
eval
isn't needed. Your example wasn't working code, but this is approximately the same thing:
def piklJar(sdf,cutoff):
sdf_temp = sdf[sdf['value'] <= cutoff]
return [load_learner(f'{datapath}/Models/Pkl {pklNum}') for pklNum in sdf_temp['number']]
After calling jar = pklJar(...)
, jar[0]
would be equivalent to learn0
, jar[1]
would be learn1
, etc. The various load_learner
calls are stored in a list generated from a list comprehension.