我收到错误,无法将序列乘以'float'类型的非整数

时间:2019-09-10 06:49:38

标签: python python-3.x vectorization tf-idf

虽然计算平均值word2vec低于错误 我遇到错误

TypeError: can't multiply sequence by non-int of type 'float' at line tf_idf = dictionary[word]*(sentence.count(word)/len(sentence))

平均Word2Vec

# compute average word2vec for each review.
X_train_preprocessed_essays_tfidf_w2v_vectors = [];
for sentence in tqdm(X_train['preprocessed_essays']):
    vector = np.zeros(300)
    tf_idf_weight =0;
    for word in sentence.split():
        if (word in glove_words) and (word in tfidf_words):
            vec = model[word]
            tf_idf = dictionary[word]*(sentence.count(word)/len(sentence))
            vector += (vec * tf_idf)
            tf_idf_weight += tf_idf
    if tf_idf_weight != 0:
        vector /= tf_idf_weight
    X_train_preprocessed_essays_tfidf_w2v_vectors.append(vector)
print(len(X_train_preprocessed_essays_tfidf_w2v_vectors))
print(len(X_train_preprocessed_essays_tfidf_w2v_vectors[0]))

我得到的错误:

  0%|                                                                                        | 0/22445 [00:00<?, ?it/s]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-167-8bbd70da23d0> in <module>
      9             vec = model[word] # getting the vector for each word
     10             # here we are multiplying idf value(dictionary[word]) and th
---> 11             tf_idf = dictionary[word]*(sentence.count(word)/len(sentence))
     12             vector += (vec * tf_idf) # calculating tfidf weighted w2v
     13             tf_idf_weight += tf_idf

TypeError: can't multiply sequence by non-int of type 'float'

0 个答案:

没有答案