scipy and preserving mat file (.mat matlab data file) structure

前端 未结 2 751
旧巷少年郎
旧巷少年郎 2021-01-28 15:59

After referring to scipy and numpy docs for a day and a half, I tried doing this -

dt = {\'names\':[u\'OSversInt\',u\'Desc\',u\'OSversStr\',\\
... u\'OSname\',u         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-28 16:16

    Based on hpaulj's comment it appears you may be looking for

    aa = np.empty((1,1), dtype='O')
    aa[0,0] = np.array([[ ([[15]], [], [u'5.0.1'], [u'Android'], [u'main'], [u'MSM8960'])]], 
              dtype=[('OSversInt', 'O'), ('Desc', 'O'), ('OSversStr', 'O'),
                     ('OSname', 'O'), ('platform', 'O'), ('Board', 'O')])
    

    which yields

    In [39]: aa
    Out[39]: 
    array([[ array([[([[15]], [], [u'5.0.1'], [u'Android'], [u'main'], [u'MSM8960'])]], 
          dtype=[('OSversInt', 'O'), ('Desc', 'O'), ('OSversStr', 'O'), ('OSname', 'O'), ('platform', 'O'), ('Board', 'O')])]], dtype=object)
    

    When you want to place an arbitrary Python object (such as a NumPy array or a list) inside of a NumPy array, the dtype must be object. In this case construction with np.array fails because this function interprets inner sequences (other than tuples) as values to be recursed upon instead of as atomic elements.

    So the trick to creating these nested object arrays is to create the outer object array first:

    aa = np.empty((1,1), dtype='O')
    

    and then at assign the desired value to the cells of the array:

    aa[0,0] = ...
    

    Note that nested NumPy arrays of dtype object do not allow you to take advantage of NumPy's fast (mainly numeric) functions. They have essentially the same memory footprint as the Python objects they contain, and performance-wise they are typically no better than plain Python lists of lists.

提交回复
热议问题