美文网首页我爱编程
练手与防遗忘系列-numpy

练手与防遗忘系列-numpy

作者: BlueCat2016 | 来源:发表于2018-06-14 14:24 被阅读0次

    越来越发现,numpy,pandas,scipy,matplotlib这类库的各种函数就像背英语单词一样,一直无法摆脱“背了忘、忘了背、背了再忘”的怪圈。为了使以后再使用的时候能够得心应手,不是那么费劲,特地记录一下练手的代码片段,以便以后可查。

    变形与统计

    # coding=utf8
    
    import numpy as np
    
    arr = np.array([1, 3, 8, 2, 4, 7])
    print(arr)
    print(arr.reshape([1, 6]))
    print(arr.reshape([3, 2]))
    print(arr.reshape([6, 1]))
    print('-----------------------------------')
    x = np.array([1, 2, 3])
    print(x)
    x1 = x.reshape([-1, 1])
    print(x1)
    
    x2 = x.reshape([1, -1])
    print(x2)
    print('-----------------------------------')
    y = np.array([[1, 2, 3], [4, 5, 6]])
    # 0维上的最小值 滑动0轴
    print(y.min(axis=0))
    # 1维上的最小值
    print(y.min(axis=1))
    print(y.mean(axis=1))
    print(y.sum(axis=1))
    print(y.prod(axis=1))
    print(y.cumsum(axis=1))
    

    运行结果:

    [1 3 8 2 4 7]
    [[1 3 8 2 4 7]]
    [[1 3]
     [8 2]
     [4 7]]
    [[1]
     [3]
     [8]
     [2]
     [4]
     [7]]
    -----------------------------------
    [1 2 3]
    [[1]
     [2]
     [3]]
    [[1 2 3]]
    -----------------------------------
    [1 2 3]
    [1 4]
    [2. 5.]
    [ 6 15]
    [  6 120]
    [[ 1  3  6]
     [ 4  9 15]]
    

    矩阵运算

    # coding=utf8
    # 矩阵运算
    
    import numpy as np
    
    arr = np.array([[1, 3, 8], [2, 4, 7]])
    new = np.mat([[4, 5, 8], [3, 6, 10]])
    print(arr + new)
    print(arr - new)
    print(arr / new)
    print('-----------------------------------')
    print(arr + 2)
    print(arr - 4)
    print(arr / 3)
    print('-----------------------------------')
    ###################ndarray数乘和点乘#####################
    arr2 = arr * 2
    print(arr2)
    print(type(arr))
    print(type(arr2))
    print(arr * arr2)
    # 数量乘法
    print(np.multiply(arr, arr2))
    print(arr.dot(arr2.T))
    print('-----------------------------------')
    ###################matrix数乘和点乘#####################
    mat2 = new * 2
    print(mat2)
    # 数乘
    print(np.multiply(new, mat2))
    print(new * mat2.transpose())
    print(new.dot(mat2.T))
    print('----------------特殊矩阵-------------------')
    print(np.zeros((2, 3)))
    print(np.ones((2, 3)))
    print(np.eye(3))
    print(np.eye(3, 2))
    print(np.identity(3))
    

    运行结果:

    [[ 5  8 16]
     [ 5 10 17]]
    [[-3 -2  0]
     [-1 -2 -3]]
    [[0.25       0.6        1.        ]
     [0.66666667 0.66666667 0.7       ]]
    -----------------------------------
    [[ 3  5 10]
     [ 4  6  9]]
    [[-3 -1  4]
     [-2  0  3]]
    [[0.33333333 1.         2.66666667]
     [0.66666667 1.33333333 2.33333333]]
    -----------------------------------
    [[ 2  6 16]
     [ 4  8 14]]
    <class 'numpy.ndarray'>
    <class 'numpy.ndarray'>
    [[  2  18 128]
     [  8  32  98]]
    [[  2  18 128]
     [  8  32  98]]
    [[148 140]
     [140 138]]
    -----------------------------------
    [[ 8 10 16]
     [ 6 12 20]]
    [[ 32  50 128]
     [ 18  72 200]]
    [[210 244]
     [244 290]]
    [[210 244]
     [244 290]]
    ----------------特殊矩阵-------------------
    [[0. 0. 0.]
     [0. 0. 0.]]
    [[1. 1. 1.]
     [1. 1. 1.]]
    [[1. 0. 0.]
     [0. 1. 0.]
     [0. 0. 1.]]
    [[1. 0.]
     [0. 1.]
     [0. 0.]]
    [[1. 0. 0.]
     [0. 1. 0.]
     [0. 0. 1.]]
    

    实用方法

    # coding=utf8
    # 实用方法
    
    import numpy as np
    import random
    
    print(np.linspace(1, 9, 6))
    data = np.random.rand(5)
    print(data)
    hist, bin_edges = np.histogram(data, [-1, 0, 0.3, 4])
    print(hist, bin_edges)
    print('-----------------------------------')
    data = sorted(random.sample(range(100), 6))
    print(data)
    # 打印当前值应该插入的索引的位置
    print(np.searchsorted(data, 8))
    print(np.searchsorted(data, 25))
    print('-----------------------------------')
    # 随机生成具有正态分布的数组
    print(np.random.rand(2, 3))
    print('-----------------------------------')
    a = np.ones((2, 2))
    b = np.eye(2)
    print(np.vstack((a, b)))
    print(np.hstack((a, b)))
    

    运行结果:

    [1.  2.6 4.2 5.8 7.4 9. ]
    [0.2564467  0.21826916 0.82746982 0.93122359 0.44223496]
    [0 2 3] [-1.   0.   0.3  4. ]
    -----------------------------------
    [21, 30, 46, 50, 74, 83]
    0
    1
    -----------------------------------
    [[0.07296967 0.92940202 0.73958934]
     [0.25950902 0.950619   0.2525271 ]]
    -----------------------------------
    [[1. 1.]
     [1. 1.]
     [1. 0.]
     [0. 1.]]
    [[1. 1. 1. 0.]
     [1. 1. 0. 1.]]
    

    相关文章

      网友评论

        本文标题:练手与防遗忘系列-numpy

        本文链接:https://www.haomeiwen.com/subject/mylyeftx.html