Python Numpy中的组合学
我想编写一个单个功能,以输出2个矩阵的所有可能组合:
def combine(*args):
return np.array(np.meshgrid(args)).T.reshape(-1, len(args)+1)
但是当传递时:
print(combine(np.array([1,2,3]), np.array([4,5,6])))
输出:
[[1 2 3]
[4 5 6]]
如何使其工作?我想保持它自动,而不是简单地通过(args [0],args [1])
I'd like to write a single function that outputs all the possible combinations of 2 matrixes:
def combine(*args):
return np.array(np.meshgrid(args)).T.reshape(-1, len(args)+1)
However when passed:
print(combine(np.array([1,2,3]), np.array([4,5,6])))
It outputs:
[[1 2 3]
[4 5 6]]
How can I make it work? I would like to keep it automatic, not to simply pass (args[0], args[1])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Python Itertools的直接使用:
或作为数组:
A straight forward use of python itertools:
or as array: