zoukankan      html  css  js  c++  java
  • numpy之meshgrid和where

    meshgrid

    np.meshgrid()

    np.meshgrid从坐标向量返回坐标矩阵。

    这样说可能很抽象。举个例子。

    
    x = np.arange(-2,2)
    y = np.arange(0,3)#生成一位数组,其实也就是向量
    
    x
    Out[31]: array([-2, -1,  0,  1])
    
    y
    Out[32]: array([0, 1, 2])
    
    z,s = np.meshgrid(x,y)#将两个一维数组变为二维矩阵
    
    z
    Out[36]: 
    array([[-2, -1,  0,  1],
           [-2, -1,  0,  1],
           [-2, -1,  0,  1]])
    
    s
    Out[37]: 
    array([[0, 0, 0, 0],
           [1, 1, 1, 1],
           [2, 2, 2, 2]])
    
    

    从代码上看,我们得到了这样一组值:

    -2, -1, 0, 1,---- 0, 0, 0, 0
    -2, -1, 0, 1,---- 1, 1, 1, 1

    也就是说,它讲 x 变成了矩阵 z 的行向量,y 变成了矩阵 s 的列向量。

    反过来,也是一样的:

    z,s = np.meshgrid(y,x)
    
    z
    Out[40]: 
    array([[0, 1, 2],
           [0, 1, 2],
           [0, 1, 2],
           [0, 1, 2]])
    
    s
    Out[41]: 
    array([[-2, -2, -2],
           [-1, -1, -1],
           [ 0,  0,  0],
           [ 1,  1,  1]])
    

    以上面这个例子来说,z 和 s 就构成了一个坐标矩阵,实际上也就是一个网格,不知道你没有注意到,z 和 s 的维数是一样的,是一个4 × 4的网格矩阵,也就是坐标矩阵。

    meshgrid 方法的参数数量不受限,可以得到任意 N 维空间中的坐标矩阵。

    注意到,传入的对象是一维的。

    想到这里,我觉得,这可能和方程式有关系(很可能我的感觉是错的,等以后发现再改这句话,但是我觉得这样的话,会很好理解这个函数方法),也就是行列式,但是方程式的右侧的 y 只有一列。

    a1x1 + b1x2 + c1x3 + d1x4 + ...... =y1
    a2x1 + b2x2 + c2x3 + d2x4 + ...... =y2
    ...
    ...

    
    x, y = np.meshgrid(np.arange(-1, 1, 0.01), np.arange(-1, 1, 0.01))
    
    contor = np.sqrt(x ** 2 + y ** 2)
    plt.imshow(contor)
    plt.colorbar()
    plt.show()
    
    

    结果

    np.where()

    where(condition, [x, y]) 当condition为True时,返回 x , 否则返回 y。

    其实,在x, y 为一维数组时,就相当于:

    [xv if c else yv for (c,xv,yv) in zip(condition,x,y)]

    测试:

    
    In [3]: x = np.arange(9).reshape(3,3)#创建一个3×3的矩阵
    
    In [4]: x
    Out[4]:
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    
    In [5]: np.where(x>4) #只输入condition
    Out[5]: (array([1, 2, 2, 2], dtype=int64), array([2, 0, 1, 2], dtype=int64))
    
    In [6]: np.where(x>7)# 只输入condition
    Out[6]: (array([2], dtype=int64), array([2], dtype=int64))
    
    

    通过上面的例子,我们可以发现,只输入condition的话,得到的结果是一个位置索引。它们就是满足条件的元素的索引,即为True的元素。

    说明下:返回的第一个第一个数组为行坐标,第二个为纵坐标。

    我们还可以用where来这样做:

    
    In [8]: y = np.random.randn(3,3)
    
    In [9]: y
    Out[9]:
    array([[ 1.59809956, -0.42735851,  1.46593089],
           [-0.26497622,  0.53948157, -2.01569974],
           [-0.11099139, -1.70616601, -1.34821361]])
    
    In [10]: np.where(y > 0, 4, -4)
    Out[10]:
    array([[ 4, -4,  4],
           [-4,  4, -4],
           [-4, -4, -4]])
    
    

    很显然,np.where()是可以嵌套使用的,其类似于if..elif...else...,如果我们有多个条件的话。

    大家都知道,布尔值在计算过程中是可以当做0和1处理的。
    因此,我们还可以这样:

    result = 3 * (con2 & -cond1) + 2 * - (cond1 | cond2)

  • 相关阅读:
    14.RabbitMQ
    13.跨域
    12.EF
    11.Redis
    GitHub获取用户ID
    10.AOP
    第26节课:pytest结合Allure操作
    第25节课:pytest测试框架
    第二十四节课:requests爬虫实战
    第二十三节课:正则表达式re模块:
  • 原文地址:https://www.cnblogs.com/sunshinewang/p/6897966.html
Copyright © 2011-2022 走看看