我正在尝试运行W2V算法。我发现索引错误,不确定我要去哪里。这是错误:
IndexError:只有整数,切片(
:
),省略号(...
),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
这段代码来自于话语包,会议包,爆米花,笑话袋。我不确定错误在哪里。我发现np.divide
引发错误。我正在Windows上
答案 0 :(得分:2)
counter = counter + 1.
应该是
counter = counter + 1
(注意点)或counter += 1
。
点使counter
成为浮点数(因为1.
等于1.0
),并且浮点数不能用作索引。
答案 1 :(得分:0)
切片变量必须为整数。 (将浮点值替换为整数,例如:0。到0)
<script>
var url_file_to_run = "{% url "your_app:file_to_run_adress" 0 %}";
var url = "{% url "your_app:python_output" 0 %}";
$.ajax({
url: url_file_to_run,
}
}).done(function(data) {
$( location ).attr("href", url);
});
</script>