max_features, int
来源:6-6 数据处理-句子编码化处理(训练字典)

慕姐7185533
2018-11-29
if isinstance(max_features, int):
count = sorted(list(count.items()), key=lambda x: x[1])
if max_features is not None and len(count) > max_features:
count = count[-int(max_features):]
for w, _ in count:
self.dict[w] = len(self.dict)
else:
for w in sorted(count.keys()):
self.dict[w] = len(self.dict)
不太懂这一段代码,为什么需要判断max_features是不是属于int类型?
第二行中key=lambda x: x[1]的意思是把v当做排序的k进行排序吗?如果是的话为什么要用v来进行排序呢?
第三行中len(count) > max_features是什么意思?为什么需要判断len(count)是否大于max_features?
两个for循环也没大看懂,这两个for循环完成了什么内容呢?
谢谢老师或同学的解答!
写回答
4回答
-
dh1211
2019-01-01
我的理解是,
1、“第二行中key=lambda x: x[1]的意思是把v当做排序的k进行排序吗?如果是的话为什么要用v来进行排序呢”:这个是按词频进行排序,词频对应count的value
2、“第三行中len(count) > max_features是什么意思?为什么需要判断len(count)是否大于max_features?":
如果没有限制features的长度(max_features)则用所有的,如果限制了,取词频最高的max_features长的特征。
3、判断int类型,大概是为了这句话count = count[-int(max_features):]
20 -
慕运维1087574
2019-02-27
同问,python语法和java差很多,希望老师讲讲什么函数是干嘛的
10 -
慕虎2364166
2018-12-26
同问,感觉老师光写代码了,内容讲的不清楚!
10 -
Stauffenb
2019-05-21
请老师解答一下问题
00