Python入门(基础)踩过的坑

来源:互联网 发布:三国志9怎么开优化 编辑:程序博客网 时间:2024/05/21 19:47

AttributeError: ‘dict’ object has no attribute ‘iteritems’
从python3.5开始字典不在有这个属性,iteritems变为items。

对于函数operator.itemgetter()的理解:
例一:
对dist=[(a,dist1,d1),(b,dist2,d2),(c,dist,d3)]里的三个元素做排序
dist.sort(key = operator.itemgetter(1))#根据每个元素里的第二个数值大小来排序。
reverse参数就不用多说了,是一个bool变量,表示升序还是降序排列,默认为false(升序排列),定义为True时将按降序排列。

例二:
a =[1,2,3]
b=operator.itemgetter(1)
b(a)=2

环境:scikit-learn 0.18 , python3.6X
在用别人程序的时候,报警告:

#from sklearn.cross_validation import train_test_split#from sklearn.grid_search import GridSearchCV

klearn\cross_validation.py”, line 41
“This module will be removed in 0.20.”, DeprecationWarning)
DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.

解决办法:该模块在0.18版本中被弃用,支持所有重构的类和函数都被移动到的model_selection模块

from sklearn.model_selection import train_test_splitfrom sklearn.model_selection import GridSearchCV