Python Forum

Full Version: Too Many Indexers Error In regression code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I have the code below that loops through every combination of columns in DF to create a subset of regression models and returns the best one. The code does not throw up any errors until I run the last line: best_subset(X, Y). It returns the following error : "IndexingError: Too many indexers".

Does anyone have an idea why it is not working?


import numpy as np
import pandas as pd
import urllib
from itertools import chain, combinations
import statsmodels.api as sm

#Data
Rawdata = pd.read_csv("C:\\Users\\Yell\Documents\\datafilePython.csv")

#Regression code
def best_subset(X, Y):
    n_features = X.shape[1]
    subsets = chain.from_iterable(combinations(range(n_features), k+1) for k in range(n_features))
    best_score = -np.inf
    best_subset = None
    for subset in subsets:
        lin_reg = sm.OLS(Y, X.iloc[:, subset]).fit()
        score = lin_reg.rsquared_adj
        if score > best_score:
            best_score, best_subset = score, subset
    return best_subset, best_score

#Define variables
X = Rawdata.iloc[:, 1:10]
y = Rawdata.iloc[:, 0]

#Run
best_subset(X, Y)
Thanks,

Bitten by Python
Please show the error trace complete and unaltered in error tags.
Hi. I'm running this in Spyder. The full error I am getting is:



best_subset(X, Y)
Traceback (most recent call last):

File "<ipython-input-550-183338afbfae>", line 1, in <module>
best_subset(X, Y)

File "<ipython-input-544-2c1113731b1c>", line 7, in best_subset
lin_reg = sm.OLS(Y, X.iloc[:, subset]).fit()

File "C:\Users\Lucas\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1494, in __getitem__
return self._getitem_tuple(key)

File "C:\Users\Lucas\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 2143, in _getitem_tuple
self._has_valid_tuple(tup)

File "C:\Users\Lucas\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 223, in _has_valid_tuple
self._validate_key(k, i)

File "C:\Users\Lucas\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 2074, in _validate_key
raise IndexingError('Too many indexers')

IndexingError: Too many indexers
please, the verbatim error (cun and paste) in error tags