Python Mentorship Program Lecture

Jul 16, 2024

Python Mentorship Program Lecture Notes

Introduction and Admin

  • Welcome and program update: Completed month 1 (November) covering Python.
  • Feedback Form: Provided a link for feedback to improve the program.
  • New initiative: One-on-one doubt clearance via Zoom introduced, fill Google Form for booking.
  • Membership move: Request to cancel YouTube membership and switch to the new website subscription; YouTube auto-deducts money which cannot be refunded.

Today's Session Outline

  • Continuation from last session on NumPy fundamentals, advancing into conceptual parts.
  • Hands-on session with key topics including NumPy arrays, comparisons, indexing, broadcasting, and plotting.

NumPy vs Python List

  • Speed of Execution: NumPy is almost 50 times faster than Python lists due to its use of C-type arrays.
  • Memory Usage: NumPy uses less memory, and memory usage can be optimized further by using different integer types like int16, int32, etc.
  • Convenience: NumPy provides a more convenient syntax for array operations.

Advanced Indexing

  • Fancy Indexing: Allows extraction of array elements using lists of indices.

    a = np.array([...])
    b = a[[0, 2, 3]]  # extract specific rows like 0th, 2nd, and 3rd
    
  • Boolean Indexing: Extracts elements based on conditions.

    mask = a > 50  # condition
    result = a[mask]  # filtered array
    

    Example combining conditions:

    mask = (a > 50) & (a % 2 == 0)
    result = a[mask]
    

Broadcasting

  • Concept: Different-shaped arrays can be used together in arithmetic operations if they satisfy broadcasting rules.
  • Rules:
    1. Make arrays have the same number of dimensions by prepending 1s to the smaller array.
    2. Compare shapes: if sizes differ but one is 1, stretch it.
    3. If dimensions mismatch and none is 1, broadcasting fails.

Creating Custom Mathematical Functions

  • Sigmoid Function:

    def sigmoid(x):
        return 1 / (1 + np.exp(-x))
    result = sigmoid(np.array([1, 2, 3]))
    
  • Mean Square Error (MSE):

    def mse(actual, predicted):
        return np.mean((actual - predicted) ** 2)
    result = mse(np.array([...]), np.array([...]))
    

Handling Missing Values

  • Creating NaN values:

    a = np.array([1, 2, np.nan, 4])
    
  • Filtering out NaN values:

    result = a[~np.isnan(a)]  # removes NaNs
    

Plotting Graphs with Matplotlib

  • Basic Plotting: Plotting simple functions using Matplotlib.

    import matplotlib.pyplot as plt
    x = np.linspace(-10, 10, 100)
    y = x
    plt.plot(x, y)  # linear line
    plt.show()
    

    Example for quadratic:

    y = x ** 2
    plt.plot(x, y)
    plt.show()
    

Conclusion

  • Session Recap: Covered NumPy vs Python list, advanced indexing, broadcasting, custom mathematical functions, handling missing values, and basic plotting with Matplotlib.
  • Feedback Reminder: Fill out the provided feedback form to help improve the program.

Next Steps

  • Complete homework on binary cross-entropy function as discussed.
  • Attend the next session for further advanced topics in NumPy.