KNN(K-紧邻)算法——用到的一些函数tile(),sum(),argsort(),get(),sorted(),itemgetter()

numpy中的tile()函数,numpy.tile()

例子

>>> import numpy as np
>>> np.tile([0,0],4)#在列方向上重复[0,0]4次
array([0, 0, 0, 0, 0, 0, 0, 0])
>>> np.tile([0,0],(2,3))#在行方向重复[0,0] 2次,在在列方向上重复[0,0]3次
array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

Python 的内置函数 sum()函数

例子

>>> sum([1,2,3])#所有的元素相加
6

Numpy中的sum()函数,numpy.sum()

例子

>>> import numpy as np
>>> np.sum([[1,2,3],[4,5,6]])#所有元素相加
21
>>> np.sum([[1,2,3],[4,5,6]],axis=0)#列表中,每一个列表对应的列相加
array([5, 7, 9])
>>> np.sum([[1,2,3],[4,5,6],[7,8,9]],axis=0)#列表中,每一个列表对应的列相加
array([12, 15, 18])
>>> np.sum([[1,2,3],[4,5,6]],axis=1)#列表中,每一个列表自身元素相加,组成新的列表
array([ 6, 15])
>>> np.sum([[1,2,3],[4,5,6],[7,8,9]],axis=1)#列表中,每一个列表自身元素相加,组成新的列表
array([ 6, 15, 24])

Numpy中的argsort()函数,numpy.argsort()返回的是数组值从小到大的索引值

例子

>>> import numpy as np
>>> a=np.array([2,1,3])
>>> np.argsort(a)#从小到大返回矩阵a中的索引值
array([1, 0, 2], dtype=int64)
>>> b = np.array([[1, 2], [3, 4]])
>>> np.argsort(b,axis=0)#列表中,每一个列表对应的列相比较,返回的索引值在同一列
array([[0, 0],
       [1, 1]], dtype=int64)
>>> c = np.array([[0, 3], [2, 1],[3,6]])
>>> np.argsort(c,axis=0)#列表中,每一个列表对应的列相比较,返回的索引值在同一列
array([[0, 1],
       [1, 0],
       [2, 2]], dtype=int64)
>>> np.argsort(b,axis=1)#列表中,每一个列表本身的元素相比较,返回的索引值在同一行
array([[0, 1],
       [0, 1]], dtype=int64)
>>> np.argsort(c, axis=1)#列表中,每一个列表本身的元素相比较,返回的索引值在同一行
array([[0, 1],
       [1, 0],
       [0, 1]], dtype=int64)

Python 字典(Dictionary) get()函数,返回指定键的值,如果值不在字典中返回默认值None

例子

>>> a={'red':1,'black':2,'green':3}#创建一个字典
>>> a.get('red')#获取‘red’的值
1
>>> print( a.get('blue'))#‘blue’不再字典中返回None
None
>>> a.get('blue',4)+1#把‘blue’的值加1
5
>>> a.get('green',4)+1#'green'的值已经存在字典中再给‘green’赋值4没有任何作用,'green'还是原来的值3
4

Python 的内置函数 sorted()函数,sorted ()可以对所有可迭代的对象进行排序操作,不会改变原始数据。

例子

>>> a=[5, 2, 3, 1, 4]
>>> sorted(a)
[1, 2, 3, 4, 5]
>>> a
[5, 2, 3, 1, 4]

sorted(iterable, key, reverse)的语法

(1) iterable指定要排序的list或者iterable

(2) key为函数,指定取待排序元素的哪一项进行排序

(3) reverse是一个bool变量,表示升序还是降序排列,默认为false(升序排列),定义为True时将按降序排列。

operator.itemgetter()函数,operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号

>>> a = [1,2,3]
>>> b=operator.itemgetter(1) #定义函数b,获取对象的第1个域的值
>>> b(a)
2
>>> b=operator.itemgetter(1,0)   #定义函数b,获取对象的第1个域和第0个的值
>>> b(a)
(2, 1)

sorted()函数与operator.itemgetter()函数结合

例子

>>>import operator
>>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>> sorted(students,key=lambda student:student[2])#以students所有元素的第三个域来进行排序
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
#有了上面的operator.itemgetter函数,也可以用该函数来实现,例如要通过student的第三个域排序,可以这么写
sorted(students, key=operator.itemgetter(2))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
sorted(students, key=operator.itemgetter(1,2)) #先根据第二个域排序,再根据第三个域排序
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
#等价于
>>> a=sorted(students, key=operator.itemgetter(1))#根据第二个域排序
>>> a
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>> sorted(students, key=operator.itemgetter(2))#根据第三个域排序
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]




猜你喜欢

转载自blog.csdn.net/zykbest/article/details/80863799