機器學習實戰KNN語法:dict.get() sorted() & itemgetter() tile() strip()&split() readlines&readline
阿新 • • 發佈:2018-11-26
1.dict.get()
from numpy import *
labels=['a','b','d','n','s']
dict={}
a=labels[3];
b=labels[4];
dict[a]=dict.get(a,0)+1
dict[b]=dict.get(a,2)+1
print(dict)
{'n': 1, 's': 2}
2.sorted(),itemgetter()
# from math import log import operator classCount={0:10,3:9,1:12} sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(0), reverse=True) print(sortedClassCount) [(3, 9), (1, 12), (0, 10)] # import operator c = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'A', 10)] #reverse降序排列,預設true。取C中一二維進行降序排列(字母和數字) d=sorted(c, key=operator.itemgetter(1,2),reverse=True) print(d) [('dave', 'A', 10), ('john', 'A', 15), ('jane', 'B', 12)] # import operator classCount={0:10,3:9,1:12} sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(0), reverse=True) print(sortedClassCount) [(3, 9), (1, 12), (0, 10)]
3.tile()
from numpy import *
a=[[1,2,3],[4,5,6]]
b=tile(a,(2,1))
print(b)
[[1 2 3]
[4 5 6]
[1 2 3]
[4 5 6]]
4.get()
https://blog.csdn.net/weixin_38705903/article/details/79231551
5.strip()&split()
https://www.cnblogs.com/yyxayz/p/4034299.html
6.readlines&readline