numpy数组坐标轴问题解决
作者:勤奋的大熊猫 时间:2022-10-23 02:48:12
不知道大家有没有一种感觉,每次当使用numpy数组的时候坐标轴总是傻傻分不清楚,然后就会十分的困惑,每次运算都需要去尝试好久才能得出想要的结果。这里我们来简单解释一下numpy中一维,二维,三维数组的坐标轴问题。
首先我们讨论一维的情况,代码如下:
import numpy as np
class Debug:
def __init__(self):
self.array1 = np.array([0, 1, 2])
self.array2 = np.array([[0], [1], [2]])
def mainProgram(self):
print("The value of array1 is: ")
print(self.array1)
print("The shape of array1 is: ")
print(self.array1.shape)
print("The value of array2 is: ")
print(self.array2)
print("The shape of array2 is: ")
print(self.array2.shape)
if __name__ == '__main__':
main = Debug()
main.mainProgram()
"""
The value of array1 is:
[0 1 2]
The shape of array1 is:
(3,)
The value of array2 is:
[[0]
[1]
[2]]
The shape of array2 is:
(3, 1)
"""
从上面的结果我们可以看到,一维横数组沿着横向排列,我们可以认定为x轴向,它的数组大小为(3,),一维列数组沿着纵向排列,我们可以认为是y轴方向,它的大小为(3, 1),我们可以从左向右,看出第二个参数代表的是横向上的参数个数,第一个参数代表的是纵向上的参数个数,因此我们可以将横向数组的大小(3,)理解为(,3)更为合适。
接下来我们研究一下二维数组,哪个参数对应的是横坐标,哪个参数对应的是纵坐标。
代码如下:
import numpy as np
class Debug:
def __init__(self):
self.array1 = np.ones((2, 3))
self.array2 = np.ones((3, 2))
def mainProgram(self):
print("The value of array1 is: ")
print(self.array1)
print("The shape of array1 is: ")
print(self.array1.shape)
print("The value of array2 is: ")
print(self.array2)
print("The shape of array2 is: ")
print(self.array2.shape)
if __name__ == '__main__':
main = Debug()
main.mainProgram()
"""
The value of array1 is:
[[1. 1. 1.]
[1. 1. 1.]]
The shape of array1 is:
(2, 3)
The value of array2 is:
[[1. 1.]
[1. 1.]
[1. 1.]]
The shape of array2 is:
(3, 2)
"""
从上面的结果我们可以看出,从左向右,第一个参数代表的是(row), 第二个参数代表的是列(column)。我们知道numpy中默认的是笛卡尔坐标系,所以横向为x,纵向为y,具体的请看坐标系(超链接点击跳转查看)。所以对self.array1来说,定义时输入的数组大小的(2, 3)代表沿着x轴拥有3个值,沿着y轴拥有2个值。对比上述得到的结果与我们在一维情况中推断得到的结果,证明我们的理解是正确的。
接着我们讨论三维的情况:代码如下:
import numpy as np
class Debug:
def __init__(self):
self.array1 = np.ones((2, 3, 4))
def mainProgram(self):
print("The value of array1 is: ")
print(self.array1)
print("The shape of array1 is: ")
print(self.array1.shape)
if __name__ == '__main__':
main = Debug()
main.mainProgram()
"""
The value of array1 is:
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
The shape of array1 is:
(2, 3, 4)
"""
不难发现,沿着x轴方向拥有4个值,沿着y轴方向拥有3个值,沿着z轴方向拥有2个值。
综上所述,在numpy数组中,定义三维数组时,从左向右, 第一个参数为z轴,第二个参数为y轴,第三个参数为x轴,即(z, y, x)。 对于各个坐标轴在空间中的朝向问题,建议阅读numpy数组坐标轴。之后我们会进一步探讨numpy模块中的其他与坐标轴相关的函数。
来源:https://blog.csdn.net/u011699626/article/details/108999206
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Go 依赖管理 go mod tidy
![](https://img.aspxhome.com/file/2023/4/130714_0s.jpg)
node.js+Ajax实现获取HTTP服务器返回数据
![](https://img.aspxhome.com/file/2023/1/125731_0s.png)
Python正则表达式 r'(.*) are (.*?) .*'的深入理解
Django修改端口号与地址的三种方式
![](https://img.aspxhome.com/file/2023/3/60983_0s.png)
python包合集shutil示例代码详解
![](https://img.aspxhome.com/file/2023/5/115755_0s.png)
Python JSON编解码方式原理详解
![](https://img.aspxhome.com/file/2023/2/97322_0s.png)
JS实现普通轮播图特效
![](https://img.aspxhome.com/file/2023/2/136312_0s.jpg)
Python locust工具使用详解
![](https://img.aspxhome.com/file/2023/3/83703_0s.png)
Oracle动态交叉表生成
Go之interface的具体使用
django 前端页面如何实现显示前N条数据
10个ASP网页制作技巧
python3 queue多线程通信
![](https://img.aspxhome.com/file/2023/7/135197_0s.png)
Oracle数据操作和控制语言详解
Session的工作机制详解和安全性问题(PHP实例讲解)
![](https://img.aspxhome.com/file/2023/2/129842_0s.png)
Python实现将Word表格嵌入到Excel中
![](https://img.aspxhome.com/file/2023/8/133858_0s.png)
Python子类继承父类构造函数详解
Python数据类型之Set集合实例详解
python人工智能使用RepVgg实现图像分类示例详解
![](https://img.aspxhome.com/file/2023/8/123568_0s.jpg)