NumPy – Using ndarray.real attribute (4 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Overview

Understanding the ndarray.real attribute in NumPy is crucial for anyone delving into data analysis, signal processing, or any computational task involving complex numbers. This tutorial takes you through the basics of ndarray.real to more applied examples, ensuring you come away with a solid grasp of its functionality and applications.

What is ndarray.real Used for?

Before diving into examples, it’s important to understand what ndarray.real does. In NumPy, an ndarray object can store complex numbers, which have both real and imaginary parts. The .real attribute allows access to the real part of the complex numbers contained within an array, effectively separating the complex value into its real component.

Example 1: Basic Usage of ndarray.real

import numpy as np

# Creating a complex numpy array
complex_array = np.array([1+2j, 3+4j, 5+6j])

# Accessing the real part of the complex array
real_part = complex_array.real

# Output
print(real_part)

The output for this example would be:

[1. 3. 5.]

This simple example serves as an introduction to accessing the real parts of complex numbers within an array. Moving on, we’ll explore more nuanced applications.

Example 2: Combining Real Parts with Mathematical Operations

import numpy as np

# Create two complex arrays
array1 = np.array([2+3j, 4+5j])
array2 = np.array([1-1j, 2-2j])

# Access the real parts
real1 = array1.real
real2 = array2.real

# Perform an operation on the real parts
result = real1 + real2

# Output
print(result)

The output will be:

[3. 6.]

This example illustrates how you can manipulate the real parts of complex numbers for computations that require only real-number operations.

Example 3: Using ndarray.real in Data Processing

Complex numbers often arise in data related to signals, like in Fourier transforms. Let’s see how ndarray.real can be applied in extracting real components from such complex data sets.

import numpy as np
from scipy.fft import fft

# Generate a simple signal
signal = np.array([0, 1, 0, -1])

# Compute the Fourier transform
fft_result = fft(signal)

# Access the real parts for analysis
real_fft = fft_result.real

# Output for demonstration
print(real_fft)

Here, the output would show the real parts of the Fourier coefficients, which hold valuable information about the signal’s frequency components:

[0. 0. 0. 0.]

Example 4: Advanced Data Analysis Using ndarray.real

Finally, let’s utilize ndarray.real in a more advanced scenario, analyzing a set of complex data representing electrical impedance measurements in a research scenario. This example demonstrates the practicality of separating and analyzing real components in complex datasets.

import numpy as np
import matplotlib.pyplot as plt

# Simulated complex dataset representing impedance
impedance_data = np.array([10+1j*10, 20+2j*10, 30+3j*10])

# Extract real part: resistance
resistance = impedance_data.real

# Plotting
plt.plot(resistance, 'o-', label='Resistance')
plt.xlabel('Sample')
plt.ylabel('Resistance (Ohms)')
plt.title('Resistance Extracted from Impedance Data')
plt.legend()
plt.show()

This visualization of the real part (resistance) extracted from complex impedance data underscores the utility of ndarray.real in scientific and engineering applications, particularly where complex numbers are standard.

Conclusion

The ndarray.real attribute is a powerful tool in NumPy for handling complex numbers by accessing their real parts. Through examples ranging from simple array manipulations to advanced data analysis, we’ve seen how ndarray.real can be applied in various contexts to simplify computations and clarify data insights. Mastery of these techniques enhances your data analysis arsenal, especially in fields rich with complex numerical data.