|
如何通过np.meshgrid快速形成网格坐标值(对)?
- # -*- coding: utf-8 -*-
- __author__ = u'东方耀 微信:dfy_88888'
- __date__ = '2019/11/18 8:14'
- __product__ = 'PyCharm'
- __filename__ = 'dfy_test_demo'
- import numpy as np
- cx = np.linspace(11, 30, 20)
- cy = np.linspace(1, 10, 10)
- print('所有x轴上的点的值:', cx, '共有多少个:', len(cx))
- print('所有y轴上的点的值:', cy, '共有多少个:', len(cy))
- cx_grid, cy_grid = np.meshgrid(cx, cy)
- print('开始形成网格meshgrid的坐标值(对):')
- print(cx_grid.shape)
- print(cx_grid[:2, :])
- print(cy_grid.shape)
- print(cy_grid[:2, :])
- print('x轴与y轴点相互可形成的坐标总数为:', cx_grid.size)
- print('x轴与y轴点相互可形成的坐标总数为:', cy_grid.size)
- print('第一种方法:')
- coords = np.dstack((cx_grid.flat, cy_grid.flat))[0]
- print(coords)
- print(coords.shape)
- print('第二种方法:开始打印出每个坐标的具体值(对):')
- cx_grid_vector = np.reshape(cx_grid, (-1, ))
- cy_grid_vector = np.reshape(cy_grid, (-1, ))
- for x, y in zip(cx_grid_vector, cy_grid_vector):
- print('横坐标为:%d, 纵坐标为:%d' % (x, y))
- cx_grid = np.expand_dims(cx_grid, -1) # 为了如下的 np.tile() 做准备
- cy_grid = np.expand_dims(cy_grid, -1) # 为了如下的 np.tile() 做准备
- print(cx_grid.shape)
- print(cy_grid.shape)
- # np.tile(A,reps)贴瓷砖:通过在对应的维度axis上重复rep给定的次数来构造数组
- cx_res = np.tile(cx_grid, reps=(2, 3, 4))
- cy_res = np.tile(cy_grid, reps=(1, 1, 4))
- print(cx_res.shape)
- print(cy_res.shape)
复制代码
|
|