Hi, I was wondering if I can get some perspective on optimizing a function that has nested for-loops?

I looked up a tutorial and created a function based upon its script. It's essentially used so I can select dependent variables that's a subset of a data frame. It runs but it is very very slow.

How would I flatten a nested for-loop such as this?

I tried implementing an enumerate version but it did not work. Ideally I'd like the complexity to be linear, currently, it's at 2^n. I'm not sure, how I can flatten the nested for loop, such that I can append the results of a function to a list.

def BestSubsetSelection(X,Y, plot = True):
    k = len(X.columns)
    RSS_list = []
    R_squared_list = []
    feature_list = []
    numb_features = []

    # Loop over all possible combinations of k features
    for k in range(1, len(X.columns) + 1):
            # Looping over all possible combinations: from 11 choose k
            for combo in itertools.combinations(X.columns,k):
                # Store temporary results
                temp_results = fit_linear_reg(X[list(combo)],Y)

                # Append RSS to RSS Lists

                # Append R-Squared TO R-Squared list

                # Append Feature/s to Feature list

                # Append the number of features to the number of features list

A copy of the full implementation can be found here: https://github.com/melmaniwan/Elections-Analysis/blob/master/Implementing%20Subset%20Selections.ipynb