zoukankan      html  css  js  c++  java
  • 一、numpy入门

    Array

    import numpy as np
    # create from python list
    list_1 = [1, 2, 3, 4]
    array_1 = np.array(list_1)#array([1, 2, 3, 4])
    list_2 = [5, 6, 7, 8]
    array_2 = np.array([list_1, list_2])
    

      结果:

    array([[1, 2, 3, 4],
           [5, 6, 7, 8]])
    View Code
    array_2.shape#(2, 4)
    array_2.size#8
    array_2.dtype#dtype('int64')
    array_3 = np.array([[1.0,2,3],[4.0,5,6]])
    array_3.dtype#dtype('float64')
    
    array_4 = np.arange(1, 10, 2)#array([1, 3, 5, 7, 9])
    np.zeros(5)#array([ 0.,  0.,  0.,  0.,  0.])
    

      

    np.zeros([2,3])
    

      结果:

    array([[ 0.,  0.,  0.],
           [ 0.,  0.,  0.]])
    View Code
    np.eye(5)
    

      结果:

    array([[ 1.,  0.,  0.,  0.,  0.],
           [ 0.,  1.,  0.,  0.,  0.],
           [ 0.,  0.,  1.,  0.,  0.],
           [ 0.,  0.,  0.,  1.,  0.],
           [ 0.,  0.,  0.,  0.,  1.]])
    View Code
    np.eye(5).dtype#dtype('float64')
    

      

    a = np.arange(1,10)#array([1, 2, 3, 4, 5, 6, 7, 8, 9])
    a[1]#2
    a[1:5]#array([2, 3, 4, 5])
    

      

    b = np.array([[1,2,3],[4,5,6]])
    b
    

      结果:

    array([[1, 2, 3],
           [4, 5, 6]])
    View Code
    b[1][0]#4
    b[1,0]#4
    

      

    c = np.array([[1,2,3],[4,5,6],[7,8,9]])
    c
    

      结果:

    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])
    View Code
    c[:2,1:]
    

      结果:

    array([[2, 3],
           [5, 6]])
    View Code

    数组与矩阵运算

    快速创建数组

    import numpy as np
    np.random.randn(10)
    

      结果:

    array([ 0.28906593,  1.4302902 ,  1.10346334,  0.11146373, -0.47497452,
            0.88859371,  0.18953089, -0.65780036, -2.06789973, -1.45679231])
    View Code
    np.random.randint(10, size=20).reshape(4, 5)
    

      结果:

    array([[8, 5, 8, 4, 5],
           [4, 5, 2, 8, 3],
           [3, 6, 9, 7, 3],
           [3, 0, 4, 7, 0]])
    View Code

    数组运算

    a = np.random.randint(10, size=20).reshape(4, 5)
    b = np.random.randint(10, size=20).reshape(4, 5)
    a
    b
    

      结果:

    array([[3, 6, 5, 9, 3],
           [3, 3, 4, 6, 8],
           [3, 3, 2, 3, 4],
           [6, 0, 9, 7, 9]])
    
    array([[1, 5, 6, 0, 6],
           [0, 2, 1, 7, 9],
           [7, 6, 1, 3, 8],
           [4, 4, 3, 1, 0]])
    View Code
    a + b
    

      结果:

    array([[ 4, 11, 11,  9,  9],
           [ 3,  5,  5, 13, 17],
           [10,  9,  3,  6, 12],
           [10,  4, 12,  8,  9]])
    View Code
    a-b
    

      结果:

    array([[ 2,  1, -1,  9, -3],
           [ 3,  1,  3, -1, -1],
           [-4, -3,  1,  0, -4],
           [ 2, -4,  6,  6,  9]])
    View Code
    a * b
    

      结果:

    array([[ 3, 30, 30,  0, 18],
           [ 0,  6,  4, 42, 72],
           [21, 18,  2,  9, 32],
           [24,  0, 27,  7,  0]])
    View Code
    a / b
    

      结果:

     RuntimeWarning: divide by zero encountered in true_divide
      """Entry point for launching an IPython kernel.
    
    array([[ 3.        ,  1.2       ,  0.83333333,         inf,  0.5       ],
           [        inf,  1.5       ,  4.        ,  0.85714286,  0.88888889],
           [ 0.42857143,  0.5       ,  2.        ,  1.        ,  0.5       ],
           [ 1.5       ,  0.        ,  3.        ,  7.        ,         inf]])
    View Code
    np.mat([[1,2,3],[4,5,6]])
    

      结果:

    matrix([[1, 2, 3],
            [4, 5, 6]])
    View Code
    a
    

      结果:

    array([[3, 6, 5, 9, 3],
           [3, 3, 4, 6, 8],
           [3, 3, 2, 3, 4],
           [6, 0, 9, 7, 9]])
    View Code
    np.mat(a)
    

      结果:

    matrix([[3, 6, 5, 9, 3],
            [3, 3, 4, 6, 8],
            [3, 3, 2, 3, 4],
            [6, 0, 9, 7, 9]])
    View Code

    矩阵的运算

    A = np.mat(a)
    B = np.mat(b)
    A
    B
    

      结果:

    matrix([[3, 6, 5, 9, 3],
            [3, 3, 4, 6, 8],
            [3, 3, 2, 3, 4],
            [6, 0, 9, 7, 9]])
    
    matrix([[1, 5, 6, 0, 6],
            [0, 2, 1, 7, 9],
            [7, 6, 1, 3, 8],
            [4, 4, 3, 1, 0]])
    View Code
    A + B
    A - B
    

      结果:

    matrix([[ 4, 11, 11,  9,  9],
            [ 3,  5,  5, 13, 17],
            [10,  9,  3,  6, 12],
            [10,  4, 12,  8,  9]])
    
    matrix([[ 2,  1, -1,  9, -3],
            [ 3,  1,  3, -1, -1],
            [-4, -3,  1,  0, -4],
            [ 2, -4,  6,  6,  9]])
    View Code
    A * B
    

      结果: ValueError: shapes (4,5) and (4,5) not aligned: 5 (dim 1) != 4 (dim 0)

    a = np.mat(np.random.randint(10, size=20).reshape(4, 5))
    b = np.mat(np.random.randint(10, size=20).reshape(5, 4))
    a
    b
    a * b
    

      结果:

    matrix([[4, 4, 3, 2, 7],
            [4, 7, 2, 4, 5],
            [8, 6, 6, 1, 0],
            [5, 9, 6, 2, 8]])
    
    matrix([[5, 8, 9, 2],
            [8, 4, 3, 7],
            [4, 6, 7, 0],
            [5, 8, 5, 3],
            [0, 6, 9, 5]])
    
    matrix([[ 74, 124, 142,  77],
            [104, 134, 136,  94],
            [117, 132, 137,  61],
            [131, 176, 196, 119]])
    View Code

    Array常用函数

    a = np.random.randint(10, size=20).reshape(4, 5)
    np.unique(a)#array([0, 1, 2, 3, 4, 5, 6, 8, 9])
    a
    

      结果:

    array([[4, 1, 2, 5, 3],
           [9, 8, 1, 4, 0],
           [5, 4, 8, 0, 2],
           [8, 6, 2, 4, 3]])
    View Code
    sum(a)#array([26, 19, 13, 13,  8])
    sum(a[0])#15
    sum(a[:,0])#26
    a.max()#9
    max(a[0])#5
    max(a[:,0])#9
    b = np.random.randint(5,10, size=20).reshape(4, 5)
    np.unique(b)#array([5, 6, 7, 8, 9])
    b
    

      结果:

    array([[6, 8, 7, 7, 9],
           [5, 5, 5, 8, 5],
           [6, 9, 6, 8, 6],
           [6, 5, 8, 5, 9]])
    View Code

    Array的input和output

    使用pickle序列化numpy array

    import pickle
    import numpy as np
    x = np.arange(10)#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    f = open('x.pkl', 'wb')
    pickle.dump(x, f)
    

      

    f = open('x.pkl', 'rb')
    pickle.load(f)#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

      

    np.save('one_array', x)
    

      

    np.load('one_array.npy')#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    

      

    y = np.arange(20)#array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19])
    np.savez('two_array.npz', a=x, b=y)
    

      

    c = np.load('two_array.npz')
    c['a']#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    c['b']#array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19])
    

      

     

     

  • 相关阅读:
    在linux系统中
    记录一次编译安装Pg_rman缺少依赖包的问题
    使用Patroni和HAProxy创建高可用的PostgreSQL集群
    Zabbix4.0国内下载源
    centos7部署postgresql集群高可用 patroni + etcd 之patroni篇
    centos7部署etcd集群
    docker-compose部署gitlab
    zabbix-agent主动模式和proxy
    docker-compose更新image命令
    shell脚本自动化安装pgsql10.5版本
  • 原文地址:https://www.cnblogs.com/tianqizhi/p/8878749.html
Copyright © 2011-2022 走看看