深度学习编程

    科技2022-08-07  143

    1.np.flatnonzero()函数 2. np.random.choice() 3. a.shape 函数 返回大小,行-列排布。

    # list没有shape这个属性 >>> a=np.array([[0,1,4],[2,3,8]]) >>> a.shape (2, 3) >>> a.shape[1] 3 >>> a.shape[0] 2

    4、a.reshape() 5、np.argsort() 6、python 中的统计函数

    >>> from collections import Counter >>> vote=Counter([1,1,2,2,2,3,6,7,7,7]) >>> vote Counter({2: 3, 7: 3, 1: 2, 3: 1, 6: 1}) >>> vote.most_common() [(2, 3), (7, 3), (1, 2), (3, 1), (6, 1)]

    7、np.split()

    >>> a=np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.split(a,5) [array([0, 1]), array([2, 3]), array([4, 5]), array([6, 7]), array([8, 9])] >>> np.split(a,10) [array([0]), array([1]), array([2]), array([3]), array([4]), array([5]), array([6]), array([7]), array([8]), array([9])] >>> np.split(a,3) Traceback (most recent call last): File "E:\python\lib\site-packages\numpy\lib\shape_base.py", line 843, in split len(indices_or_sections) TypeError: object of type 'int' has no len() During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> np.split(a,3) File "E:\python\lib\site-packages\numpy\lib\shape_base.py", line 849, in split 'array split does not result in an equal division') ValueError: array split does not result in an equal division

    8、np.concatenate 9、enumerate

    k_choices = [1, 3, 5, 8, 10, 12, 15, 20, 50, 100] >>> for ik,k in enumerate(k_choices): print(ik,k) 0 1 1 3 2 5 3 8 4 10 5 12 6 15 7 20 8 50 9 100 >>> for ik in enumerate(k_choices): print(ik) (0, 1) (1, 3) (2, 5) (3, 8) (4, 10) (5, 12) (6, 15) (7, 20) (8, 50) (9, 100)

    10、lambda表达式 11、random:randrange 从中不大于这个数的大小里随机选择一个数。 注意:默认基准值为1

    >>> from random import randrange >>> ix = tuple([randrange(m) for m in x.shape]) >>> ix (1, 0) >>> ix = tuple([randrange(m) for m in x.shape]) >>> ix (1, 1) >>> ix = tuple([randrange(m) for m in x.shape]) >>> ix (0, 2) >>> x.shape (2, 3) >>> [m for m in x.shape] [2, 3]

    12、numpy 索引的一种方式

    1>>>x=np.array([[1,2,3],[5,6,2]]) >>> x[(1,2)] 2 >>> x[[1,2]] Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> x[[1,2]] IndexError: index 2 is out of bounds for axis 0 with size 2 2>>> a=np.array([[1,2,3],[4,5,6]]) >>> a[range(2),1] array([2, 5]) >>> a array([[1, 2, 3], [4, 5, 6]]) >>> a[1,range(2)] array([4, 5]) >>> range(2) range(0, 2) >>> a[1,list(range(2))] array([4, 5]) >>> list(range(2)) [0, 1]
    Processed: 0.009, SQL: 8