Skip to content

Latest commit

 

History

History
103 lines (68 loc) · 4.01 KB

File metadata and controls

103 lines (68 loc) · 4.01 KB

Numerical Computations with NumPy

NumPy is a powerful library in Python used for numerical computing. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently.

Introduction to NumPy Arrays and Mathematical Operations

NumPy arrays are the core data structure for representing numerical data in NumPy. They are similar to Python lists but offer more functionality and efficiency for numerical computations.

Creating NumPy Arrays

NumPy arrays can be created from Python lists using the np.array() function.

import numpy as np

# Create a 1D array from a Python list
arr1d = np.array([1, 2, 3, 4, 5])

# Create a 2D array from a nested Python list
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print("1D Array:", arr1d)
print("2D Array:", arr2d)
1D Array: [1 2 3 4 5]
2D Array: [[1 2 3]
          [4 5 6]
          [7 8 9]]

Mathematical Operations

NumPy provides a wide range of mathematical functions that can be applied directly to arrays.

Element-wise Operations

# Element-wise addition
result = arr1d + 10

# Element-wise multiplication
result = arr2d * 2

print("Result of addition:", result)

Dot Product

# Dot product of two arrays
dot_product = np.dot(arr1d, arr1d)

print("Dot product:", dot_product)

Universal Functions (ufuncs)

NumPy provides universal functions (ufuncs) for element-wise operations on arrays.

# Square root of elements
sqrt_arr = np.sqrt(arr1d)

# Exponential of elements
exp_arr = np.exp(arr1d)

print("Square root:", sqrt_arr)
print("Exponential:", exp_arr)
Square root: [1.         1.41421356 1.73205081 2.         2.23606798]
Exponential: [  2.71828183   7.3890561   20.08553692  54.59815003 148.4131591 ]

Differences Between Python Lists and NumPy Arrays

  1. Memory Efficiency: NumPy arrays are more memory efficient compared to Python lists, especially when dealing with large datasets. This efficiency comes from the homogeneous data types of NumPy arrays, which allow them to be stored more compactly in memory.

  2. Performance: NumPy arrays offer better performance for numerical computations compared to Python lists. This is because NumPy arrays are implemented in C and optimized for performance, while Python lists are dynamic arrays with more overhead.

  3. Ease of Use: NumPy arrays provide a wide range of mathematical functions and operations that can be applied directly to the arrays. This makes it easier to perform complex numerical computations compared to using Python lists and looping constructs.

  4. Broadcasting: NumPy arrays support broadcasting, which allows operations to be performed on arrays of different shapes. This eliminates the need for explicit looping and makes code more concise and readable.

  5. Vectorized Operations: NumPy arrays support vectorized operations, where mathematical operations are applied element-wise to entire arrays. This makes code more expressive and concise compared to using explicit loops with Python lists.

When to Use NumPy Arrays vs. Python Lists

  • Use NumPy Arrays When:

    • Dealing with large datasets or multi-dimensional data.
    • Performing numerical computations or linear algebra operations.
    • Needing memory efficiency and better performance.
    • Performing element-wise operations or broadcasting.
  • Use Python Lists When:

    • Dealing with small datasets or simple data structures.
    • Needing flexibility in data types or structure.
    • Performing general-purpose programming tasks that don't require numerical computations.

NumPy arrays provide a powerful and efficient way to work with numerical data in Python, offering better performance and memory efficiency compared to Python lists. However, Python lists still have their place in situations where flexibility and simplicity are more important than performance and efficiency.