zoukankan      html  css  js  c++  java
  • 数据分析之Numpy模块下

    三、ndarray的基本操作下部

    5.切分

      1. 与级联类似,三个函数完成切分工作:

        np.split(arr,   行/列号 ,轴):参数2是一个列表类型

        np.vsplit  行切分

        np.hsplit  列切分

      示例:

      创建一个二维数组
        np.random.seed(10)
        num_arr = np.random.randint(60,100,size=(5,6))
        num_arr
      结果:
        array([[69, 96, 75, 60, 88, 85],
               [89, 89, 68, 69, 60, 96],
               [76, 96, 71, 84, 93, 68],
               [96, 74, 73, 65, 73, 85],
               [73, 88, 82, 90, 90, 85]])
      根据索引去切片,0 表示行着切,1表示竖着切,[2,3]表示在索引为2和索引为3的时候个切一次
      行着切:
        np.split(num_arr,[2,3],axis=0)
      结果:
        [array([[69, 96, 75, 60, 88, 85],
                [89, 89, 68, 69, 60, 96]]),
         array([[76, 96, 71, 84, 93, 68]]),
         array([[96, 74, 73, 65, 73, 85],
                [73, 88, 82, 90, 90, 85]])]
    
      竖着切:
        np.split(num_arr,[1,3],axis=1)  
      结果:
        [array([[69],
                [89],
                [76],
                [96],
                [73]]), array([[96, 75],
                [89, 68],
                [96, 71],
                [74, 73],
                [88, 82]]), array([[60, 88, 85],
                [69, 60, 96],
                [84, 93, 68],
                [65, 73, 85],
                [90, 90, 85]])]

        2.切分照片

          导入一张照片
          # 导入matplotlib.pyplot,并起别名为plt
          import matplotlib.pyplot as plt
          img_arr = plt.imread('./cat.jpg')
          plt.imshow(img_arr)

        结果:

                

         2.1 行切分

        # axis为0的时候,表示行切分
        imgs = np.split(img_arr,[50,350],axis=0)
        plt.imshow(imgs[1])

          结果:

            

         2.2 列切分   

        imgs = np.split(img_arr,[100,600],axis=1)
        plt.imshow(imgs[1])

          结果:

            

    6.副本

      所有赋值运算不会为ndarray的任何元素创建副本。对赋值后的对象的操作也对原来的对象生效。

      可使用copy()函数创建副本

        arr = np.array([1,2,3,4,5,6])
        arr1 = arr.copy()

    四、ndarray的聚合操作

      1. 求和np.sum() 

      2. 最大最小值:np.max() /  np.min()

      3.平均值:np.mean()

      创建一个二维数组
      np.random.seed(10)
      arr = np.random.randint(1,50,size=(3,4))
      arr
      求和     arr.sum()   最大值     arr.max()   最小值     arr.min()   平均值     arr.mean()

      4.其他聚合操作

      Function Name    NaN-safe Version    Description
      np.sum    np.nansum    Compute sum of elements
      np.prod    np.nanprod    Compute product of elements
      np.mean    np.nanmean    Compute mean of elements
      np.std    np.nanstd    Compute standard deviation
      np.var    np.nanvar    Compute variance
      np.min    np.nanmin    Find minimum value
      np.max    np.nanmax    Find maximum value
      np.argmin    np.nanargmin    Find index of minimum value
      np.argmax    np.nanargmax    Find index of maximum value
      np.median    np.nanmedian    Compute median of elements
      np.percentile    np.nanpercentile    Compute rank-based statistics of elements
      np.any    N/A    Evaluate whether any elements are true
      np.all    N/A    Evaluate whether all elements are true
      np.power 幂运算

    五、广播机制

     

    【重要】ndarray广播机制的三条规则:缺失维度的数组将维度补充为进行运算的数组的维度。

                     缺失的数组元素使用已有元素进行补充。

    • 规则一:为缺失的维度补1(进行运算的两个数组之间的维度只能相差一个维度)
    • 规则二:缺失元素用已有值填充
    • 规则三:缺失维度的数组只能有一行或者一列

     示例1:

      

    示例二:

        

    示例三

          

    六、ndarray的排序

      1. 快速排序

        np.sort()与ndarray.sort()都可以,但有区别:

          np.sort()不改变输入

          ndarray.sort()本地处理,不占用空间,但改变输入

        示例:

          

      2.部分排序    np.partition(a,k)

        有的时候我们不是对全部数据感兴趣,我们可能只对最小或最大的一部分感兴趣。

        当k为正时,我们想要得到最小的k个数 当k为负时,我们m想要得到最大的k个数

        示例:

            

  • 相关阅读:
    P2572 [SCOI2010]序列操作
    P2787 语文1(chin1)- 理理思维
    P1835 素数密度_NOI导刊2011提高(04)
    P3942 将军令
    P1273 有线电视网
    U45490 还没想好名字的题Ⅱ
    U40620 还没想好名字的题
    P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚
    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
    T51071 Tony到死都想不出の数学题
  • 原文地址:https://www.cnblogs.com/mwhylj/p/10287336.html
Copyright © 2011-2022 走看看