zoukankan      html  css  js  c++  java
  • 再战CS231-数组的访问

    1.切片访问和整形访问的区别

    可以同时使用整型和切片语法来访问数组。但是,这样做会产生一个比原数组低阶的新数组

    import numpy as np
    
    # Create the following rank 2 array with shape (3, 4)
    # [[ 1  2  3  4]
    #  [ 5  6  7  8]
    #  [ 9 10 11 12]]
    a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
    
    # Two ways of accessing the data in the middle row of the array.
    # Mixing integer indexing with slices yields an array of lower rank,
    # while using only slices yields an array of the same rank as the
    # original array:
    row_r1 = a[1, :]    # Rank 1 view of the second row of a
    row_r2 = a[1:2, :]  # Rank 2 view of the second row of a
    print (row_r1, row_r1.shape)  # Prints "[5 6 7 8] (4,)"
    print (row_r2, row_r2.shape )# Prints "[[5 6 7 8]] (1, 4)"
    
    # We can make the same distinction when accessing columns of an array:
    col_r1 = a[:, 1]
    col_r2 = a[:, 1:2]
    print (col_r1, col_r1.shape)  # Prints "[ 2  6 10] (3,)"
    print (col_r2, col_r2.shape)  # Prints "[[ 2]
                                #          [ 6]
                                #          [10]] (3, 1)"
    

      

  • 相关阅读:
    2019/9/10
    2019/9/9
    软件课程设计(21)
    软件课程设计(20)
    软件课程设计(19)
    软件课程设计(18)
    软件课程设计(17)
    软件课程设计(16)
    数风流人物,还看今朝
    峰回路转二十四天
  • 原文地址:https://www.cnblogs.com/jycjy/p/8107924.html
Copyright © 2011-2022 走看看