numpy learn three (This article content from numpy Chinese document)

Pass function

NumPy a familiar mathematical functions such as sin, cos, and exp. In NumPy, these are called "pass function" ufunc( ). In NumPy, these functions are performed by the operation on the array elements, an array generated as output.

>>> B = np.arange(3)
>>> B
array([0, 1, 2])
>>> np.exp(B)
array([ 1.        ,  2.71828183,  7.3890561 ])
>>> np.sqrt(B)
array([ 0.        ,  1.        ,  1.41421356])
>>> C = np.array([2., -1., 4.])
>>> np.add(B, C)
array([ 2.,  0.,  6.])

Sort: axis: when the reference array is sorted, axis = 0, column ordered; axis = 1, sorted by row. kind: The method used for sort the array, wherein: kind = 'quicksort' faster discharge; 'mergesort' of shuffling; 'heapsort' of stack discharge. order: a string or list, you can set a property to sort. Import numpy AS NP

>>> list1 = [[4,3,2],[2,1,4]]
>>> array=np.array(list1) 
>>> array
array([[4, 3, 2],
       [2, 1, 4]])
>>> array.sort(axis=1)   
>>> array
array([[2, 3, 4],
       [ . 1, 2, 4 ]])
 # Axis =. 1, described is sorted by row, i.e., elements on each line to achieve incremental, 
# [4, 3, 2] to a [2, 3, 4], [2, 1, 4] to the [1, 2, 4] 
>>> Array.sort (Axis = 0)
 >>>> Array
array([[1, 2, 4],
       [ 2,. 3,. 4 ]])
 # Axis = 0, instructions are sorted by column, that is, each element in the column to achieve the increment 
# [2, 1] to a [1, 2], [ 3, 2] to a [2, 3] 
>>> np.sort (Array, Axis = None)
Array ([ . 1, 2, 2,. 3,. 4,. 4 ])
 # when axis = None, all elements uniform sort 
Import numpy AS NP >>> DTYPE = [( ' the Name ' , ' SlO ' ), ( ' the Height ' , a float), ( ' Age ' , int)] >>> values = [( ' of Li ' , 1.8, 41 is), ( ' Wang ' , 1.9, 38 is), ( ' Duan ' , 1.7, 38 is )] > A = np.array >> (values, DTYPE = DTYPE) >>> np.sort (A, Order = 'Height') # Accordance ordering property Height, this time parameter is a string Array ([( ' Duan ' , 1.7, 38 is), ( ' of Li ' , 1.8, 41 is), ( ' Wang ' , 1.9, 38 is )], DTYPE = [( ' the Name ' , ' | SlO ' ), ( ' the Height ' , ' <F8 ' ), ( ' Age ' , ' <I4 ' )]) >>> np.sort (A, Order = [ ' Age ' , ' Height ' ]) # first sorting by attributes Age, Age if equal, then sorted according Height, then the list parameter Array ([( ' Duan ' , 1.7, 38 is), ( ' Wang ' , 1.9, 38 is ), ( '', 1.8, 41)], dtype=[('Name', '|S10'), ('Height', '<f8'), ('Age', '<i4')])
A = >>> [1,5,1,4,3,4,4 ]
 >>> B = [9,4,0,4,0,2,1 ]
 >>> np.lexsort ((B, a))
 # b front, in a rear, that is, according to the first comparing element a 
# as a minimum of two is 1, which are indexed 0,2, then regardless of the value of the corresponding index b , i.e. 9,0 
# minimum should correspond are: 0, and its corresponding index is 2, the sorted results returned first index value 2 
# at a minimum should be: 1,9, and its the corresponding index is zero, the result returns a value of the sorted index 0 
# and so on ... 
Array ([2, 0,. 4,. 6,. 5,. 3,. 1], DTYPE = Int64)
 >> > np.lexsort ((a, b))
 # a front, b after that is to be compared according to the element b 
# as the minimum value of b is 0 to two, which are indexes 0, 4, and then regardless of a value of the corresponding index, i.e. 1,3 
# minimum should correspond are: 0, 1, and its corresponding index is 2, the sorted results returned first index value 2 
# at a minimum should They are: 0,3, and its corresponding index is 4, the result returns a value of the sorted index 4 
# and so on ...
array([2, 4, 6, 5, 3, 1, 0], dtype=int64)
>>> c=[[1,5,1,4,3,4,4],[9,4,0,4,0,2,1]]
>>> c
[[ . 1,. 5,. 1,. 4,. 3,. 4,. 4], [. 9,. 4, 0,. 4, 0, 2,. 1 ]]
 >>> np.lexsort (C)
 # this case after the first b where a consistent 
Array ([2,. 4,. 6,. 5,. 3,. 1, 0], DTYPE = Int64)
List3 = >>> [1,2,3,4,5 ]
 >>> np.searchsorted (list3,2 )
 1
 # as list3 To insert elements 2, it should be inserted in place of the original list of index 1 that is inserted after the element 1 
>>> np.searchsorted (list3, [- 5,7,4,9 ])
Array ([0, . 5,. 3,. 5], DTYPE = Int64)
 # as to insert the element -5 in list3, then it should be inserted in the original list index where 0, i.e., is inserted in front of the element 1 
# Other and so on ...
>>>list=[3,4,5,2,1]
>>>np.partition(list,3)
Array ([ 2,. 1, 3,. 4,. 5 ])
 # In the third number after sorting, i.e. 3 to partition, i.e. the partition is the result: 
# less than 2,1 in front of the element 3 is 3, is greater than element 3 is equal to 3, 4, 5 behind
sorted(iterable[, cmp[, key[, reverse]]])
sorted () function sort operation on all objects iterations.
    sort and sorted differences:
    The method is applied on the sort of the list, sorted can be sorted objects operate all iterations.
    list sort method returns a list of the existing operation, built-in functions and the method returns a sorted
    The new list, rather than operations carried out in the original basis.
# The sorted () with the parameter can reverse the order reverse = True 
>>> List = [3,4,2,6,1 ]
 >>> the sorted (List)
[1, 2, 3, 4, 6]
>>>sorted(list, reverse=True)
[6, 4, 3, 2, 1]

This article from the original code section link: https: //blog.csdn.net/haiyang_duan/article/details/79221458

Guess you like

Origin www.cnblogs.com/tsy-0209/p/12453138.html