浮动列表中最大值的索引| Python

浮动列表中最大值的索引| Python,python,list,indexing,floating-point,max,Python,List,Indexing,Floating Point,Max,我看过不少帖子,但似乎没有一篇有用 我想计算术语频率&逆文档频率;用于深度学习的一袋单词技巧。此代码的目的只是计算公式。我在这里没有实现ANN 下面是一个简单的代码示例。在for循环之后,我遇到了这个问题 import math docs = 1000 words_per_doc = 100 # length of doc #word_freq = 10 #doc_freq = 100 dp = 4 print('Term Frequency Inverse Document Freque

我看过不少帖子,但似乎没有一篇有用

我想计算术语频率&逆文档频率;用于深度学习的一袋单词技巧。此代码的目的只是计算公式。我在这里没有实现ANN

下面是一个简单的代码示例。在for循环之后,我遇到了这个问题

import math

docs = 1000
words_per_doc = 100  # length of doc
#word_freq = 10
#doc_freq = 100
dp = 4

print('Term Frequency Inverse Document Frequency')
# term, word_freq, doc_freq
words = [['the', 10, 100], ['python', 10, 900]]
tfidf_ = []
for idx, val in enumerate(words):
  print(words[idx][0] + ':')
  word_freq = words[idx][1]
  doc_freq = words[idx][2]

  tf = round(word_freq/words_per_doc, dp)
  idf = round(math.log10(docs/doc_freq), dp)
  tfidf = round((tf*idf), dp)
  print(str(tf) + ' * ' + str(idf) + ' = ' + str(tfidf))
  tfidf_.append(tfidf)
  print()

max_val = max(tfidf)
max_idx = tfidf.index(max_val)

#max_idx = tfidf.index(max(tfidf))
lowest_idx = 1 - max_idx

print('Therefore, \'' + words[max_idx][0] + '\' semantically is more important than  \'' + words[lowest_idx][0] + '\'.')


#print('log(N/|{d∈D:w∈W}|)')
错误:

line 25, in <module>
    max_val = max(tfidf)
TypeError: 'float' object is not iterable
第25行,在
最大值=最大值(tfidf)
TypeError:“float”对象不可编辑

您试图在函数上传递tfidf,而不是tfidf_

tfidf是int,tfidf_uu是您的列表

所以代码应该是

max_val = max(tfidf_)
max_idx = tfidf_.index(max_val)

你能编辑你的帖子以包含错误信息吗?什么错误?接缝对我来说很好。我得到max_idx=0是的,对不起。完成。是否有名为
max
的变量?正如其他人所说,错误似乎出现在您尚未共享的代码行上