zoukankan      html  css  js  c++  java
  • numpy基操

    import numpy as np

    In [8]:
      a = np.arange(15).reshape(3,5) 
    In [10]:
      a
    Out[10]:
      array([[ 0, 1, 2, 3, 4],
      [ 5, 6, 7, 8, 9],
      [10, 11, 12, 13, 14]])
    In [11]:
      a.shape
    Out[11]:
      (3, 5)
    In [12]:
        a.dtype.name
    Out[12]:
      'int32'
    In [16]:
     np.empty((2,3))
    Out[16]:
    array([[0., 0., 0.],
           [0., 0., 0.]])
    In [17]:
     np.arange(10,30,5) # 10-30 步长为5
    Out[17]:
    array([10, 15, 20, 25])
    In [18]:
     np.arange(0,2,0.3) #支持浮点数
    Out[18]:
    array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
    In [19]:
     np.linspace(0,2,9) # 生成 0 到 2 的 9 个数,
    Out[19]:
    array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.25, 1.5 , 1.75, 2.  ])
    In [21]:
    from numpy import pi
    In [27]:
    x = np.linspace(0,2*pi,100) # 在有多个小数位时的使用
    x
    Out[27]:
    array([0.        , 0.06346652, 0.12693304, 0.19039955, 0.25386607,
           0.31733259, 0.38079911, 0.44426563, 0.50773215, 0.57119866,
           0.63466518, 0.6981317 , 0.76159822, 0.82506474, 0.88853126,
           0.95199777, 1.01546429, 1.07893081, 1.14239733, 1.20586385,
           1.26933037, 1.33279688, 1.3962634 , 1.45972992, 1.52319644,
           1.58666296, 1.65012947, 1.71359599, 1.77706251, 1.84052903,
           1.90399555, 1.96746207, 2.03092858, 2.0943951 , 2.15786162,
           2.22132814, 2.28479466, 2.34826118, 2.41172769, 2.47519421,
           2.53866073, 2.60212725, 2.66559377, 2.72906028, 2.7925268 ,
           2.85599332, 2.91945984, 2.98292636, 3.04639288, 3.10985939,
           3.17332591, 3.23679243, 3.30025895, 3.36372547, 3.42719199,
           3.4906585 , 3.55412502, 3.61759154, 3.68105806, 3.74452458,
           3.8079911 , 3.87145761, 3.93492413, 3.99839065, 4.06185717,
           4.12532369, 4.1887902 , 4.25225672, 4.31572324, 4.37918976,
           4.44265628, 4.5061228 , 4.56958931, 4.63305583, 4.69652235,
           4.75998887, 4.82345539, 4.88692191, 4.95038842, 5.01385494,
           5.07732146, 5.14078798, 5.2042545 , 5.26772102, 5.33118753,
           5.39465405, 5.45812057, 5.52158709, 5.58505361, 5.64852012,
           5.71198664, 5.77545316, 5.83891968, 5.9023862 , 5.96585272,
           6.02931923, 6.09278575, 6.15625227, 6.21971879, 6.28318531])
    In [28]:
    f = np.sin(x)
    f
    Out[28]:
    array([ 0.00000000e+00,  6.34239197e-02,  1.26592454e-01,  1.89251244e-01,
            2.51147987e-01,  3.12033446e-01,  3.71662456e-01,  4.29794912e-01,
            4.86196736e-01,  5.40640817e-01,  5.92907929e-01,  6.42787610e-01,
            6.90079011e-01,  7.34591709e-01,  7.76146464e-01,  8.14575952e-01,
            8.49725430e-01,  8.81453363e-01,  9.09631995e-01,  9.34147860e-01,
            9.54902241e-01,  9.71811568e-01,  9.84807753e-01,  9.93838464e-01,
            9.98867339e-01,  9.99874128e-01,  9.96854776e-01,  9.89821442e-01,
            9.78802446e-01,  9.63842159e-01,  9.45000819e-01,  9.22354294e-01,
            8.95993774e-01,  8.66025404e-01,  8.32569855e-01,  7.95761841e-01,
            7.55749574e-01,  7.12694171e-01,  6.66769001e-01,  6.18158986e-01,
            5.67059864e-01,  5.13677392e-01,  4.58226522e-01,  4.00930535e-01,
            3.42020143e-01,  2.81732557e-01,  2.20310533e-01,  1.58001396e-01,
            9.50560433e-02,  3.17279335e-02, -3.17279335e-02, -9.50560433e-02,
           -1.58001396e-01, -2.20310533e-01, -2.81732557e-01, -3.42020143e-01,
           -4.00930535e-01, -4.58226522e-01, -5.13677392e-01, -5.67059864e-01,
           -6.18158986e-01, -6.66769001e-01, -7.12694171e-01, -7.55749574e-01,
           -7.95761841e-01, -8.32569855e-01, -8.66025404e-01, -8.95993774e-01,
           -9.22354294e-01, -9.45000819e-01, -9.63842159e-01, -9.78802446e-01,
           -9.89821442e-01, -9.96854776e-01, -9.99874128e-01, -9.98867339e-01,
           -9.93838464e-01, -9.84807753e-01, -9.71811568e-01, -9.54902241e-01,
           -9.34147860e-01, -9.09631995e-01, -8.81453363e-01, -8.49725430e-01,
           -8.14575952e-01, -7.76146464e-01, -7.34591709e-01, -6.90079011e-01,
           -6.42787610e-01, -5.92907929e-01, -5.40640817e-01, -4.86196736e-01,
           -4.29794912e-01, -3.71662456e-01, -3.12033446e-01, -2.51147987e-01,
           -1.89251244e-01, -1.26592454e-01, -6.34239197e-02, -2.44929360e-16])
    In [31]:
    a = np.array([20,30,40,50])
    b = np.arange(4)
    c = a-b
    c
    Out[31]:
    array([20, 29, 38, 47])
    In [32]:
    b**2
    Out[32]:
    array([0, 1, 4, 9], dtype=int32)
    In [33]:
    10*np.sin(a)
    Out[33]:
    array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])
    In [34]:
    a<35
    Out[34]:
    array([ True,  True, False, False])
    In [35]:
    A = np.array([[1,1],[0,1]])
    B = np.array([[2,0],[3,4]])
    A*B
    Out[35]:
    array([[2, 0],
           [0, 4]])
    In [36]:
    A.dot(B)
    Out[36]:
    array([[5, 4],
           [3, 4]])
    In [37]:
    np.dot(A,B)
    Out[37]:
    array([[5, 4],
           [3, 4]])
    In [39]:
    a = np.ones((2,3),dtype = int)
    b = np.random.random((2,3))
    a *= 3
    a
    Out[39]:
    array([[3, 3, 3],
           [3, 3, 3]])
    In [40]:
    b += a
    b
    Out[40]:
    array([[3.17811736, 3.01278175, 3.89765992],
           [3.63055786, 3.48837569, 3.42782035]])
    In [41]:
    a += b  #TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int32') with casting rule 'same_kind'
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-41-294cacd62d6f> in <module>
    ----> 1 a += b
    
    TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int32') with casting rule 'same_kind'
    
    In [42]:
    a = np.ones(3,dtype=np.int32)
    b = np.linspace(0,pi,3)
    b.dtype.name
    Out[42]:
    'float64'
    In [44]:
    c = a+b
    c
    Out[44]:
    array([1.        , 2.57079633, 4.14159265])
    In [45]:
    a = np.random.random((2,3))
    a
    Out[45]:
    array([[0.23300036, 0.61133323, 0.7632916 ],
           [0.5514208 , 0.67509734, 0.55061737]])
    In [46]:
    a.sum()
    Out[46]:
    3.384760707418866
    In [47]:
    a.min()
    Out[47]:
    0.23300036373497113
    In [48]:
    b = np.arange(12).reshape(3,4)
    b
    Out[48]:
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    In [49]:
    b.sum(axis=0)
    Out[49]:
    array([12, 15, 18, 21])
    In [50]:
    b.min(axis=1)
    Out[50]:
    array([0, 4, 8])
    In [51]:
    b.cumsum(axis=1)
    Out[51]:
    array([[ 0,  1,  3,  6],
           [ 4,  9, 15, 22],
           [ 8, 17, 27, 38]], dtype=int32)
    In [52]:
    a = np.arange(10)**3
    a
    Out[52]:
    array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729], dtype=int32)
    In [54]:
    a[:6:2] = -1000
    a
    Out[54]:
    array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,
             729], dtype=int32)
    In [55]:
    a[::-1]
    Out[55]:
    array([  729,   512,   343,   216,   125, -1000,    27, -1000,     1,
           -1000], dtype=int32)
    In [58]:
    for i in a:
        print(i**(1/3.))
    nan
    1.0
    nan
    3.0
    nan
    5.0
    5.999999999999999
    6.999999999999999
    7.999999999999999
    8.999999999999998
    D:work01softwareanlibsite-packagesipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in power
    In [59]:
    def f(x,y):
        return 10*x + y
    b = np.fromfunction(f,(5,4),dtype=int)
    b
    Out[59]:
    array([[ 0,  1,  2,  3],
           [10, 11, 12, 13],
           [20, 21, 22, 23],
           [30, 31, 32, 33],
           [40, 41, 42, 43]])
    In [61]:
    b[1,2]   # [1,2] 里面是索引
    Out[61]:
    12
    In [62]:
    b[0:5,1] # 每行的第 2 个元素
    Out[62]:
    array([ 1, 11, 21, 31, 41])
    In [63]:
    b[-1] # 最后一行,等价于 b[-1,:]
    Out[63]:
    array([40, 41, 42, 43])
    In [64]:
    c = np.array([[[0,1,2],[10,12,13]],[[100,101,102],[110,112,113]]])
    c.shape
    Out[64]:
    (2, 2, 3)
    In [65]:
    c[1,...]
    Out[65]:
    array([[100, 101, 102],
           [110, 112, 113]])
    In [68]:
    c[1,:,:]
    Out[68]:
    array([[100, 101, 102],
           [110, 112, 113]])
    In [69]:
    c[...,2]
    Out[69]:
    array([[  2,  13],
           [102, 113]])
    In [75]:
    a = np.arange(12).reshape(3,4)     #  bool切片需要保证  元素个数对应行(bool数组的个数等于被切数组的行)或者列
    b1 = np.array([True,True,True])
    b2 = np.array([True,True,False,True])
    a[b1,b2]     #   神奇的一幕,每行只取了一个元素
    Out[75]:
    array([ 0,  5, 11])
    In [76]:
    for row in a:
        print(row)
    [0 1 2 3]
    [4 5 6 7]
    [ 8  9 10 11]
    In [77]:
    for element in a.flat:   #  flat属性,是数组元素的一个迭代器
        print(element)
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    In [78]:
    # 数组赋值
    a = np.arange(5)
    In [79]:
    a
    Out[79]:
    array([0, 1, 2, 3, 4])
    In [80]:
    a[[1,3,4]] = 0
    In [81]:
    a
    Out[81]:
    array([0, 0, 2, 0, 0])
    In [82]:
    a = np.arange(5)
    In [83]:
    a[[0,0,2]] = [1,2,3]
    a
    Out[83]:
    array([2, 1, 3, 3, 4])
    In [84]:
    a = np.arange(5)
    a[[0,0,2]] += 1
    a    # 注意,使用 += 的时候,即使索引0出现了两次,但是仅增加一次,这是因为Python要求 a+=1 和 a=a+1 等同
    Out[84]:
    array([1, 1, 3, 3, 4])
    In [87]:
    # 数组的组合 和 拆分
    a = np.floor(10*np.random.random((2,2)))
    b = np.floor(10*np.random.random((2,2)))
    a
    Out[87]:
    array([[3., 3.],
           [7., 8.]])
    In [88]:
    b
    Out[88]:
    array([[1., 6.],
           [5., 7.]])
    In [92]:
    np.vstack((a,b))    #  纵向拼接,增加样本个数
    Out[92]:
    array([[3., 3.],
           [7., 8.],
           [1., 6.],
           [5., 7.]])
    In [93]:
    np.hstack((a,b))   #横向拼接, 增加特征量
    Out[93]:
    array([[3., 3., 1., 6.],
           [7., 8., 5., 7.]])
    In [100]:
    # 函数column_stack:仅支持以序列顺序将多个一维数组或者一个二维数组按对位组合成新的二维数组
    a = np.array((1,2,3))
    b = np.array((2,3,4))
    np.column_stack((a,b))
    Out[100]:
    array([[1, 2],
           [2, 3],
           [3, 4]])
    In [95]:
    a = np.floor(10*np.random.random((2,12)))
    a
    Out[95]:
    array([[3., 2., 5., 2., 1., 9., 3., 2., 0., 3., 0., 9.],
           [5., 9., 8., 0., 6., 3., 4., 3., 6., 1., 5., 8.]])
    In [96]:
    np.hsplit(a,3)    #  将数组a切割成3份
    Out[96]:
    [array([[3., 2., 5., 2.],
            [5., 9., 8., 0.]]), array([[1., 9., 3., 2.],
            [6., 3., 4., 3.]]), array([[0., 3., 0., 9.],
            [6., 1., 5., 8.]])]
    In [97]:
    np.hsplit(a,(3,4))   #  在数组a的第3索引前和第4索引前切割
    Out[97]:
    [array([[3., 2., 5.],
            [5., 9., 8.]]), array([[2.],
            [0.]]), array([[1., 9., 3., 2., 0., 3., 0., 9.],
            [6., 3., 4., 3., 6., 1., 5., 8.]])]
    In [98]:
    x = np.arange(9)
    In [99]:
    np.split(x,[3,5,6,10])   #  直接用split 更加灵活

    Out[99]:
    [array([0, 1, 2]),
     array([3, 4]),
     array([5]),
     array([6, 7, 8]),
     array([], dtype=int32)]
  • 相关阅读:
    表单
    表格的应用
    图像与超链接
    HTML基础(部分标签的应用)
    DataFrame合并数据df.append
    DataFrame处理接口返回数据
    pandas读取excel文件
    数据加载、存储于文件格式:二进制数据格式pickle
    AttributeError: 'DataFrame' object has no attribute 'save'
    web信息收集:获取所有url
  • 原文地址:https://www.cnblogs.com/pyyolo/p/12167157.html
Copyright © 2011-2022 走看看