UNQ_C10, looping through the tuples

Hi! I can’t figure out what combination of keys i should use for constructing the param_d list (in fact i’m a bit lost in all the variables)…

loop through each tuple in the list of tuples

for val_tuple in combination_l_of_t: # complete this line
    param_d = {}
    
    # Enumerate each key in the original hyperparams dictionary
    for i, k in hyperparam.items(): # complete this line

        # add a key value pair to param_d for each value in val_tuple
        param_d[k] = 

I understand that this is the step when we want to transform the list of combinations of hyperparameters to a dictionary, but i can’t see what this for loop does…

Hi @Vlad_Poltorak ,

Lets review the instructions.

The first loop says:

loop through each tuple in the list of tuples

So we are traversing the val_tuple(s) in combination_l_of_t…

then inside the 2nd for-loop we have to…

add a key value pair to param_d for each value in val_tuple

Here we need to complete param_d[k] “for each value in val_tuple”.

Which val_tuple should we getting here?

Well, from our instruction, i guess we should be getting the val_tuple on the i’th position? Sorry if i’m a bit confused

Yes! That’s exactly it.

You may also have another issue in the second for-loop with the iteration over the hyperparameters. Lets see how this runs as is. If it fails we can work out this potential other issue.

Yup, when i set
param_d[k] = val_tuple[i]
i get the error “tuple indices must be integers or slices, not str”

For the 2nd loop the instruction is:

Enumerate each key in the original hyperparams dictionary

In your second loop you have:

for i, k in hyperparam.items():

Basically you are iterating here through the elements of the dictionary, instead of enumerating them.

Try a different for-loop, one which traverses the hyperparam enumerating each key.

You can experiment with this like so:

for i, j in hyperparams.items():
print(i)

vs

for i, j in enumerate(hyperparams):
print(i)

It seems i’ve overcome this issue now using enumerate (indeed i suspected it was the problem), but now i’m ruminating on the next section where i use the hyperparameters in the model:

    # Enumerate each key in the original hyperparams dictionary
    for i, k in enumerate(hyperparam.keys()): # complete this line
        
        # add a key value pair to param_d for each value in val_tuple
        param_d[k] = val_tuple[i]
    
    # append the param_dict to the list of dictionaries
    combination_l_of_d.append(param_d)
    

# For each hyperparam dictionary in the list of dictionaries:
for param_d in combination_l_of_d: # complete this line
    
    # Set the model to the given hyperparams
    estimator = clf(param_d)
    
    # Train the model on the training features and labels
    estimator.fit(X_train_hp,y_train_hp)
    
    # Predict the risk of death using the validation features
    preds = estimator.predict_proba(X_val_hp)

Geting “n_estimators must be an integer, got <class ‘dict’>”
Sorry for the long line of questions, it seems like the trickiest assignment in the specialization :sweat_smile:

Yes, this is a tricky one.

Remember from the test lab on sklearn on previous lesson that the param has to be passed in a special fashion, adding ** to it: clf(**param_d)

I encourage you to go over that test lab again to refresh your concepts :slight_smile:

Try that and let me know.

This is the snippet where the double asterisk is explained:

Let’s say you want to tweak this model’s default parameters. You can pass a dictionary containing the values you specify to the classifier when you instantiate it. Notice that these must be passed as keyword arguments, or kwargs , which are created by using the ** prefix

Check this link out if you want to learn more about args and kwargs in Python:

Thanks, indeed i forgot it was explained in the lab,
but still the same error appears (after correcting to “estimator = clf(**param_d)”),
“n_estimators must be an integer” :smiling_face_with_tear:

At this point, please send me a Direct MEssage with all your code (copy / paste) and I’ll take a look.

@Vlad_Poltorak ,

Make sure that you initialize hyper_param_l properly. This initi will need values. As per the instructions:

“Get the values of the hyperparam and store them as a list of lists”

Also make sure that combination_l_of_t is properly initialized as a “list”. Please read the instructions:

Generate a list of tuples with all possible combinations of the hyperparams

@Vlad_Poltorak ,

Please let me know how it goes.

Got it!
Wrote concordance_index(y_val_hp,preds), where i needed to add preds[:,1], also,
in the ‘if’ statement “estimator_score>best_estimator” confused the estimator with the best_score.

Great to know it is working now!

Go on with your great effort and move to week 2 :slight_smile:

Juan