IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

3,450 views
Skip to first unread message

Siva Kumar S

unread,
Apr 26, 2017, 1:09:36 PM4/26/17
to gensim
Here is my code
def makeFeatureVec(words, model, num_features):
    featureVec=np.zeros((num_features, ), dtype="float32")
    nwords=0.
    index2word_set=set(model.wv.index2word)
    for word in words:
        if word in index2word_set:
            nwords=nwords + 1.
            featureVec=np.add(featureVec,model[word])
    featureVec=np.divide(featureVec,nwords)
    return featureVec

def getAvgFeatureVecs(reviews, model, num_features):
    counter = 0.
    reviewFeatureVecs = np.zeros((len(reviews), num_features), dtype="float32")
    for review in reviews:
        if counter%1000. == 0.:
            print "Review %d of %d" % (counter, len(reviews))
        reviewFeatureVecs[counter] = makeFeatureVec(review, model, num_features)     <== Line no 182
        counter = counter + 1.
    return reviewFeatureVecs

Runtime Error Message
Traceback (most recent call last):
  File "/test_IMDB_W2V_RF.py", line 198, in <module>
    trainDataVecs=getAvgFeatureVecs(clean_train_reviews, model, num_features)
  File "/test_IMDB_W2V_RF.py", line 182, in getAvgFeatureVecs
    reviewFeatureVecs[counter] = makeFeatureVec(review, model, num_features)
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Process finished with exit code 1

Description :

Can anyone explain how to overcome the indexing error.

Gordon Mohr

unread,
Apr 26, 2017, 3:16:07 PM4/26/17
to gensim
By placing a `.` after your `0` when assigning to `counter`, you've made it a float value (`0.0`). Float values aren't valid for array-indexing (as with `[]`). 

Using an integer (plain `0` without a trailing period) should work – and in general, default to ints for counting/looping purposes unless you're certain you need something else. 

- Gordon

Lev Konstantinovskiy

unread,
Apr 26, 2017, 8:00:54 PM4/26/17
to gensim
It is a recent change in numpy - we had to do some refactoring in gensim to accomodate it too.

varada vamsi

unread,
Apr 14, 2018, 1:06:42 PM4/14/18
to gensim
I have the same condition where i removed .  after integer but i am getting the same error.

 File "C:\Users\varada.vamsi\Documents\stockmarket\StockPricePrediction-master\scripts\Algorithms\regression_helpers.py", line 226, in performRegression
    fine_tune=False, maxiter=maxiter, SGD=True, batch=batch, rho=0.9))
  File "C:\Users\varada.vamsi\Documents\stockmarket\StockPricePrediction-master\scripts\Algorithms\regression_helpers.py", line 265, in benchmark_model
    model.fit(train[features].as_matrix(), train[output].as_matrix(), *args, **kwargs)
  File "C:\Users\varada.vamsi\Documents\stockmarket\StockPricePrediction-master\scripts\Algorithms\Neural_Network.py", line 204, in fit
    Y_new[i,v] = 1
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices



Reply all
Reply to author
Forward
0 new messages