IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices"
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices"
我正在尝试 运行 W2V 算法。我发现索引错误并且不确定我哪里出错了。这是错误:
IndexError: only integers, slices (:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices
这是代码:
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
counter = counter + 1.
应该是
counter = counter + 1
(注意圆点)或 counter += 1
。
点使 counter
成为浮点数(因为 1.
等同于 1.0
)并且浮点数不能用作索引。
切片变量必须是整数。 (用整数替换浮点值,例如: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
我正在尝试 运行 W2V 算法。我发现索引错误并且不确定我哪里出错了。这是错误:
IndexError: only integers, slices (
:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices
这是代码:
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
counter = counter + 1.
应该是
counter = counter + 1
(注意圆点)或 counter += 1
。
点使 counter
成为浮点数(因为 1.
等同于 1.0
)并且浮点数不能用作索引。
切片变量必须是整数。 (用整数替换浮点值,例如: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