2015-01-17 5 views
9

NumPy "구조적 배열", "레코드 배열"및 "재 배열"의 차이점은 무엇입니까?NumPy "레코드 배열"또는 "구조적 배열"또는 "재 배열"

NumPy docs은 처음 두 개가 동일 함을 의미합니다. 일치하는 경우이 개체에 대한 선호 용어는 무엇입니까?

같은 문서에서 (페이지 하단에) 재 배열 및 구조 배열 (두 가지 차이 포함)에 대한 자세한 정보는 here을 참조하십시오. 이 차이점에 대한 간단한 설명이 있습니까?

+0

[구조 배열 (일명 "레코드 배열")] (http://docs.scipy.org/doc/numpy/user/basics.rec.html) –

+0

@Ashwini Chaudhary 질문을 명확히했습니다. . – xnx

+0

문서의 차이에 대한 설명이 명확하지 않습니다. recarray는'arr.foo' 형식의 필드에 대한 액세스를 지원하는 반면, 일반적인 구조화 된 배열은'arr [ 'foo']'형식을 통해서만 액세스를 지원하지만 조회하는 것이 더 빠릅니다. 필자는 "구조화 된 배열"을 "레코드 배열"이라고 부르지 않을 것입니다. 왜냐하면 "재 배열"에 대해 많은 혼란을 일으킬 수 있기 때문입니다. – zehnpaard

답변

7

기록은/recarrays는

기록 배열 기록 배열 속성으로 구조화 된 배열의 필드를 노출이 파일에서

https://github.com/numpy/numpy/blob/master/numpy/core/records.py

일부 관련 따옴표로 구현됩니다. 재 배열은 표준 배열과 거의 같습니다 ( 명명 된 필드를 이미 지원함). 가장 큰 차이점은 속성 - 검색을 사용하여 필드를 찾고 레코드를 사용하여 구성된다는 것입니다.

recarray은 (matrixmasked arrays는 것과 같은 방식으로) ndarray의 서브 클래스이다. 그러나 생성자가 np.array과 다릅니다. 그것은 np.empty(size, dtype)과 같습니다.

class recarray(ndarray): 
    """Construct an ndarray that allows field access using attributes. 
    This constructor can be compared to ``empty``: it creates a new record 
     array but does not fill it with data. 

속성 동작으로 고유 필드를 구현하기위한 핵심 기능을 __getattribute__입니다 (__getitem__ 구현하는 색인) : - .data, .strides, 일을 .shape처럼

def __getattribute__(self, attr): 
    # See if ndarray has this attr, and return it if so. (note that this 
    # means a field with the same name as an ndarray attr cannot be 
    # accessed by attribute). 
    try: 
     return object.__getattribute__(self, attr) 
    except AttributeError: # attr must be a fieldname 
     pass 

    # look for a field with this name 
    fielddict = ndarray.__getattribute__(self, 'dtype').fields 
    try: 
     res = fielddict[attr][:2] 
    except (TypeError, KeyError): 
     raise AttributeError("recarray has no attribute %s" % attr) 
    obj = self.getfield(*res) 

    # At this point obj will always be a recarray, since (see 
    # PyArray_GetField) the type of obj is inherited. Next, if obj.dtype is 
    # non-structured, convert it to an ndarray. If obj is structured leave 
    # it as a recarray, but make sure to convert to the same dtype.type (eg 
    # to preserve numpy.record type if present), since nested structured 
    # fields do not inherit type. 
    if obj.dtype.fields: 
     return obj.view(dtype=(self.dtype.type, obj.dtype.fields)) 
    else: 
     return obj.view(ndarray) 

그것은 먼저는 일반 속성을 가져 오려고 시도 뿐만 아니라 모든 방법 (.sum, .reshape, 등). 실패하면 필드 이름에 dtype의 이름을 찾습니다. 그래서 이것은 몇 가지 재정의 된 접근 방법을 가진 구조화 된 배열입니다.

나는 record arrayrecarray이 가장 똑같다고 말할 수 있습니다.

또 다른 파일은 구조화 된 배열을 조작 할 수있는 역사 유틸리티

https://github.com/numpy/numpy/blob/master/numpy/lib/recfunctions.py

컬렉션의 무언가를 보여줍니다. 이러한 기능의 대부분은 matplotlib의 John Hunter에 의해 처음 구현되었습니다. 편의를 위해 다시 작성되고 확장되었습니다.

if asrecarray: 
     output = output.view(recarray) 

recarray보기 '얇은'이 레이어는 방법을 보여줍니다 당신이 배열을 반환 할 수 있다는 사실을이 파일의 기능

많은 사람들로 끝납니다.

numpy은 오랜 역사를 가지고 있으며 몇 가지 독립적 인 프로젝트를 병합합니다. 제 생각에 recarray은 더 오래된 아이디어이고 구조화 된 배열은 일반화 된 dtype을 기반으로 구현 된 현재 구현입니다. recarrays은 새로운 개발보다 편리하고 이전 버전과의 호환성을 위해 유지되는 것으로 보입니다. 그러나 나는 github 파일 기록을 연구해야하며, 최근의 모든 문제/확실한 요청을 풀어 야합니다.