Records/recarrays sono implementati in
https://github.com/numpy/numpy/blob/master/numpy/core/records.py
Alcune citazioni rilevanti da questo file
Registra Array Registra array espongono i campi di matrici strutturate come proprietà. Il recarray è quasi identico a un array standard (che supporta già i campi denominati ) La più grande differenza è che può utilizzare la ricerca di attributi per trovare i campi ed è creata utilizzando un record .
recarray
è una sottoclasse di ndarray
(nello stesso modo in cui matrix
e masked arrays
sono). Notare che il costruttore è diverso da np.array
. È più simile a 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.
La funzione chiave per l'attuazione del campo unico come un comportamento attributo è __getattribute__
(__getitem__
implementa l'indicizzazione):
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)
E 'un primo momento cerca di ottenere un attributo regolare - le cose come .shape
, .strides
, .data
, così come tutti i metodi (.sum
, .reshape
, ecc.). In caso contrario, cerca il nome nei nomi dei campi dtype
. Quindi è davvero solo una matrice strutturata con alcuni metodi di accesso ridefiniti.
Come meglio posso dire record array
e recarray
sono gli stessi.
Un altro file mostra qualcosa della storia
https://github.com/numpy/numpy/blob/master/numpy/lib/recfunctions.py
raccolta di utility per manipolare gli array strutturati. La maggior parte di queste funzioni è stata inizialmente implementata da John Hunter per il modello matplotlib. Sono stati riscritti ed estesi per comodità.
Molte delle funzioni in questo file terminano con:
if asrecarray:
output = output.view(recarray)
Il fatto che è possibile restituire un array come recarray
vista mostra come 'sottile' questo strato è.
numpy
ha una lunga storia e unisce diversi progetti indipendenti. La mia impressione è che l'recarray
sia un'idea più vecchia e gli array strutturati l'attuale implementazione basata su un numero generalizzato dtype
. recarrays
sembrano essere conservati per comodità e compatibilità con le versioni precedenti di qualsiasi nuovo sviluppo. Ma dovrei studiare la cronologia dei file github
e eventuali problemi recenti/richieste di pull per essere sicuro.
[array strutturati (aka “Record array”)]
(http://docs.scipy.org/doc/numpy/user/basics.rec.html) –
ho chiarito la questione, @Ashwini Chaudhary - grazie . – xnx
Cosa non è chiaro sulla spiegazione della differenza sui documenti? Recarray supporta l'accesso ai campi nel formato 'arr.foo', mentre i normali array strutturati supportano l'accesso solo tramite il formato' arr ['foo'] ', ma è più veloce da cercare. Non chiamerei mai "array strutturati" "array di registrazione", proprio perché causa così tanta potenziale confusione con i "recray". – zehnpaard