IndexError: допустимыми являются только целые числа, срезы (`:`), многоточие (`...`), numpy.newaxis (`None`) и целые или логические массивы" - PullRequest
0 голосов
/ 09 октября 2018

Я пытаюсь запустить алгоритм W2V.Я нахожу ошибку индекса и не уверен, где я иду неправильноВот ошибка:

IndexError: допустимы индексы * 1007, только целые числа, срезы (:), многоточие (...), numpy.newaxis (None) и целые или логические массивы.*

и вот код:

    def makeFeatureVec(words, model, num_features):
# Function to average all of the word vectors in a given
# paragraph
#
# Pre-initialize an empty numpy array (for speed)
featureVec = np.zeros((num_features,),dtype="float32")
#
nwords = 0.
# 
# Index2word is a list that contains the names of the words in 
# the model's vocabulary. Convert it to a set, for speed 
index2word_set = set(model.wv.index2word)
#
# Loop over each word in the review and, if it is in the model's
# vocaublary, add its feature vector to the total
for word in words:
    if word in index2word_set: 
        nwords = nwords + 1.
        featureVec = np.add(featureVec,model[word])
# 
# Divide the result by the number of words to get the average
featureVec = np.true_divide(featureVec,nwords)
return featureVec

    def getAvgFeatureVecs(reviews,model,num_features):
# Given a set of reviews (each one a list of words), calculate 
# the average feature vector for each one and return a 2D numpy array 
# 
# Initialize a counter
counter = 0.
# 
# Preallocate a 2D numpy array, for speed
reviewFeatureVecs = np.zeros((len(reviews),num_features),dtype="float32")
# 
# Loop through the reviews
for review in reviews:
   #
   # Print a status message every 1000th review
    if counter%1000. == 0.:
        print ("Review %d of %d" % (counter, len(reviews)))
   # 
   # Call the function (defined above) that makes average feature vectors
    reviewFeatureVecs[counter] = makeFeatureVec(review, model,num_features)
   #
   # Increment the counter
    counter = counter + 1.
return reviewFeatureVecs

Этот фрагмент кода от Bag-of-Words-Meets-Bags-of-Popcorn-Kaggle.Я не уверен, где ошибка.Я думаю, что np.divide вызывает ошибку.Я работаю на windows

Ответы [ 2 ]

0 голосов
/ 15 мая 2019

Переменная Slicing должна быть целым числом.(Заменить значения с плавающей запятой на целое число, например: от 0 до 0)

1)    nwords = 0.

2)   # Print a status message every 1000th review
        if counter%1000. == 0.: 
            print ("Review %d of %d" % (counter, len(reviews)))

3)   # Increment the counter
         counter = counter + 1.
      return reviewFeatureVecs
0 голосов
/ 09 октября 2018

counter = counter + 1.

должно быть

counter = counter + 1 (отметьте точку) или counter += 1.

Точка превращает counter в число с плавающей точкой (поскольку 1. эквивалентно 1.0), и числа с плавающей точкой не могут использоваться в качестве индексов.

...