0

I keep getting the error IndexError: tuple index out of range and i am not sure what is happening. My code was working just fine however when i restarted the jupyter notebook i started receiving this error.

this is my code:

X = df.Tweet
y = df.target


from sklearn import linear_model
import pyswarms as ps

# Create an instance of the classifier
classifier = linear_model.LogisticRegression()

# Define objective function
def f_per_particle(m, alpha):

    total_features = X.shape[1]
    # Get the subset of the features from the binary mask
    if np.count_nonzero(m) == 0:
        X_subset = X
    else:
        X_subset = X[:,m==1]
    # Perform classification and store performance in P
    classifier.fit(X_subset, y)
    P = (classifier.predict(X_subset) == y).mean()
    # Compute for the objective function
    j = (alpha * (1.0 - P)
        + (1.0 - alpha) * (1 - (X_subset.shape[1] / total_features)))

    return j

[some more code]

options = {'c1': 0.5, 'c2': 0.5, 'w':0.9, 'k': 30, 'p':2}

# Call instance of PSO
dimensions = X.shape[1] # dimensions should be the number of features
optimizer = ps.discrete.BinaryPSO(n_particles=30, dimensions=dimensions, options=options)

# Perform optimization
cost, pos = optimizer.optimize(f, iters=1000)

i received the following traceback:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-76-bea8cf064cd2> in <module>
      2 
      3 # Call instance of PSO
----> 4 dimensions = X.shape[1] # dimensions should be the number of features
      5 optimizer = ps.discrete.BinaryPSO(n_particles=30, dimensions=dimensions, options=options)
      6 

IndexError: tuple index out of range
hellomoto
  • 25
  • 1
  • 4
  • Does this answer your question? [IndexError: tuple index out of range ----- Python](https://stackoverflow.com/questions/20296188/indexerror-tuple-index-out-of-range-python) – ashraful16 Jan 01 '21 at 20:57
  • What is this line X = df.Tweet? Like from where are you reading this? And what you can try is to print X before X.shape[1] – Ignacio Alorre Jan 01 '21 at 21:00
  • hello:) X contains a list of tweets that have been transformed using tfidf/count vectorizer and y is a label assigned to thetweets. i am trying to use pyswarms based on the code: https://github.com/ahcantao/PSOFeatureSelection/blob/master/feature_subset_selection.ipynb. However my code was working just fine before i restarted the notebook and found it a bit weird. when i check x.shape[0] i get an output of 8324 and when i did x.shape[1] i got the error tuple index oyt of range . When i change my code to X.shape[0] i get the error ValueError: key of type tuple not found and not a MultiIndex.n – hellomoto Jan 01 '21 at 21:09

1 Answers1

0

It is not absolutely clear, but it seems to me that your df variable might be a Pandas dataframe, and your df.Tweet may be a Pandas series.

In that case, being a series, your X will have only one dimension (so, only the first element of the tuple X.shape, X.shape[0]), instead of two dimensions - reason for the index out of range exception in your code. The two dimensions case occurs only when the variable is a dataframe.

More information: https://www.google.com/amp/s/www.geeksforgeeks.org/python-pandas-series-shape/amp/

nunohpinheiro
  • 2,169
  • 13
  • 14