在python中捕获异常

时间:2015-07-01 14:03:41

标签: python

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.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
    if nwords == 0 :
        return -1
    featureVec = np.divide(featureVec,nwords)
    return featureVec

上面的函数通过仅取字的特征向量的平均值来计算特征向量。但是如果单词的数量等于0,它会抛出一个错误,所以我把if条件处理掉了。但是,当我以下列方式调用此函数时,我现在遇到问题:

feature = makeFeatureVec(words, model, int(num_features))  
if feature != -1 :
      docs_feature_vec.append(feature)

以下是错误追溯:

Traceback (most recent call last):
  File "classifier.py", line 161, in <module>
    if __name__ == "__main__": main()
  File "classifier.py", line 159, in main
    classify(train_file, model_file, flag, num_features)
  File "classifier.py", line 144, in classify
    data,label = create_feature_vector_docs(train_file, model_file, flag, num_features)
  File "classifier.py", line 94, in create_feature_vector_docs
    if feature != -1 :
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

1 个答案:

答案 0 :(得分:2)

要处理python中的错误,您必须使用tryexcept

try:
    if feature != -1:
        doSomething()
except Exception: 
    doSomethingElse()

不会为你正在做的事情推荐这个解决方案。

在我看来,你不应该return -1而不是array

我返回None,然后使用

if feature is not None:
    doSomething()
else:
    doSomethingElse()

try代码中,您基本上希望发生Exception

这不是一个好习惯,而Exceptions只有在您不期待它们时才会真正发生。