zoukankan      html  css  js  c++  java
  • numpy学习整理

    今天先整理到这里,剩下的下次再整理
    1.改变形状:
    reshape()返回改变的数组形状,但无法改变源数组形状
    resize() 可以改变源数组形状
    ravel() 输出类似C数组的列表,和reshape()一样,返回C似的数组但无法改变源数组形状
    例如:

    >>> from numpy import *
    >>> c = arange(24)
    >>> print c
    [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
    >>> c.resize(4,6)
    >>> print c
    [[ 0  1  2  3  4  5]
     [ 6  7  8  9 10 11]
     [12 13 14 15 16 17]
     [18 19 20 21 22 23]]
    >>> c.reshape(3,8)
    array([[ 0,  1,  2,  3,  4,  5,  6,  7],
           [ 8,  9, 10, 11, 12, 13, 14, 15],
           [16, 17, 18, 19, 20, 21, 22, 23]])
    >>> print c
    [[ 0  1  2  3  4  5]
     [ 6  7  8  9 10 11]
     [12 13 14 15 16 17]
     [18 19 20 21 22 23]]
    >>> print c.ravel()
    [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
    >>> print c
    [[ 0  1  2  3  4  5]
     [ 6  7  8  9 10 11]
     [12 13 14 15 16 17]
     [18 19 20 21 22 23]]

    2.组合(stack)不同的数组
    vstack():横向组合数组
    hstack():纵向组合数组
    column_stack():纵向组合数组,和hstack()效果一样,区别在哪,目前我也不懂…
    row_stack(): 横向组合数组,和vstack()效果一样,区别在哪,目前我也不懂…

    >>> b = floor(10*random.random((2,2)))
    >>> a = floor(10*random.random((2,2)))
    >>> a
    array([[ 6.,  3.],
           [ 9.,  9.]])
    >>> b
    array([[ 9.,  3.],
           [ 6.,  5.]])
    >>> column_stack((a,b))
    array([[ 6.,  3.,  9.,  3.],
           [ 9.,  9.,  6.,  5.]])
    >>> hstack((a,b))
    array([[ 6.,  3.,  9.,  3.],
           [ 9.,  9.,  6.,  5.]])
    >>> row_stack((a,b))
    array([[ 6.,  3.],
           [ 9.,  9.],
           [ 9.,  3.],
           [ 6.,  5.]])
    >>> vstack((a,b))
    array([[ 6.,  3.],
           [ 9.,  9.],
           [ 9.,  3.],
           [ 6.,  5.]])
    >>>

    3.复制(视图复制)
    不同的数组对象分享同一个数据。视图方法创造一个新的数组对象“指向”同一数据。视图复制之后,有独立的数据形状
    但是这是浅复制,数据是同步的

    >>> a = arange(12).reshape((3,4))
    >>> a
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    >>> c = a.view()
    >>> c
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    >>> c is a
    False
    >>> c.resize((2,6))
    >>> c
    array([[ 0,  1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10, 11]])
    >>> a
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    >>> c[0] = 1234
    >>> c
    array([[ 1234,  1234,  1234,  1234,  1234,  1234],
           [ 6,  7,  8,  9, 10, 11]])
    >>> a
    array([[1234, 1234, 1234, 1234],
           [   1234,    1234,    6,    7],
           [   8,    9,   10,   11]])
    欢迎来邮件交流:lq65535@163.com
  • 相关阅读:
    坦克大战
    java多线程应用场景
    java中的多线程(资料)
    设置线程名
    线程名称的设置及取得
    java调试
    文件上传细节处理
    Servlet生命周期
    java的动态绑定与静态绑定
    Mysql 连接池调用完成后close代理方法引出的设计模式
  • 原文地址:https://www.cnblogs.com/lq1024/p/7593643.html
Copyright © 2011-2022 走看看