python - 矩阵,其中行是网格网格点的坐标
问题描述
在本教程中,我找到了类似的代码
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
Y, X = np.meshgrid(y, x)
xy = np.vstack([X.ravel(), Y.ravel()]).T
print(xy)
难道没有比使用meshgrid
+ vstack
+ ravel
+更短的方法来获得行是网格点坐标的矩阵transpose
吗?
输出:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
...
解决方案
您可以通过使用and的笛卡尔积来更直接地跳过meshgrid
并获得您想要的内容:x
y
from itertools import product
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.array(list(product(x,y)))
print(xy)
输出:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
[-4.47368421 -0.55555556]
[-4.47368421 -0.33333333]
...
]
推荐阅读
- python - AttributeError:“hyperopt_estimator”对象没有属性“_best_preprocs”
- javascript - 出现错误:没有“App.js”的默认导出来呈现
- scala - Scala高阶函数:下划线(_)的问题
- ios - SKReceiptRefreshRequest 是否仅限于测试环境,或者我可以在应用程序的最终版本中调用它吗?
- r - 更新现有绘图而不是在 for 循环中创建新绘图
- python - 无法在 mac os 中卸载 python 2.7
- bash - 删除少于三行的文本文件
- java - 运行 spring-boot 应用程序时出错。调用目标异常
- xamarin.android - Xamarin.Android 存档未出现
- c# - 如何修复:字符串中每个单词的首字母大写 - 一个字母单词除外