当前位置:网站首页>np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍
np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍
2022-06-12 09:56:00 【chensi000000】
一、np.meshgrid()函数
1、np.meshgrid()介绍
X, Y = np.meshgrid(x, y) 代表的是将x中每一个数据和y中每一个数据组合生成很多点,然后将这些点的x坐标放入到X中,y坐标放入Y中,并且相应位置是对应的
下面是官方文档,官方文档写的也很抽象,可以不读,直接看【2、np.meshgrid()生成坐标位置】中的例子
numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
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.
2、np.meshgrid()生成坐标位置
①其实np.meshgrid()生成的并不是二维空间中的坐标,还需要把对应位置的两个值合并在一起,才是坐标位置:
例如:
import numpy as np
x = np.linspace(0, 2, 3)
y = np.linspace(0, 1, 2)
X, Y = np.meshgrid(x, y)
coors = np.concatenate((X[:, :, None], Y[:, :, None]), axis=-1)
print(X)
print(Y)
print(X[:, :, None])
print(Y[:, :, None])
print(coors)返回值:
# X
[[0. 1. 2.]
[0. 1. 2.]]
# Y
[[0. 0. 0.]
[1. 1. 1.]]
# X[:, :, None]
[[[0.]
[1.]
[2.]]
[[0.]
[1.]
[2.]]]
# Y[:, :, None]
[[[0.]
[0.]
[0.]]
[[1.]
[1.]
[1.]]]
# !!!!coors
[[[0. 0.]
[1. 0.]
[2. 0.]]
[[0. 1.]
[1. 1.]
[2. 1.]]]可以看到,np.meshgrid()生成的值,要再经过一次np.concatenate()才是坐标。
重点来了:
从上面例子的coors的输出值,可以看出:在生成坐标位置的矩阵时,是以最后一维的向量作为元素的,所以,其第-2维才是第真正的-1维,第-3维才是真正的-2维。
如下图所示:
② np.meshgrid()也可以生成三维及以上维度的坐标
import numpy as np
x = np.linspace(0, 2, 3)
y = np.linspace(0, 1, 2)
z = np.linspace(0, 3, 4)
X, Y, Z = np.meshgrid(x, y, z)
coors = np.concatenate((X[:, :, :, None], Y[:, :, :, None], Z[:, :, :, None]), axis=-1) # 注意这里是三个冒号
print(X)
print(Y)
print(Z)
print(coors)返回值:
# X
[[[0. 0. 0. 0.]
[1. 1. 1. 1.]
[2. 2. 2. 2.]]
[[0. 0. 0. 0.]
[1. 1. 1. 1.]
[2. 2. 2. 2.]]]
# Y
[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
# Z
[[[0. 1. 2. 3.]
[0. 1. 2. 3.]
[0. 1. 2. 3.]]
[[0. 1. 2. 3.]
[0. 1. 2. 3.]
[0. 1. 2. 3.]]]
# coors
[[[[0. 0. 0.]
[0. 0. 1.]
[0. 0. 2.]
[0. 0. 3.]]
[[1. 0. 0.]
[1. 0. 1.]
[1. 0. 2.]
[1. 0. 3.]]
[[2. 0. 0.]
[2. 0. 1.]
[2. 0. 2.]
[2. 0. 3.]]]
[[[0. 1. 0.]
[0. 1. 1.]
[0. 1. 2.]
[0. 1. 3.]]
[[1. 1. 0.]
[1. 1. 1.]
[1. 1. 2.]
[1. 1. 3.]]
[[2. 1. 0.]
[2. 1. 1.]
[2. 1. 2.]
[2. 1. 3.]]]]但是,np.meshgrid()生成三维坐标位置也是有很大问题的:
无论怎么修改np.meshgrid()中x、y、z的顺序,都无法实现对x,y,z中的值都实现从小到大,而且先x从到大,然后y从小到大,最后z从小到大,见【二、1、】中例子的输出结果
二、不使用np.meshgrid()生成三维坐标位置
1、使用np.repeat()实现
numpy.repeat(a, repeats, axis=None)
Repeat elements of an array.
Parameters:
a:array_like
Input array.
repeats:int or array of ints
The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.
axis:int, optional
The axis along which to repeat values. By default, use the flattened input array, and return a flat output array.
Returns
repeated_array:ndarray
Output array which has the same shape as a, except along the given axis.
例子:
>>> np.repeat(3, 4) array([3, 3, 3, 3]) >>> x = np.array([[1,2],[3,4]]) >>> np.repeat(x, 2) # 如果没写维度,就是flatten以后再repeat array([1, 1, 2, 2, 3, 3, 4, 4]) >>> np.repeat(x, 3, axis=1) array([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]]) >>> np.repeat(x, [1, 2], axis=0) # 在axis=0这个维度上,第一行重复1次,第二行重复2次 array([[1, 2], [3, 4], [3, 4]])
生成三维坐标位置:
(这个方式是我比较推荐的方式)
import numpy as np
x = np.linspace(0, 2, 3)
y = np.linspace(0, 1, 2)
z = np.linspace(0, 3, 4)
xx = np.repeat(x[None, :], len(y), axis=0) # 第一个None对应Z,第二个None对应Y;所以后面是(len(z), len(y))
xxx = np.repeat(xx[None, :, :], len(z), axis=0)
yy = np.repeat(y[:, None], len(x), axis=1)
yyy = np.repeat(yy[None, :, :], len(z), axis=0)
zz = np.repeat(z[:, None], len(y), axis=1)
zzz = np.repeat(zz[:, :, None], len(x), axis=2)
# 这里zzz, yyy, xxx的顺序别错了,不然不好理解
coors = np.concatenate((zzz[:, :, :, None], yyy[:, :, :, None], xxx[:, :, :, None]), axis=-1) # 这里zzz, yyy, xxx的顺序别错了,不然不好理解
print(coors)返回值:
[[[[0. 0. 0.]
[0. 0. 1.]
[0. 0. 2.]]
[[0. 1. 0.]
[0. 1. 1.]
[0. 1. 2.]]]
[[[1. 0. 0.]
[1. 0. 1.]
[1. 0. 2.]]
[[1. 1. 0.]
[1. 1. 1.]
[1. 1. 2.]]]
[[[2. 0. 0.]
[2. 0. 1.]
[2. 0. 2.]]
[[2. 1. 0.]
[2. 1. 1.]
[2. 1. 2.]]]
[[[3. 0. 0.]
[3. 0. 1.]
[3. 0. 2.]]
[[3. 1. 0.]
[3. 1. 1.]
[3. 1. 2.]]]]2、通过np.where()获得坐标位置
也是比较推荐的方法之一
zeros = np.zeros((4, 2, 3))
zzz, yyy, xxx = np.where(zeros==0)
print(zzz)
print(yyy)
print(xxx)
zzz = np.reshape(zzz, (4, 2, 3))
yyy = np.reshape(yyy, (4, 2, 3))
xxx = np.reshape(xxx, (4, 2, 3))
coors = np.concatenate((zzz[:, :, :, None], yyy[:, :, :, None], xxx[:, :, :, None]), axis=-1) # 这里zzz, yyy, xxx的顺序别错了,不然不好理解
print(coors)返回值:
# zzz
[0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3]
# yyy
[0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1]
# xxx
[0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2]
# coors
[[[[0 0 0]
[0 0 1]
[0 0 2]]
[[0 1 0]
[0 1 1]
[0 1 2]]]
[[[1 0 0]
[1 0 1]
[1 0 2]]
[[1 1 0]
[1 1 1]
[1 1 2]]]
[[[2 0 0]
[2 0 1]
[2 0 2]]
[[2 1 0]
[2 1 1]
[2 1 2]]]
[[[3 0 0]
[3 0 1]
[3 0 2]]
[[3 1 0]
[3 1 1]
[3 1 2]]]]边栏推荐
- Essentials reading notes
- Introduction to applet
- Auto.js学习笔记4:autojs打包后,大部分华为等大牌子手机无法安装?利用模拟器远程在autoPro里签名打包可以解决该问题。
- UE4_ Explore the method of creating background scenes with ready-made resources
- 2021-02-12
- 003:what does AWS think is a data lake?
- Auto. JS learning notes 5:ui interface foundation of autojs Chapter 1
- 科创人·世界500强集团CIO李洋:数字化转型成事在人,决策者应时刻聚焦于「柴」
- MYSQL的最左匹配原則的原理講解
- Auto.js学习笔记10:实例化自定义对象,在子线程使用JSON.stringify()方法导致报错(已解决)
猜你喜欢

传输层协议 ——— TCP协议

基于SSM实现水果商城批发平台

FPGA基于DE2-115平台的VGA显示

In 2026, the capacity of China's software defined storage market will be close to US $4.51 billion

哈希表的理论讲解

使用Visual Studio 2017创建简单的窗口程序

Auto. JS learning note 4: after autojs is packaged, most Huawei and other big brand mobile phones cannot be installed? This problem can be solved by using the simulator to remotely sign and package in

《真北》读书笔记

High quality and good books help guide apes and recommend "good summer books" with the four major publishers

Autojs learning notes 6:text (txt) Findone() will report an error when switching apps. Finally, solve the implementation effect and switch any app until the script finds the control with the specified
随机推荐
2021-02-21
MySQL index FAQs
优质好书助成长 猿辅导携四大出版社推荐“暑期好书”
002:数据湖有哪些特征
Auto.js学习笔记9:脚本引擎使用,启动指定路径脚本文件和关闭等基础方法
MySQL optimized slow log query
string类对象的访问及遍历操作
【clickhouse专栏】基础数据类型说明
Auto.js学习笔记8:常用且重要的一些API
QQ, wechat chat depends on it (socket)?
科创人·神州数码集团CIO沈旸:最佳实践模式正在失灵,开源加速分布式创新
C language recursive folder code
004:aws data Lake solution
一文读懂Dfinity生态中的首个NFT平台:IMPOSSIBLE THINGS
小程序介绍
Common tree summary
《要领》读书笔记
SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
Theoretical explanation of hash table
行业分析怎么做
