Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Too Many Indexers Error In regression code

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

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]

best_subset(X, Y)

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\", line 1494, in __getitem__
return self._getitem_tuple(key)

File "C:\Users\Lucas\Anaconda3\lib\site-packages\pandas\core\", line 2143, in _getitem_tuple

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

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

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Linear Regression Python3 code giving weird solutions deepsen 0 211 Nov-01-2019, 12:06 PM
Last Post: deepsen

Forum Jump:

Users browsing this thread: 1 Guest(s)