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 ]
...]
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
