numpy数组坐标轴问题如何解决

其他教程   发布日期:2023年08月20日   浏览次数:376

本篇内容介绍了“numpy数组坐标轴问题如何解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

首先我们讨论一维的情况,代码如下:

  1. import numpy as np
  2. class Debug:
  3. def __init__(self):
  4. self.array1 = np.array([0, 1, 2])
  5. self.array2 = np.array([[0], [1], [2]])
  6. def mainProgram(self):
  7. print("The value of array1 is: ")
  8. print(self.array1)
  9. print("The shape of array1 is: ")
  10. print(self.array1.shape)
  11. print("The value of array2 is: ")
  12. print(self.array2)
  13. print("The shape of array2 is: ")
  14. print(self.array2.shape)
  15. if __name__ == '__main__':
  16. main = Debug()
  17. main.mainProgram()
  18. """
  19. The value of array1 is:
  20. [0 1 2]
  21. The shape of array1 is:
  22. (3,)
  23. The value of array2 is:
  24. [[0]
  25. [1]
  26. [2]]
  27. The shape of array2 is:
  28. (3, 1)
  29. """

从上面的结果我们可以看到,一维横数组沿着横向排列,我们可以认定为x轴向,它的数组大小为(3,),一维列数组沿着纵向排列,我们可以认为是y轴方向,它的大小为(3, 1),我们可以从左向右,看出第二个参数代表的是横向上的参数个数,第一个参数代表的是纵向上的参数个数,因此我们可以将横向数组的大小(3,)理解为(,3)更为合适。

接下来我们研究一下二维数组,哪个参数对应的是横坐标,哪个参数对应的是纵坐标。
代码如下:

  1. import numpy as np
  2. class Debug:
  3. def __init__(self):
  4. self.array1 = np.ones((2, 3))
  5. self.array2 = np.ones((3, 2))
  6. def mainProgram(self):
  7. print("The value of array1 is: ")
  8. print(self.array1)
  9. print("The shape of array1 is: ")
  10. print(self.array1.shape)
  11. print("The value of array2 is: ")
  12. print(self.array2)
  13. print("The shape of array2 is: ")
  14. print(self.array2.shape)
  15. if __name__ == '__main__':
  16. main = Debug()
  17. main.mainProgram()
  18. """
  19. The value of array1 is:
  20. [[1. 1. 1.]
  21. [1. 1. 1.]]
  22. The shape of array1 is:
  23. (2, 3)
  24. The value of array2 is:
  25. [[1. 1.]
  26. [1. 1.]
  27. [1. 1.]]
  28. The shape of array2 is:
  29. (3, 2)
  30. """

从上面的结果我们可以看出,从左向右,第一个参数代表的是(row), 第二个参数代表的是列(column)。我们知道numpy中默认的是笛卡尔坐标系,所以横向为x,纵向为y,具体的请看坐标系(超链接点击跳转查看)。所以对self.array1来说,定义时输入的数组大小的(2, 3)代表沿着x轴拥有3个值,沿着y轴拥有2个值。对比上述得到的结果与我们在一维情况中推断得到的结果,证明我们的理解是正确的。

接着我们讨论三维的情况:代码如下:

  1. import numpy as np
  2. class Debug:
  3. def __init__(self):
  4. self.array1 = np.ones((2, 3, 4))
  5. def mainProgram(self):
  6. print("The value of array1 is: ")
  7. print(self.array1)
  8. print("The shape of array1 is: ")
  9. print(self.array1.shape)
  10. if __name__ == '__main__':
  11. main = Debug()
  12. main.mainProgram()
  13. """
  14. The value of array1 is:
  15. [[[1. 1. 1. 1.]
  16. [1. 1. 1. 1.]
  17. [1. 1. 1. 1.]]
  18. [[1. 1. 1. 1.]
  19. [1. 1. 1. 1.]
  20. [1. 1. 1. 1.]]]
  21. The shape of array1 is:
  22. (2, 3, 4)
  23. """

不难发现,沿着x轴方向拥有4个值,沿着y轴方向拥有3个值,沿着z轴方向拥有2个值。

以上就是numpy数组坐标轴问题如何解决的详细内容,更多关于numpy数组坐标轴问题如何解决的资料请关注九品源码其它相关文章!