当前位置:网站首页>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]]]]边栏推荐
- Crazy temporary products: super low price, big scuffle and new hope
- 六月集训(第12天) —— 链表
- Auto. JS learning notes 8: some common and important APIs
- 行业分析怎么做
- Value investment
- C# break continue return 三者区别
- 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
- CEPH performance optimization and enhancement
- 抽象类和接口
- 使用Visual Studio 2017创建简单的窗口程序
猜你喜欢

SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000

How CEPH improves storage performance and storage stability

markdown_图片并排的方案

软件定义存储概览(一篇就够)

Auto.js调试:使用雷电模拟器的网络模式进行调试

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

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

Web3.0与数字时尚,该如何落地?

Differences among list, set and map

7-13 underground maze exploration (adjacency table)
随机推荐
MySQL优化之慢日志查询
gnu-efi开发环境设置
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
SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
Auto.js学习笔记9:脚本引擎使用,启动指定路径脚本文件和关闭等基础方法
Auto.js调试:使用雷电模拟器的网络模式进行调试
spark_ sql
Theoretical explanation of hash table
004:aws data Lake solution
[preview of the open class of Jishu] arm's strongest MCU core cortex-m85 processor helps the innovation of the Internet of things in an all-round way (there is a lottery)
C# break continue return 三者区别
DNA数字信息存储的研究进展
002: what are the characteristics of the data lake
【系统分析师之路】第十八章 复盘系统安全分析与设计
7-5 zhe zhe playing games
Autojs微信研究:微信不同的版本或模拟器上的微信里的控件ip是不同的。
Combat tactics based on CEPH object storage
【云原生】具体指什么呢---此文和大伙儿分享答案
005:数据湖与数据仓库的区别
Overview of software definition storage (one article is enough)
