欢迎访问WDPHP素材源码!今天是2024年04月27日 星期六,傍晚好!下班了!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 教程 > 其他教程 > 

numpy数组坐标轴问题如何解决
栏目分类:其他教程    发布日期:2023-08-13    浏览次数:313次     收藏

本篇内容介绍了“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个值。

源码 模板 特效 素材 资源 教程 站长