np.meshgrid

np.meshgrid参考
官方文档给出的解释

Return coordinate matrices from coordinate vectors.
Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,…, xn.

参数
indexing : {‘xy’, ‘ij’}, optional Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output.
返回

 X1, X2,..., XN : ndarray

For vectors x1, x2,…, xn with lengths Ni=len(xi) , return (N1, N2, N3,...Nn) shaped arrays if indexing=‘ij’ or (N2, N1, N3,...Nn) shaped arrays if indexing=‘xy’ with the elements of xi repeated to fill the matrix along the first dimension for x1, the second for x2 and so on.

  • 关于indexing,默认是xy形式的,即笛卡尔坐标系的形式。
  • 如果输入向量 x1, x2,…, xn 的长度Ni=len(xi),那么在indexing = 'xy'时返回(N2, N1, N3,...Nn)形式的numpy数组,indexing = ‘ij’时返回 (N2, N1, N3,...Nn)形式的numpy数组
  • 如果xi是np.array形式的数据,在进行np.meshgrid会自动地将xi先展平,再进行操作
    下面x是维度为(2,3,4)地numpy数组,展平后是(24,)
x = np.linspace(1,15,24).reshape(2,3,4)
y = np.linspace(11,15,6)
z = np.linspace(21,25,5)

xx,yy,zz = np.meshgrid(x,y,z)

print(xx.shape) # (6,24,5)
  • 对于numpy里面地矩阵(np.matrix),则不会进行展平。而且,需要注意变量的维度
m, n = (5, 3)
x = np.linspace(0, 1, m)
y = np.linspace(0, 1, n)
x:array([ 0. , 0.25, 0.5 , 0.75, 1. ])
y:array([ 0. , 0.5, 1. ])

在这里插入图片描述

xx,yy = np.meshgrid(x,y) 

x 是m 维向量,y是n维向量,np.meshgrid(x,y)后的结果是(n,m)
得到的xx,是x在纵轴方向上复制n次,yy是y在横轴方向上复制m次
在这里插入图片描述
生成测试数据网格

grid_test = np.stack((xx.flat,yy.flat),axis=1)
grid_predict= gmm.predict(grid_test)

得到grid_test

[[0.   0.  ]
 [0.25 0.  ]
 [0.5  0.  ]
 [0.75 0.  ]
 [1.   0.  ]
 [0.   0.5 ]
...]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值