zoukankan      html  css  js  c++  java
  • NumPy的思考……


    问题:

      为什么第一次输出矩阵形式的数据,第二次输出list形式的数据?

    详见代码:

    a = np.array([[1, 2], [3, 4]])
    print(a)
    print('ndim :', a.ndim)

    控制台输出:

    [[1 2]
     [3 4]]
    ndim : 2

    然而,代码修改一下:

    b = np.array([[11, 12], [14, 16, 17]])
    print(b)
    print(type(b))

     控制台输出:

    [list([11, 12]) list([14, 16, 17])]
    <class 'numpy.ndarray'>

    答案:

      第二次输出元素 [ [11, 12], [14, 16, 17] ] 在形式上不能用矩阵形式输出,不对称。


    问题:

    array(p_object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
    order : {'K', 'A', 'C', 'F'}, optional
                Specify the memory layout of the array. order参数用于:指定数组的内存布局。

      order的作用体现在哪里?

      怎么才能看出数组的内存布局?

     答案:

      ndarray.flags属性, 查看ndarray对象的内存信息

      C_CONTIGUOUS : True        c_contiguous(连续的)
      F_CONTIGUOUS : False        f_contiguous
      OWNDATA : True           own data
      WRITEABLE : True          writeable
      ALIGNED : True           aligned(对齐)
      WRITEBACKIFCOPY : False     write back if copy
      UPDATEIFCOPY : False       update if copy

    sin():

    Parameters
        ----------
        x : array_like
            Angle, in radians (:math:`2 pi` rad equals 360 degrees).

    2 pi = 2π

    360°等于2π弧度
    在数学和物理中,弧度是角的量度单位.它是由国际单位制导出的单位,单位缩写是rad.(radians)

    弧度定义:弧长等于圆半径的弧所对的圆心角为1弧度

    根据定义,一周的弧度数为2πr/r=2π, 360°角=2π弧度,因此,1弧度约为57.3°,即57°17'44.806'',1°为π/180弧度,近似值为0.01745弧度,周角为2π弧度, 平角(即180°角)为π弧度, 直角为π/2弧度.
    在具体计算中,角度以弧度给出时,通常不写弧度单位,直接写值.最典型的例子是三角函数,如sin 8π、tan (3π/2).
    弧长公式:
    l rad=nπr/180
    在这里,n就是角度数.


    np.repeat()使用技巧

    c = np.array([[1, 2, 3]])
    c = c.reshape(-1, 3).repeat(2, 0)
    输出c:
    [[1, 2, 3],
     [1, 2, 3]]
    =====================
    cc = c.reshape(-1, 3)
    # -1代表不管行数,只是确定列数为3
    
    # repeat用法
    d = c.reshape(-1, 3)
    d.repeat(2, 0)                           可以
    d.repeat(preats=2, axis=0)               可以
    np.repeat(d, 2, 0)                       也可以                        

    问题:

      同样是order排序,传入'C','F'不同,则打印不同?

    代码:

    import numpy as np
    a = np.arange(0, 60, 5).reshape((3, 4))
    print(a)
    for x in np.nditer(a,flags=['external_loop'], order='C'):
        print(x, end=', ')

    控制台输出:

    [[ 0  5 10 15]
     [20 25 30 35]
     [40 45 50 55]]
    [ 0  5 10 15 20 25 30 35 40 45 50 55], 

    然而,代码:

    import numpy as np
    a = np.arange(0, 60, 5).reshape((3, 4))
    print(a)
    for x in np.nditer(a,flags=['external_loop'], order='F'):
        print(x, end=', ')

    控制台输出:

    [[ 0  5 10 15]
     [20 25 30 35]
     [40 45 50 55]]
    [ 0 20 40], [ 5 25 45], [10 30 50], [15 35 55],

    答案:

      迭代器遍历对应于每列,并组合为一维数组。(默认)


    问题:

      a.T在nditer中迭代不应该输出

    [[0 3]
     [1 4]
     [2 5]]

      吗?

    代码:

    a = np.arange(6).reshape(2, 3)
    print(a)
    print(a.T)
    print(a.T.flags)
    for x in np.nditer(a.T):
        print(x, end=', ')
    print('
    ')

    控制台输出:

    [[0 1 2]
     [3 4 5]]
    
    [[0 3]
     [1 4]
     [2 5]]
    
      C_CONTIGUOUS : False
      F_CONTIGUOUS : True
      OWNDATA : False
      WRITEABLE : True
      ALIGNED : True
      WRITEBACKIFCOPY : False
      UPDATEIFCOPY : False
    
    0, 1, 2, 3, 4, 5, 

    答案:

      np.nditer(..., order='K') :

        order参数默认‘K’

      源码中,as close sth. as possible : 尽可能靠近

    'K' means as close to the order the array elements appear in memory as possible.

      翻译:'K' 意味着,(顺序要)尽可能靠近内存中出现的的数组的元素顺序。   


    =============================================

  • 相关阅读:
    angularjs的$on、$emit、$broadcast
    angularjs中的路由介绍详解 ui-route(转)
    ionic入门教程-ionic路由详解(state、route、resolve)(转)
    Cocos Creator 加载使用protobuf第三方库,因为加载顺序报错
    Cocos Creator 计时器错误 cc.Scheduler: Illegal target which doesn't have uuid or instanceId.
    Cocos Creator 构造函数传参警告 Can not instantiate CCClass 'Test' with arguments.
    Cocos Creator 对象池NodePool
    Cocos Creator 坐标系 (convertToWorldSpaceAR、convertToNodeSpaceAR)
    Cocos Creator 常驻节点addPersistRootNode
    Cocos Creator 配合Tiled地图的使用
  • 原文地址:https://www.cnblogs.com/daemonFlY/p/10029667.html
Copyright © 2011-2022 走看看