Real-World Problems Solved Using NumPy

NumPy provides powerful tools to solve real-world problems, including data normalization, solving systems of equations, and signal processing.

1. Data Normalization

Data normalization is essential in machine learning and statistical analysis to ensure that different features contribute equally.

Example: Normalizing a Dataset

    import numpy as np
    
    # Sample dataset
    data = np.array([[100, 200, 300], [400, 500, 600], [700, 800, 900]])
    
    # Min-Max Normalization
    normalized_data = (data - np.min(data, axis=0)) / (np.max(data, axis=0) - np.min(data, axis=0))
    print("Normalized Data:")
    print(normalized_data)
        

Result:

The dataset is normalized, scaling values between 0 and 1.

2. Solving Systems of Equations

Solving linear systems is a common problem in engineering and scientific computing.

Example: Solving Ax = b

    import numpy as np
    
    # Coefficients matrix
    A = np.array([[2, -1], [1, 3]])
    # Constants vector
    b = np.array([1, 12])
    
    # Solving the system
    x = np.linalg.solve(A, b)
    print("Solution:", x)
        

Result:

The values of x are computed, solving the system of equations.

3. Signal Processing

NumPy enables signal processing tasks such as filtering and Fourier analysis.

Example: Fourier Transform

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Generate a sample signal
    t = np.linspace(0, 1, 1000)
    signal = np.sin(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 10 * t)
    
    # Compute FFT
    fft_values = np.fft.fft(signal)
    frequencies = np.fft.fftfreq(len(t), d=t[1] - t[0])
    
    # Plot FFT result
    plt.plot(frequencies[:len(frequencies)//2], np.abs(fft_values[:len(frequencies)//2]))
    plt.xlabel("Frequency (Hz)")
    plt.ylabel("Magnitude")
    plt.title("Fourier Transform of Signal")
    plt.show()
        

Result:

The Fourier Transform reveals the frequency components of the signal.

Conclusion

NumPy effectively handles real-world problems, making it indispensable in data science, engineering, and signal processing applications.





Advertisement