东方耀AI技术分享

标题: 如何通过np.meshgrid快速形成网格坐标值(对)? [打印本页]

作者: 东方耀    时间: 2019-11-20 11:34
标题: 如何通过np.meshgrid快速形成网格坐标值(对)?
如何通过np.meshgrid快速形成网格坐标值(对)?
  1. # -*- coding: utf-8 -*-
  2. __author__ = u'东方耀 微信:dfy_88888'
  3. __date__ = '2019/11/18 8:14'
  4. __product__ = 'PyCharm'
  5. __filename__ = 'dfy_test_demo'

  6. import numpy as np

  7. cx = np.linspace(11, 30, 20)
  8. cy = np.linspace(1, 10, 10)
  9. print('所有x轴上的点的值:', cx, '共有多少个:', len(cx))
  10. print('所有y轴上的点的值:', cy, '共有多少个:', len(cy))

  11. cx_grid, cy_grid = np.meshgrid(cx, cy)
  12. print('开始形成网格meshgrid的坐标值(对):')
  13. print(cx_grid.shape)
  14. print(cx_grid[:2, :])

  15. print(cy_grid.shape)
  16. print(cy_grid[:2, :])
  17. print('x轴与y轴点相互可形成的坐标总数为:', cx_grid.size)
  18. print('x轴与y轴点相互可形成的坐标总数为:', cy_grid.size)
  19. print('第一种方法:')
  20. coords = np.dstack((cx_grid.flat, cy_grid.flat))[0]
  21. print(coords)
  22. print(coords.shape)
  23. print('第二种方法:开始打印出每个坐标的具体值(对):')
  24. cx_grid_vector = np.reshape(cx_grid, (-1, ))
  25. cy_grid_vector = np.reshape(cy_grid, (-1, ))
  26. for x, y in zip(cx_grid_vector, cy_grid_vector):
  27.     print('横坐标为:%d, 纵坐标为:%d' % (x, y))


  28. cx_grid = np.expand_dims(cx_grid, -1)  # 为了如下的 np.tile() 做准备
  29. cy_grid = np.expand_dims(cy_grid, -1)  # 为了如下的 np.tile() 做准备
  30. print(cx_grid.shape)
  31. print(cy_grid.shape)
  32. # np.tile(A,reps)贴瓷砖:通过在对应的维度axis上重复rep给定的次数来构造数组

  33. cx_res = np.tile(cx_grid, reps=(2, 3, 4))
  34. cy_res = np.tile(cy_grid, reps=(1, 1, 4))
  35. print(cx_res.shape)
  36. print(cy_res.shape)



复制代码








欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/) Powered by Discuz! X3.4