Search code examples
pythonlistinputproductpython-itertools

itertools.product for the full range of columns


as a part of my code, I'm trying to get a full factorial matrix, this is not a problem since I already have a working code for it. However, I would like to generalize it in a way that it wouldn't matter the number of inputs. This would require modifying the line:

for combination in itertools.product(X[0,:],X[1,:],X[2,:],X[3,:],X[4,:],X[5,:],X[6,:]):

input_list = dfraw.columns[0:n_inputs]
output_list = dfraw.columns[n_inputs:len(dfraw.columns)]

fflvls = 4
lhspoints = 60000
X = np.zeros((n_inputs, fflvls),float)
ii=0
for entrada in input_list:   
    X[ii] = np.linspace(min(dfraw[entrada]), max(dfraw[entrada]), fflvls)
    ii+=1
number=1
i=0
X_fact=np.zeros((int(fflvls**n_inputs),n_inputs),float)
for combination in itertools.product(X[0,:],X[1,:],X[2,:],X[3,:],X[4,:],X[5,:],X[6,:]):
    X_fact[i,:] = (combination)
    i +=1
    number+=1

I thought of writing the input of itertools.product as a string with a loop and then evaluating but it doesn't work and I've also seen it is regarded as bad practice

prodstring = ['X[0,:]']
for ii in range(n_inputs):
    prodstring.append(',X[%d,:]'%(ii))
in_products = ''.join(prodstring)
for combination in itertools.product(eval(in_products)):
    X_fact[i,:] = (combination)
    i +=1
    number+=1

what other way is there to inputing the full range of columns in this function? (or similar ones)


Solution

  • who said working harder is working better? im back from lunch and I delved into *args and **kwargs as a form of procrastination cause ive sometimes seen them mentioned and i was curious. It seems like it was just the tool I needed. In case this can help other code rookies like me in the future:

    args = ()
    for ii in range(n_inputs):
        b = (X[ii,:])
        args += (b,)
    for combination in itertools.product(*args):
        X_fact[i,:] = (combination)
        i +=1
        number+=1
    

    Seems to work properly. Solved in an hour of "not working" what i haven't solved in approx 4 hours of "working"