Numpy meshgrid 保持底层一个数组
Numpy meshgrid keep bottom layer an array
我想做一个meshgrid,但是底层要保持数组。
当我这样做时:
One = np.array([["1A1","1A2"],["1B1","1B2"]])
Two = np.array([["2A1","2A2"],["2B1","2B2"]])
np.array(np.meshgrid(One, Two))
我得到:
array([[['1A1', '1A2', '1B1', '1B2'],
['1A1', '1A2', '1B1', '1B2'],
['1A1', '1A2', '1B1', '1B2'],
['1A1', '1A2', '1B1', '1B2']],
[['2A1', '2A1', '2A1', '2A1'],
['2A2', '2A2', '2A2', '2A2'],
['2B1', '2B1', '2B1', '2B1'],
['2B2', '2B2', '2B2', '2B2']]], dtype='<U3')
但我不想单独获取所有字符串。我希望["1A1","1A2"]
的基本结构不被拆分
基本上,我正在寻找的结果是:
array([[[['1A1', '1A2'],['1B1', '1B2']],
[['1A1', '1A2'],['1B1', '1B2']]],
[[['2A1', '2A2'],['2A1', '2A2']],
[['2B1', '2B2'],['2B1', '2B2']]]], dtype='<U3')
是否有功能或其他方法可以实现?
编辑:
字符串只是为了让它更容易理解。如果有任何其他方法,我将与 ints
一起使用。
您的布局有点不寻常,但这个重复的堆栈似乎可以完成工作:
In [79]: np.stack((np.stack((One,One)), np.stack((Two,Two))))
Out[79]:
array([[[['1A1', '1A2'],
['1B1', '1B2']],
[['1A1', '1A2'],
['1B1', '1B2']]],
[[['2A1', '2A2'],
['2B1', '2B2']],
[['2A1', '2A2'],
['2B1', '2B2']]]], dtype='<U3')
这两个评论(@hpaulj, @Richard K Yu) 都犯了一个小错误,但幸运的是他们犯了不同的错误,所以我可以合并他们的想法来创建一个解决方案。我的解决方案是:
np.array([np.stack((One,One)),np.array(np.meshgrid(*Two))])
但是,如果有人知道更优雅的解决方案,我会很高兴,因为它现在有点笨拙。
另一种方法是创建对象 dtype 数组,其中包含要作为元素保持在一起的单位:
In [82]: One_ = np.empty(2,object)
In [84]: One_[:] = One.tolist()
In [85]: One_
Out[85]: array([list(['1A1', '1A2']), list(['1B1', '1B2'])], dtype=object)
In [86]: Two_ = np.empty(2,object)
In [87]: Two_[:] = Two.tolist()
现在 meshgrid
可以组合它们,就像使用数字或字符串数据类型一样:
In [88]: np.meshgrid(One_, Two_)
Out[88]:
[array([[list(['1A1', '1A2']), list(['1B1', '1B2'])],
[list(['1A1', '1A2']), list(['1B1', '1B2'])]], dtype=object),
array([[list(['2A1', '2A2']), list(['2A1', '2A2'])],
[list(['2B1', '2B2']), list(['2B1', '2B2'])]], dtype=object)]
并作为一个数组:
In [89]: np.stack(_)
Out[89]:
array([[[list(['1A1', '1A2']), list(['1B1', '1B2'])],
[list(['1A1', '1A2']), list(['1B1', '1B2'])]],
[[list(['2A1', '2A2']), list(['2A1', '2A2'])],
[list(['2B1', '2B2']), list(['2B1', '2B2'])]]], dtype=object)
并转换回字符串数组:
In [90]: np.array(_.tolist())
Out[90]:
array([[[['1A1', '1A2'],
['1B1', '1B2']],
[['1A1', '1A2'],
['1B1', '1B2']]],
[[['2A1', '2A2'],
['2A1', '2A2']],
[['2B1', '2B2'],
['2B1', '2B2']]]], dtype='<U3')
或者使用结构化数组视图对两个字符串进行分组:
In [92]: One.view([('f0','U3',(2,))])
Out[92]:
array([[(['1A1', '1A2'],)],
[(['1B1', '1B2'],)]], dtype=[('f0', '<U3', (2,))])
In [93]: Two.view([('f0','U3',(2,))])
Out[93]:
array([[(['2A1', '2A2'],)],
[(['2B1', '2B2'],)]], dtype=[('f0', '<U3', (2,))])
In [94]: np.meshgrid(_92, _93)
Out[94]:
[array([[(['1A1', '1A2'],), (['1B1', '1B2'],)],
[(['1A1', '1A2'],), (['1B1', '1B2'],)]],
dtype=[('f0', '<U3', (2,))]),
array([[(['2A1', '2A2'],), (['2A1', '2A2'],)],
[(['2B1', '2B2'],), (['2B1', '2B2'],)]],
dtype=[('f0', '<U3', (2,))])]
In [95]: np.stack(_)
Out[95]:
array([[[(['1A1', '1A2'],), (['1B1', '1B2'],)],
[(['1A1', '1A2'],), (['1B1', '1B2'],)]],
[[(['2A1', '2A2'],), (['2A1', '2A2'],)],
[(['2B1', '2B2'],), (['2B1', '2B2'],)]]],
dtype=[('f0', '<U3', (2,))])
In [96]: import numpy.lib.recfunctions as rf
In [97]: rf.structured_to_unstructured(_95)
Out[97]:
array([[[['1A1', '1A2'],
['1B1', '1B2']],
[['1A1', '1A2'],
['1B1', '1B2']]],
[[['2A1', '2A2'],
['2A1', '2A2']],
[['2B1', '2B2'],
['2B1', '2B2']]]], dtype='<U3')
我想做一个meshgrid,但是底层要保持数组。
当我这样做时:
One = np.array([["1A1","1A2"],["1B1","1B2"]])
Two = np.array([["2A1","2A2"],["2B1","2B2"]])
np.array(np.meshgrid(One, Two))
我得到:
array([[['1A1', '1A2', '1B1', '1B2'],
['1A1', '1A2', '1B1', '1B2'],
['1A1', '1A2', '1B1', '1B2'],
['1A1', '1A2', '1B1', '1B2']],
[['2A1', '2A1', '2A1', '2A1'],
['2A2', '2A2', '2A2', '2A2'],
['2B1', '2B1', '2B1', '2B1'],
['2B2', '2B2', '2B2', '2B2']]], dtype='<U3')
但我不想单独获取所有字符串。我希望["1A1","1A2"]
的基本结构不被拆分
基本上,我正在寻找的结果是:
array([[[['1A1', '1A2'],['1B1', '1B2']],
[['1A1', '1A2'],['1B1', '1B2']]],
[[['2A1', '2A2'],['2A1', '2A2']],
[['2B1', '2B2'],['2B1', '2B2']]]], dtype='<U3')
是否有功能或其他方法可以实现?
编辑:
字符串只是为了让它更容易理解。如果有任何其他方法,我将与 ints
一起使用。
您的布局有点不寻常,但这个重复的堆栈似乎可以完成工作:
In [79]: np.stack((np.stack((One,One)), np.stack((Two,Two))))
Out[79]:
array([[[['1A1', '1A2'],
['1B1', '1B2']],
[['1A1', '1A2'],
['1B1', '1B2']]],
[[['2A1', '2A2'],
['2B1', '2B2']],
[['2A1', '2A2'],
['2B1', '2B2']]]], dtype='<U3')
这两个评论(@hpaulj, @Richard K Yu) 都犯了一个小错误,但幸运的是他们犯了不同的错误,所以我可以合并他们的想法来创建一个解决方案。我的解决方案是:
np.array([np.stack((One,One)),np.array(np.meshgrid(*Two))])
但是,如果有人知道更优雅的解决方案,我会很高兴,因为它现在有点笨拙。
另一种方法是创建对象 dtype 数组,其中包含要作为元素保持在一起的单位:
In [82]: One_ = np.empty(2,object)
In [84]: One_[:] = One.tolist()
In [85]: One_
Out[85]: array([list(['1A1', '1A2']), list(['1B1', '1B2'])], dtype=object)
In [86]: Two_ = np.empty(2,object)
In [87]: Two_[:] = Two.tolist()
现在 meshgrid
可以组合它们,就像使用数字或字符串数据类型一样:
In [88]: np.meshgrid(One_, Two_)
Out[88]:
[array([[list(['1A1', '1A2']), list(['1B1', '1B2'])],
[list(['1A1', '1A2']), list(['1B1', '1B2'])]], dtype=object),
array([[list(['2A1', '2A2']), list(['2A1', '2A2'])],
[list(['2B1', '2B2']), list(['2B1', '2B2'])]], dtype=object)]
并作为一个数组:
In [89]: np.stack(_)
Out[89]:
array([[[list(['1A1', '1A2']), list(['1B1', '1B2'])],
[list(['1A1', '1A2']), list(['1B1', '1B2'])]],
[[list(['2A1', '2A2']), list(['2A1', '2A2'])],
[list(['2B1', '2B2']), list(['2B1', '2B2'])]]], dtype=object)
并转换回字符串数组:
In [90]: np.array(_.tolist())
Out[90]:
array([[[['1A1', '1A2'],
['1B1', '1B2']],
[['1A1', '1A2'],
['1B1', '1B2']]],
[[['2A1', '2A2'],
['2A1', '2A2']],
[['2B1', '2B2'],
['2B1', '2B2']]]], dtype='<U3')
或者使用结构化数组视图对两个字符串进行分组:
In [92]: One.view([('f0','U3',(2,))])
Out[92]:
array([[(['1A1', '1A2'],)],
[(['1B1', '1B2'],)]], dtype=[('f0', '<U3', (2,))])
In [93]: Two.view([('f0','U3',(2,))])
Out[93]:
array([[(['2A1', '2A2'],)],
[(['2B1', '2B2'],)]], dtype=[('f0', '<U3', (2,))])
In [94]: np.meshgrid(_92, _93)
Out[94]:
[array([[(['1A1', '1A2'],), (['1B1', '1B2'],)],
[(['1A1', '1A2'],), (['1B1', '1B2'],)]],
dtype=[('f0', '<U3', (2,))]),
array([[(['2A1', '2A2'],), (['2A1', '2A2'],)],
[(['2B1', '2B2'],), (['2B1', '2B2'],)]],
dtype=[('f0', '<U3', (2,))])]
In [95]: np.stack(_)
Out[95]:
array([[[(['1A1', '1A2'],), (['1B1', '1B2'],)],
[(['1A1', '1A2'],), (['1B1', '1B2'],)]],
[[(['2A1', '2A2'],), (['2A1', '2A2'],)],
[(['2B1', '2B2'],), (['2B1', '2B2'],)]]],
dtype=[('f0', '<U3', (2,))])
In [96]: import numpy.lib.recfunctions as rf
In [97]: rf.structured_to_unstructured(_95)
Out[97]:
array([[[['1A1', '1A2'],
['1B1', '1B2']],
[['1A1', '1A2'],
['1B1', '1B2']]],
[[['2A1', '2A2'],
['2A1', '2A2']],
[['2B1', '2B2'],
['2B1', '2B2']]]], dtype='<U3')