Abhijit Rai
Abhijit Rai

Reputation: 31

How to set a variable equal to another local variable inside a function by passing an external argument to the function?

The below code snippet works fine for generating a random forest with a varying number of min_samples_leaf.

for x in [12,14,16,18]:
    rf = ensemble.RandomForestClassifier(n_estimators=2000,max_depth=None,criterion='entropy',
                                                                              max_features=5, n_jobs=-1, random_state=0,
                                                                              verbose=0,oob_score=True,bootstrap=True,
                                                                              min_samples_leaf =x)

Similarly, I want to vary other parameters, say max_features, then the code will change to-

for x in [12,14,16,18]:
    rf = ensemble.RandomForestClassifier(n_estimators=2000,max_depth=None,criterion='entropy',
                                                                              max_features=x, n_jobs=-1, random_state=0,
                                                                              verbose=0,oob_score=True,bootstrap=True,
                                                                              min_samples_leaf =14)

This also works fine, with the generalization being, I have to set as "x" the parameter I want to loop. keeping this in mind I have written this function -

def forest_tuning(parameter,tree,crit,mf,rs,msf):
#Some Codes here
    for x in parameter:
        rf = ensemble.RandomForestClassifier(n_estimators=tree,max_depth=None,criterion=crit,
                                                                                  max_features=mf, n_jobs=-1, random_state=rs,
                                                                                  verbose=0,oob_score=True,bootstrap=True,
                                                                                  min_samples_leaf =msf)
    #Some Codes here
#Function call
p=forest_tuning(parameter=[40,50],tree=2000, crit='entropy',mf=5,rs=0,msf=x)  

I have set variable (msf) as "x", the counter variable to replicate the first code. This approach isn't working, as the "x" passed is not able to bind with the counter variable.

I am not able to pass an argument to a function, and trick the function to treat it as a local variable.The static "x" is local, and one passed is global hence the problem .

I can think of statically placing "x" as in 1st 2 codes, but then I lose the flexibility and compactness of passing arguments in a function.

Please help me with generalizing this for loop with a function.

Upvotes: 0

Views: 206

Answers (2)

jme
jme

Reputation: 20715

If I understand your question correctly, you can have the default behavior of the function be such that if mst is not specified, it takes the value of x at each iteration of your loop. If it is specified, then this default behavior is overridden:

def forest_tuning(parameter, tree, crit, mf, rs, msf=None):
    #Some Codes here
    for x in parameter:
        if msf is None:
            msf = x
        rf = ensemble.RandomForestClassifier(
                n_estimators=tree,max_depth=None,criterion=crit,                                                              
                max_features=mf, n_jobs=-1, random_state=rs,
                verbose=0,oob_score=True,bootstrap=True,
                min_samples_leaf=msf)

So that writing:

>>> forest_tuning(parameter=[40,50], tree=2000, crit='entropy',
                  mf=5, rs=0)

will set min_samples_leaf to x at each iteration of the loop, while:

 >>> forest_tuning(parameter=[40,50], tree=2000, crit='entropy', 
                   mf=5, rs=0, msf=10)

will set it to 10 for each iteration. You could even be fancier and allow the function to accept an iterable for msf so that different values of min_samples_leaf are used on each iteration:

from itertools import repeat

def forest_tuning(parameter, tree, crit, mf, rs, msf=None):
        try:
            msf = iter(msf)
        except TypeError:
            if msf is None:
                msf = iter(parameter)
            else:
                msf = repeat(msf)

        for x in parameter:
            rf = ensemble.RandomForestClassifier(
                    n_estimators=tree,max_depth=None,criterion=crit,                                                              
                    max_features=mf, n_jobs=-1, random_state=rs,
                    verbose=0,oob_score=True,bootstrap=True,
                    min_samples_leaf=next(msf))

So now you can write things like...

>>> forest_tuning(parameter=[40,50], tree=2000, crit='entropy', 
                       mf=5, rs=0, msf=[5, 10])

Upvotes: 1

jcfollower
jcfollower

Reputation: 3158

I think this would work, but I can't say that it's a good way to do it ...

def forest_tuning(parameter, param_type, tree, crit, mf, rs, msf):

    all_params = {'mf': mf,
                  'msf': msf}

#Some Codes here
    for x in parameter:
        all_params[param_type] = x
        rf = ensemble.RandomForestClassifier(n_estimators=tree,max_depth=None,criterion=crit,
                max_features=all_params['mf'], n_jobs=-1, random_state=rs,
                verbose=0,oob_score=True,bootstrap=True,
                min_samples_leaf =all_params['msf'])
    #Some Codes here

#Function call
p=forest_tuning(parameter=[40,50], 'msf', tree=2000, crit='entropy',mf=5,rs=0,msf=x)

Upvotes: 0

Related Questions