0

I am trying to create multiple datasets using different oversampling methodologies from imblearn, name them based on a list, and then be able to call them later based on those names. When trying to name the outputs I am inadvertently adding the whole dataset to the list rather than assigning the string object from the list as the name for the dataset object.

I read through this post, but it isn't clear to me if / how it applies to my use case: How do I create variable variables?

I am using python 3.7.5

Where I think issue is popping up:

os_X_train_list[i], os_y_train_list[i] = sampler().fit_resample(X_train, y_train)

I tried wrapping each "name" in a str() so the interpreter would recognize it as such rather than a location in the list, but then got a syntax error.

Full(er) code:

import imblearn.over_sampling as imbos

oversampling_list = ['RandomOverSampler', 'SMOTE']
os_X_train_list = []
os_y_train_list = []

for i in oversampling_list:
    os_X_train_list.append(i+'_X_train_resample')
    os_y_train_lust.append(i+'_y_train_resample')

df_sample_list = []

for i in range(len(overampling_list)):
   print(f'Oversampling using {oversampling_list[i]}')
   sampler = getattr(imbos, over_sampling_list[i])
   os_X_train_list[i], os_y_train_list[i] = sampler().fit_resample(X_train, y_train)
   df_sample_list.append(os_X_train_list[i])
   df_sample_list.append(os_y_train_list[i])

Thanks in advance for your help

  • 1
    Why do you need to give them dynamic names?! Just `a, b = sampler()...` `df_sample_list.append(a)` `df_sample_list.append(b)` will do just fine. Or in fact even just `df_sample_list.extend(sampler()...)`. – deceze Oct 07 '21 at 15:36
  • Static names will get overwritten when loop iterates. I want to create objects for each loop through and be able to access them afterwards. – user6825943 Oct 07 '21 at 15:44
  • But you don’t need those names afterwards, do you? If you just want to append the results to a list, the intermediate names don’t matter. And if you *do* need those names afterwards, how are you going to know which names you can access when they’ve been created dynamically? – deceze Oct 07 '21 at 16:01
  • I do need them afterwards as I want to be able to access each unique dataset created by the loop. I would be able to access them if there is a list with all of the dynamically created names. I was just able to resolve by using a dictionary rather than a list to call the dataset names. Thanks for your help – user6825943 Oct 07 '21 at 16:10
  • Yes, exactly, a *dict* can be used for mapping names to values; as the duplicate says… – deceze Oct 07 '21 at 17:00
  • Yeah, I didn't get it the first time around. – user6825943 Oct 07 '21 at 19:57

0 Answers0