numpy:从recarray dtype获取ndarray dtype

时间:2019-03-22 06:08:11

标签: numpy

我有一个由函数返回的recarray,我想将其转换为具有相同dtype的ndarray。示例是以下代码中的z变量。如何从d1的{​​{1}}中获得dtype

z

现在您看到>>> d1 = np.dtype([('a', float), ('b', int)]) >>> x = np.random.randn(4).view(d1); x array([( 1.3209, -1.11 ), (-1.1721, -0.5442)], dtype=[('a', '<f8'), ('b', '<f8')]) >>> y = x.view(np.recarray); y rec.array([( 1.3209, -1.11 ), (-1.1721, -0.5442)], dtype=[('a', '<f8'), ('b', '<f8')]) >>> z = y.view(np.ndarray); z array([( 1.3209, -1.11 ), (-1.1721, -0.5442)], dtype=(numpy.record, [('a', '<f8'), ('b', '<f8')])) z具有相同的数据结构,但具有不同的xdtypez.dtype周围有一个包装np.record。如何删除包装器以获取基础的x.dtype

1 个答案:

答案 0 :(得分:0)

import dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objs as go
import json
import pandas as pd

app = dash.Dash()


#Consider this as the dataframe to be shown in hover
L = ["Text A", "Text B", "Text C", "Text D", "Text E"]
df = pd.DataFrame({'col':L})


# Here write your custom preprocessing function, 
# We can do whatever we want here to truncate the list
# Here every element in the list is truncated to have only 4 characters
def process_list(a):
    return [elem[:4] for elem in a]

app.layout = html.Div([
    dcc.Graph(
        id='life-exp-vs-gdp',
        figure={
            'data': [
                go.Scatter(
                    x = [1,2,3,4,5],
                    y = [2,1,6,4,4],
                    #call the pre processing function with the data frame
                    text = process_list(df['col']),
                    hoverinfo = 'text',
                    marker = dict(
                        color = 'green'
                    ),
                    showlegend = False
                )
            ],
            'layout': go.Layout(
            )
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)

结构化数组

In [19]: d1 = np.dtype([('a',float),('b',float)])                               

rearray视图:

In [20]: x = np.array([(1,2),(3,4),(5,6)], d1)                                  
In [21]: x                                                                      
Out[21]: array([(1., 2.), (3., 4.), (5., 6.)], dtype=[('a', '<f8'), ('b', '<f8')])

In [22]: y = x.view(np.recarray) In [23]: y Out[23]: rec.array([(1., 2.), (3., 4.), (5., 6.)], dtype=[('a', '<f8'), ('b', '<f8')])

dtype

In [24]: x.dtype Out[24]: dtype([('a', '<f8'), ('b', '<f8')]) In [25]: y.dtype Out[25]: dtype((numpy.record, [('a', '<f8'), ('b', '<f8')])) dtype匹配,因此我们可以做到:

descr

https://docs.scipy.org/doc/numpy/user/basics.rec.html#record-arrays

  

为方便起见,以np.recarray类型查看ndarray将自动转换为np.record数据类型,因此可以将dtype放在视图之外:

     

要返回普通ndarray,必须同时重置dtype和type。考虑到记录器不是结构化类型的异常情况,以下视图这样做了:

In [26]: y.dtype.descr                                                          
Out[26]: [('a', '<f8'), ('b', '<f8')]
In [27]: np.dtype(y.dtype.descr)                                                
Out[27]: dtype([('a', '<f8'), ('b', '<f8')])
In [28]: x.dtype                                                                
Out[28]: dtype([('a', '<f8'), ('b', '<f8')])

===

随着多字段访问方式的变化,In [31]: y.view(y.dtype.fields, np.ndarray) Out[31]: array([(1., 2.), (3., 4.), (5., 6.)], dtype=[('a', '<f8'), ('b', '<f8')]) 添加了一些实用程序功能。一个将结构化转换为非结构化(过去一直有点尴尬):

recfunctions

和另一个方向:

In [35]: import numpy.lib.recfunctions as rf                                    
In [36]: rf.structured_to_unstructured(x)                                       
Out[36]: 
array([[1., 2.],
       [3., 4.],
       [5., 6.]])
In [37]: rf.structured_to_unstructured(y)                                       
Out[37]: 
rec.array([[1., 2.],
           [3., 4.],
           [5., 6.]],
          dtype=float64)
In [38]: rf.structured_to_unstructured(y).view(np.ndarray)                      
Out[38]: 
array([[1., 2.],
       [3., 4.],
       [5., 6.]])