k- near algorithm used functions

k- near algorithm used functions

  1. argsort ()
 import numpy as np
 x=np.array([1,4,3,-1,6,9])
 x.argsort()

Output is defined as y = array ([3,0,2,1,4,5]). x [3] = - 1 minimum, x [5] = 9 maximum; according to ascending order of the index number corresponding to the output.

  1. GET (Key, default = None)
    Key - key to the dictionary to find.
    default - if the specified key does not exist, the return value is the default value.
dict = {'Name': 'Zara', 'Age': 27}

print "Value : %s" %  dict.get('Age')
print "Value : %s" %  dict.get('Sex', "Never")

Examples of the above output is:

Value : 27
Value : Never
  1. the sorted ()
    the sorted Syntax:
    the sorted (Iterable [, CMP [, Key [, Reverse]]])
    Parameters:
    Iterable - iterables.
    cmp - compare function, the two parameters, parameter values are taken from the subject may be iterative, this function must comply with the rules is greater than 1 is returned, it is less than -1, is equal to 0 is returned.
    key - is mainly used for the comparison element, only one parameter, the specific parameter is a function of iteration may be taken from the object, specify one of the elements in the iteration to be sorted.
    reverse - collation, reverse = True descending, reverse = False ascending (default).
>>> L=[('b',2),('a',1),('c',3),('d',4)]
>>> sorted(L, key=lambda x:x[1]) 

[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
  1. items ()
    Returns may traverse the (key, value) tuples array.
dict = {'Name': 'Runoob', 'Age': 7}

print ("Value : %s" %  dict.items())

Examples of the above output is:

Value : dict_items([('Age', 7), ('Name', 'Runoob')])
  1. operator.itemgetter(1)
op={'1':(1,0,6),'3':(0,45,8),'2':(2,34,10)}  
lp3=sorted(op.items(),key=operator.itemgetter(1),reverse=True)
print(lp3)

Output:

[('2', (2, 34, 10)), ('1', (1, 0, 6)), ('3', (0, 45, 8))]

Sorted in accordance with a first element of the array op size value-

  1. the readlines ()
    the readlines () automatically analyzing contents of the file list into a row, this list can be processed by a structure in ... for ... of the Python
  2. Strip ()
    s.strip (RM) by default remove white space characters (including '\ n', '\ r ', '\ t', '')
  3. numpy array
#创建矩阵
>>> m=mat([1,2,3])
>>> m
matrix([[1, 2, 3]])
#取值
>>> m[0]                #取一行
matrix([[1, 2, 3]])
>>> m[0,1]              #第0行,第1个数据
2
#索引取值
>>>m=mat([[2,5,1],[2,4,6]]) 
>>> m[1,:]                      #取得第一行的所有元素
matrix([[2, 4, 6]])
>>> m[1,0:1]                  #第一行第0个元素,注意左闭右开
matrix([[2]])
>>> m[1,0:3]
matrix([[2, 4, 6]])
>>> m[1,0:2]
matrix([[2, 4]])
>>> m[0:3]                      #前三个元素
matrix([[2, 5,1]])
Released nine original articles · won praise 0 · Views 258

Guess you like

Origin blog.csdn.net/powerful_ren/article/details/84589928