Array creation routines
- 이 질문을 이해하기 위해서 np.ma.asarray와 np.asanyarray의 차이를 찾아보았습니다
MaskedArray Class는 missing data를 다루기 위한 ndarray의 subclass입니다.
MaskedArray는 일반적인 np.ndarray로 된 data 부분과 해당 data의 boolean mask부분으로 되어있습니다.
np.asarray
: Convert the input to an array.
: No copy is performed if the input is already an ndarray with matching dtype and order.
: If a is a subclass of ndarray, a base class ndarray is returned.
np.asanyarray
: Convert the input to an ndarray, but pass ndarray subclasses through.
: If a is an ndarray or a subclass of ndarray, it is returned as-is and no copy is performed.
np.ma.asarray
: Convert the input to a masked array of the given data-type.
: No copy is performed if the input is already an
ndarray
. If a is a subclass ofMaskedArray
, a base classMaskedArray
is returned.np.ma.asanyarray
: Convert the input to a masked array, conserving subclasses.
: If
a
is a subclass ofMaskedArray
, its class is conserved. No copy is performed if the input is already anndarray
.→ code를 보면
subok=True
condition에서 asarry와 차이가 있는 것을 확인할 수 있습니다.
- Example
MaskedArray를 input으로 주었을 때,
- np.asarray는 ndarray를 return하고,
- np.asanyarray, np.ma.asarry, np.ma.asanyarray는 MaskedArray를 return하는 것을 확인할 수 있습니다.
asanyarray의 경우 input이 MaskedArray인 경우 아래와 같이 input을 그대로 반환하기에 is 비교시 True값을 얻을 수 있었습니다.
1
2
3
4
def asanyarray(a, dtype=None):
if isinstance(a, MaskedArray) and (dtype is None or dtype == a.dtype):
return a
return masked_array(a, dtype=dtype, copy=False, keep_mask=True, subok=True)