东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 2343|回复: 0
打印 上一主题 下一主题

[课堂笔记] 如何通过np.meshgrid快速形成网格坐标值(对)?

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14435
QQ
跳转到指定楼层
楼主
发表于 2019-11-20 11:34:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如何通过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)



复制代码



让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-26 17:21 , Processed in 0.379968 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表