zoukankan      html  css  js  c++  java
  • numpy模块

    import numpy as np  #矩阵计算包
    In [2]:
    #1创建数组
    np.array([1,3,5])
    Out[2]:
    array([1, 3, 5])
    In [5]:
    np.array([[1,3,5],[2,7,9]])
    Out[5]:
    array([[1, 3, 5],
           [2, 7, 9]])
    In [6]:
    a=np.array([[1,3,5],[2,7,9]])
    type(a)
     Out[6]:
    numpy.ndarray
    In [7]:
    a.shape#显示有多少行多少列
    Out[7]:
    (2, 3)
    In [8]:
    #自动填充
    np.arange(1,10,2)#开始 结束 步长
    Out[8]:
    array([1, 3, 5, 7, 9])
    In [37]:
    np.linspace(0,10,100)#开始  结束  生成多少个
    Out[37]:
    array([ 0.        ,  0.1010101 ,  0.2020202 ,  0.3030303 ,  0.4040404 ,
            0.50505051,  0.60606061,  0.70707071,  0.80808081,  0.90909091,
            1.01010101,  1.11111111,  1.21212121,  1.31313131,  1.41414141,
            1.51515152,  1.61616162,  1.71717172,  1.81818182,  1.91919192,
            2.02020202,  2.12121212,  2.22222222,  2.32323232,  2.42424242,
            2.52525253,  2.62626263,  2.72727273,  2.82828283,  2.92929293,
            3.03030303,  3.13131313,  3.23232323,  3.33333333,  3.43434343,
            3.53535354,  3.63636364,  3.73737374,  3.83838384,  3.93939394,
            4.04040404,  4.14141414,  4.24242424,  4.34343434,  4.44444444,
            4.54545455,  4.64646465,  4.74747475,  4.84848485,  4.94949495,
            5.05050505,  5.15151515,  5.25252525,  5.35353535,  5.45454545,
            5.55555556,  5.65656566,  5.75757576,  5.85858586,  5.95959596,
            6.06060606,  6.16161616,  6.26262626,  6.36363636,  6.46464646,
            6.56565657,  6.66666667,  6.76767677,  6.86868687,  6.96969697,
            7.07070707,  7.17171717,  7.27272727,  7.37373737,  7.47474747,
            7.57575758,  7.67676768,  7.77777778,  7.87878788,  7.97979798,
            8.08080808,  8.18181818,  8.28282828,  8.38383838,  8.48484848,
            8.58585859,  8.68686869,  8.78787879,  8.88888889,  8.98989899,
            9.09090909,  9.19191919,  9.29292929,  9.39393939,  9.49494949,
            9.5959596 ,  9.6969697 ,  9.7979798 ,  9.8989899 , 10.        ])
    In [13]:
    a=np.linspace(0,10,100)
    a.reshape((20,5))#变形 20行 5 列

    Out[13]:

    array([[ 0.        ,  0.1010101 ,  0.2020202 ,  0.3030303 ,  0.4040404 ],
           [ 0.50505051,  0.60606061,  0.70707071,  0.80808081,  0.90909091],
           [ 1.01010101,  1.11111111,  1.21212121,  1.31313131,  1.41414141],
           [ 1.51515152,  1.61616162,  1.71717172,  1.81818182,  1.91919192],
           [ 2.02020202,  2.12121212,  2.22222222,  2.32323232,  2.42424242],
           [ 2.52525253,  2.62626263,  2.72727273,  2.82828283,  2.92929293],
           [ 3.03030303,  3.13131313,  3.23232323,  3.33333333,  3.43434343],
           [ 3.53535354,  3.63636364,  3.73737374,  3.83838384,  3.93939394],
           [ 4.04040404,  4.14141414,  4.24242424,  4.34343434,  4.44444444],
           [ 4.54545455,  4.64646465,  4.74747475,  4.84848485,  4.94949495],
           [ 5.05050505,  5.15151515,  5.25252525,  5.35353535,  5.45454545],
           [ 5.55555556,  5.65656566,  5.75757576,  5.85858586,  5.95959596],
           [ 6.06060606,  6.16161616,  6.26262626,  6.36363636,  6.46464646],
           [ 6.56565657,  6.66666667,  6.76767677,  6.86868687,  6.96969697],
           [ 7.07070707,  7.17171717,  7.27272727,  7.37373737,  7.47474747],
           [ 7.57575758,  7.67676768,  7.77777778,  7.87878788,  7.97979798],
           [ 8.08080808,  8.18181818,  8.28282828,  8.38383838,  8.48484848],
           [ 8.58585859,  8.68686869,  8.78787879,  8.88888889,  8.98989899],
           [ 9.09090909,  9.19191919,  9.29292929,  9.39393939,  9.49494949],
           [ 9.5959596 ,  9.6969697 ,  9.7979798 ,  9.8989899 , 10.        ]])
    In [16]:
    a.resize((20,5))#改变了值本身形态 -
    a
    Out[16]:
    array([[ 0.        ,  0.1010101 ,  0.2020202 ,  0.3030303 ,  0.4040404 ],
           [ 0.50505051,  0.60606061,  0.70707071,  0.80808081,  0.90909091],
           [ 1.01010101,  1.11111111,  1.21212121,  1.31313131,  1.41414141],
           [ 1.51515152,  1.61616162,  1.71717172,  1.81818182,  1.91919192],
           [ 2.02020202,  2.12121212,  2.22222222,  2.32323232,  2.42424242],
           [ 2.52525253,  2.62626263,  2.72727273,  2.82828283,  2.92929293],
           [ 3.03030303,  3.13131313,  3.23232323,  3.33333333,  3.43434343],
           [ 3.53535354,  3.63636364,  3.73737374,  3.83838384,  3.93939394],
           [ 4.04040404,  4.14141414,  4.24242424,  4.34343434,  4.44444444],
           [ 4.54545455,  4.64646465,  4.74747475,  4.84848485,  4.94949495],
           [ 5.05050505,  5.15151515,  5.25252525,  5.35353535,  5.45454545],
           [ 5.55555556,  5.65656566,  5.75757576,  5.85858586,  5.95959596],
           [ 6.06060606,  6.16161616,  6.26262626,  6.36363636,  6.46464646],
           [ 6.56565657,  6.66666667,  6.76767677,  6.86868687,  6.96969697],
           [ 7.07070707,  7.17171717,  7.27272727,  7.37373737,  7.47474747],
           [ 7.57575758,  7.67676768,  7.77777778,  7.87878788,  7.97979798],
           [ 8.08080808,  8.18181818,  8.28282828,  8.38383838,  8.48484848],
           [ 8.58585859,  8.68686869,  8.78787879,  8.88888889,  8.98989899],
           [ 9.09090909,  9.19191919,  9.29292929,  9.39393939,  9.49494949],
           [ 9.5959596 ,  9.6969697 ,  9.7979798 ,  9.8989899 , 10.        ]])
    In [18]:
    a.flatten()#对数组降维,数组本身不变
    Out[18]:
    array([ 0.        ,  0.1010101 ,  0.2020202 ,  0.3030303 ,  0.4040404 ,
            0.50505051,  0.60606061,  0.70707071,  0.80808081,  0.90909091,
            1.01010101,  1.11111111,  1.21212121,  1.31313131,  1.41414141,
            1.51515152,  1.61616162,  1.71717172,  1.81818182,  1.91919192,
            2.02020202,  2.12121212,  2.22222222,  2.32323232,  2.42424242,
            2.52525253,  2.62626263,  2.72727273,  2.82828283,  2.92929293,
            3.03030303,  3.13131313,  3.23232323,  3.33333333,  3.43434343,
            3.53535354,  3.63636364,  3.73737374,  3.83838384,  3.93939394,
            4.04040404,  4.14141414,  4.24242424,  4.34343434,  4.44444444,
            4.54545455,  4.64646465,  4.74747475,  4.84848485,  4.94949495,
            5.05050505,  5.15151515,  5.25252525,  5.35353535,  5.45454545,
            5.55555556,  5.65656566,  5.75757576,  5.85858586,  5.95959596,
            6.06060606,  6.16161616,  6.26262626,  6.36363636,  6.46464646,
            6.56565657,  6.66666667,  6.76767677,  6.86868687,  6.96969697,
            7.07070707,  7.17171717,  7.27272727,  7.37373737,  7.47474747,
            7.57575758,  7.67676768,  7.77777778,  7.87878788,  7.97979798,
            8.08080808,  8.18181818,  8.28282828,  8.38383838,  8.48484848,
            8.58585859,  8.68686869,  8.78787879,  8.88888889,  8.98989899,
            9.09090909,  9.19191919,  9.29292929,  9.39393939,  9.49494949,
            9.5959596 ,  9.6969697 ,  9.7979798 ,  9.8989899 , 10.        ])
    In [20]:
    a.ravel()#对数组降维并改变了数组
    Out[20]:
    array([ 0.        ,  0.1010101 ,  0.2020202 ,  0.3030303 ,  0.4040404 ,
            0.50505051,  0.60606061,  0.70707071,  0.80808081,  0.90909091,
            1.01010101,  1.11111111,  1.21212121,  1.31313131,  1.41414141,
            1.51515152,  1.61616162,  1.71717172,  1.81818182,  1.91919192,
            2.02020202,  2.12121212,  2.22222222,  2.32323232,  2.42424242,
            2.52525253,  2.62626263,  2.72727273,  2.82828283,  2.92929293,
            3.03030303,  3.13131313,  3.23232323,  3.33333333,  3.43434343,
            3.53535354,  3.63636364,  3.73737374,  3.83838384,  3.93939394,
            4.04040404,  4.14141414,  4.24242424,  4.34343434,  4.44444444,
            4.54545455,  4.64646465,  4.74747475,  4.84848485,  4.94949495,
            5.05050505,  5.15151515,  5.25252525,  5.35353535,  5.45454545,
            5.55555556,  5.65656566,  5.75757576,  5.85858586,  5.95959596,
            6.06060606,  6.16161616,  6.26262626,  6.36363636,  6.46464646,
            6.56565657,  6.66666667,  6.76767677,  6.86868687,  6.96969697,
            7.07070707,  7.17171717,  7.27272727,  7.37373737,  7.47474747,
            7.57575758,  7.67676768,  7.77777778,  7.87878788,  7.97979798,
            8.08080808,  8.18181818,  8.28282828,  8.38383838,  8.48484848,
            8.58585859,  8.68686869,  8.78787879,  8.88888889,  8.98989899,
            9.09090909,  9.19191919,  9.29292929,  9.39393939,  9.49494949,
            9.5959596 ,  9.6969697 ,  9.7979798 ,  9.8989899 , 10.        ])
    In [23]:
    ##创建多维数组并自动填充
    np.ones((2,5))#2行5列 用1填充
    Out[23]:
    array([[1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.]])
    In [24]:
    np.zeros((2,5))#用0填充
    Out[24]:
    array([[0., 0., 0., 0., 0.],
           [0., 0., 0., 0., 0.]])
    In [29]:
    np.empty((2,5))#随机数填充

    Out[29]:

    array([[0., 0., 0., 0., 0.],
           [0., 0., 0., 0., 0.]])
    In [33]:
    np.eye(3)#3*3矩阵
    Out[33]:
    array([[1., 0., 0.],
           [0., 1., 0.],
           [0., 0., 1.]])
    In [35]:
    a=np.eye(3)
    np.diag(a,1)#对角线矩阵
    Out[35]:
    array([0., 0.])
    In [36]:
    np.full((3,3),8)#全部为8的三乘以3矩阵
    Out[36]:
    array([[8, 8, 8],
           [8, 8, 8],
           [8, 8, 8]])
    In [38]:
    np.random.rand(2,5)#o-1 之间均匀分布

    Out[38]:

    array([[0.63777893, 0.61140518, 0.31321926, 0.99111662, 0.77213003],
           [0.66032703, 0.26635147, 0.51230109, 0.00825155, 0.80438856]])
    In [39]:
    np.random.randn(2,5)#标准正太分布
    Out[39]:
    array([[-0.85813291, -0.51808816,  0.84557228,  0.25999279, -0.26334869],
           [ 0.80162927,  0.52697261, -0.55807018,  1.15921478, -1.75240519]])
    In [41]:
    np.random.seed(0)#随机数种子
    np.random.rand(2,5)

    Out[41]:

    array([[0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ],
           [0.64589411, 0.43758721, 0.891773  , 0.96366276, 0.38344152]])
    In [65]:
    a=np.arange(10).reshape(5,2)
     In [66]:
    np.random.shuffle(a)#对一个序列进行随机排序
    Out[66]:
    array([[6, 7],
           [2, 3],
           [8, 9],
           [4, 5],
           [0, 1]])
    In [67]:
    ##切割与组合
    a=[1,2,3]
    b=[4,5,6]
    np.hstack((a,b))#横向组合
    Out[67]:
    array([1, 2, 3, 4, 5, 6])
    In [68]:
    np.vstack((a,b))#纵向组合
    Out[68]:
    array([[1, 2, 3],
           [4, 5, 6]])
    In [71]:
    import numpy.linalg as nl#矩阵计算包
    In [72]:
    a=np.full((3,3),8)
    a
    Out[72]:
    array([[8, 8, 8],
           [8, 8, 8],
           [8, 8, 8]])
    In [73]:
    nl.det(a)#计算行列式
    Out[73]:
    0.0
    In [74]:
    nl.eig(a)#计算特征值与特征向量
    Out[74]:
    (array([-1.77635684e-15,  2.40000000e+01,  0.00000000e+00]),
     array([[-0.81649658,  0.57735027,  0.        ],
            [ 0.40824829,  0.57735027, -0.70710678],
            [ 0.40824829,  0.57735027,  0.70710678]]))
    In [76]:
    nl.qr(a)#计算qr分解
    Out[76]:
    (array([[-0.57735027, -0.57735027, -0.57735027],
            [-0.57735027,  0.78867513, -0.21132487],
            [-0.57735027, -0.21132487,  0.78867513]]),
     array([[-13.85640646, -13.85640646, -13.85640646],
            [  0.        ,   0.        ,   0.        ],
            [  0.        ,   0.        ,   0.        ]]))
    In [77]:
    nl.svd(a)#计算奇异值分解
    Out[77]:
    (array([[-0.57735027, -0.57735027, -0.57735027],
            [-0.57735027, -0.21132487,  0.78867513],
            [-0.57735027,  0.78867513, -0.21132487]]),
     array([24.,  0.,  0.]),
     array([[-0.57735027, -0.57735027, -0.57735027],
            [ 0.        , -0.70710678,  0.70710678],
            [ 0.81649658, -0.40824829, -0.40824829]]))
     
     
  • 相关阅读:
    【计蒜客】贝壳找房户外拓展(中等)扫描线+线段树
    【CodeForces】925 C.Big Secret 异或
    【LibreOJ】#6392. 「THUPC2018」密码学第三次小作业 / Rsa 扩展欧几里得算法
    【LibreOJ】#6395. 「THUPC2018」城市地铁规划 / City 背包DP+Prufer序
    【BZOJ】1095: [ZJOI2007]Hide 捉迷藏 括号序列+线段树
    【BZOJ】2111: [ZJOI2010]Perm 排列计数 计数DP+排列组合+lucas
    【计蒜客】百度科学家(困难)
    【BZOJ】2115: [Wc2011] Xor
    【GDOI2018】所有题目和解题报告
    【BZOJ】2434: [Noi2011]阿狸的打字机 AC自动机+树状数组+DFS序
  • 原文地址:https://www.cnblogs.com/Koi504330/p/11908887.html
Copyright © 2011-2022 走看看