Extract complex real components
Separate amplitudes from complex-valued numerical data.
NumPy · Pipe
Returns the real part of an ndarray. It usually provides a view into the source array, while object arrays use each element’s .real value.
Accessing ndarray.real extracts the real component of every array element. For most numeric arrays it usually provides a view into the original array; object arrays instead obtain each element’s .real value individually.
ndarray.realThe real part of the array; usually a view for ordinary numeric arrays and elementwise .real values for object arrays.
import numpy as np
x = np.sqrt([1 + 0j, 0 + 1j])
real_parts = x.real
print(real_parts)
# [1. 0.70710678]
print(real_parts.dtype)
# float64Extracts the real component of each complex array element.
Separate amplitudes from complex-valued numerical data.
Inspect the real values produced by complex-valued NumPy expressions.
Use the array’s real component without an explicit conversion function.
ndarray.real is an attribute that returns the array’s real part. For a complex array, each value contributes its real component while preserving the array’s shape.
Follow-up: How does it behave for object arrays? It evaluates .real elementwise rather than using the usual view behavior.
For ordinary numeric arrays, ndarray.real usually returns a view into the original array rather than copying the data. This can make access efficient and means the result may share storage with the source.
Follow-up: What exception does the documentation identify? Object arrays return elementwise .real values instead of the usual view into the original array.
The attribute is accessed as x.real, where x is an ndarray. It is useful for separating real components from complex-valued data without calling a separate function.
Follow-up: What equivalent function does NumPy document? numpy.real is the equivalent function.
numpy.realndarray.imag