美文网首页我爱编程
Numpy学习笔记(三)

Numpy学习笔记(三)

作者: 深思海数_willschang | 来源:发表于2016-09-20 10:19 被阅读182次

    【转载】Numpy教程

    复制与视图

    完全不拷贝
    简单的赋值不拷贝数组对象或它们的数据

    >>> a = arange(12)
    >>> b = a            # no new object is created
    >>> b is a           # a and b are two names for the same ndarray object
    True
    >>> b.shape = 3,4    # changes the shape of a
    >>> a.shape
    (3, 4)
    

    视图(view)和浅复制
    不同的数组对象分享同一个数据。视图方法创造一个新的数组对象指向同一数据。

    >>> c = a.view()
    >>> c is a
    False
    >>> c.base is a                        # c is a view of the data owned by a
    True
    >>> c.flags.owndata
    False
    >>>
    >>> c.shape = 2,6                      # a's shape doesn't change
    >>> a.shape
    (3, 4)
    >>> c[0,4] = 1234                      # a's data changes
    >>> a
    array([[   0,    1,    2,    3],
           [1234,    5,    6,    7],
           [   8,    9,   10,   11]])
    

    切片数组返回它的一个视图:

    
    >>> s = a[ : , 1:3]     # spaces added for clarity; could also be written "s = a[:,1:3]"
    >>> s[:] = 10           # s[:] is a view of s. Note the difference between s=10 and s[:]=10
    >>> a
    array([[   0,   10,   10,    3],
           [1234,   10,   10,    7],
           [   8,   10,   10,   11]])
    

    深复制
    这个复制方法完全复制数组和它的数据。

    >>> d = a.copy()                          # a new array object with new data is created
    >>> d is a
    False
    >>> d.base is a                           # d doesn't share anything with a
    False
    >>> d[0,0] = 9999
    >>> a
    array([[   0,   10,   10,    3],
           [1234,   10,   10,    7],
           [   8,   10,   10,   11]])
    

    函数和方法(method)总览

    这是个NumPy函数和方法分类排列目录。这些名字链接到 NumPy示例 ,你可以看到这些函数起作用。

    创建数组
    arange, array, copy, empty, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r , zeros, zeros_like

    转化
    astype, atleast 1d, atleast 2d, atleast 3d, mat

    操作
    array split, column stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack

    询问
    all, any, nonzero, where

    排序
    argmax, argmin, argsort, max, min, ptp, searchsorted, sort

    运算
    choose, compress, cumprod, cumsum, inner, fill, imag, prod, put, putmask, real, sum

    基本统计
    cov, mean, std, var

    基本线性代数
    cross, dot, outer, svd, vdot

    ix_()函数
    ix_ 函数可以为了获得 多元组 的结果而用来结合不同向量。例如,如果你想要用所有向量a、b和c元素组成的三元组来计算 a+b*c:

    >>> a = array([2,3,4,5])
    >>> b = array([8,5,4])
    >>> c = array([5,4,6,8,3])
    >>> ax,bx,cx = ix_(a,b,c)
    >>> ax
    array([[[2]],
    
           [[3]],
    
           [[4]],
    
           [[5]]])
    >>> bx
    array([[[8],
            [5],
            [4]]])
    >>> cx
    array([[[5, 4, 6, 8, 3]]])
    >>> ax.shape, bx.shape, cx.shape
    ((4, 1, 1), (1, 3, 1), (1, 1, 5))
    >>> result = ax+bx*cx
    >>> result
    array([[[42, 34, 50, 66, 26],
            [27, 22, 32, 42, 17],
            [22, 18, 26, 34, 14]],
           [[43, 35, 51, 67, 27],
            [28, 23, 33, 43, 18],
            [23, 19, 27, 35, 15]],
           [[44, 36, 52, 68, 28],
            [29, 24, 34, 44, 19],
            [24, 20, 28, 36, 16]],
           [[45, 37, 53, 69, 29],
            [30, 25, 35, 45, 20],
            [25, 21, 29, 37, 17]]])
    >>> result[3,2,4]
    17
    >>> a[3]+b[2]*c[4]
    17
    

    相关文章

      网友评论

        本文标题:Numpy学习笔记(三)

        本文链接:https://www.haomeiwen.com/subject/mmtoettx.html