简体   繁体   English

Numpy meshgrid 保持底层数组

[英]Numpy meshgrid keep bottom layer an array

I want to make a meshgrid, but keep the bottom layer an array.我想制作一个网格,但将底层保留为一个数组。

When I do this:当我这样做时:

One = np.array([["1A1","1A2"],["1B1","1B2"]])
Two = np.array([["2A1","2A2"],["2B1","2B2"]])
np.array(np.meshgrid(One, Two))

I get:我得到:

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')

But I don't want to get all the Strings separately.但我不想单独获取所有字符串。 I want the basic structure of ["1A1","1A2"] not to be split up.我希望["1A1","1A2"]的基本结构不被拆分。

Basically, what I am looking for is something, that results in this:基本上,我正在寻找的是一些东西,结果是:

array([[[['1A1', '1A2'],['1B1', '1B2']],
        [['1A1', '1A2'],['1B1', '1B2']]],

       [[['2A1', '2A2'],['2A1', '2A2']],
        [['2B1', '2B2'],['2B1', '2B2']]]], dtype='<U3')

Is there a function or other way to achieve that?是否有 function 或其他方式来实现?

EDIT: The strings are just there to make it easier to understand.编辑:字符串只是为了让它更容易理解。 I will use this with ints if there are any other methods.如果有任何其他方法,我会将它与ints一起使用。

Another approach is to make object dtype arrays with the units you want to keep together as elements:另一种方法是使 object dtype arrays 与您要作为元素保持在一起的单位:

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()                                                                      

Now meshgrid can combine them, just as it would with numeric or string dtypes:现在meshgrid可以组合它们,就像它与数字或字符串 dtypes 一样:

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)]

and as one array:并作为一个数组:

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)

And conversion back to string array:并转换回字符串数组:

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')

Or use a structured array view to group the two strings:或者使用结构化数组视图对两个字符串进行分组:

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')

Here is my attempt.这是我的尝试。 I split into four of the "basic structures" that you wanted, applied meshgrid separately to pairs, and then put them all into one array:我分成了四个你想要的“基本结构”,分别将网格网格应用于对,然后将它们全部放入一个数组中:

import numpy as np

one = np.array(["1A1","1A2"])
two = np.array(["1B1","1B2"])
three = np.array(["2A1","2A2"])
four = np.array(["2B1","2B2"])

y = np.array(np.meshgrid(one,two))
y_2 = np.array(np.meshgrid(three,four))
y_3 = np.array([y,y_2])

print(y_3, type(y_3))

Here is the output.这是 output。 I believe it is an array and the inner elements should be dtype '<U3'.我相信它是一个数组,内部元素应该是 dtype '<U3'。 在此处输入图像描述

Your layout is a bit unusual, but this repeated stack seems to do the job:您的布局有点不寻常,但这个重复的堆栈似乎可以完成这项工作:

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')

The two comments(@hpaulj, @Richard K Yu) that were made both had a small mistake, but fortunately they made a different mistake, so I could merge their ideas to create a solution.提出的两条评论(@hpaulj,@Richard K Yu)都有一个小错误,但幸运的是他们犯了一个不同的错误,所以我可以合并他们的想法来创建一个解决方案。 My solution is:我的解决方案是:

np.array([np.stack((One,One)),np.array(np.meshgrid(*Two))])

However, if someone knows a more elegant solution, I would be very happy, because it is kind of clumpy at the moment.但是,如果有人知道更优雅的解决方案,我会很高兴,因为目前它有点笨拙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM