Python Refresher
NumPy & Matplotlib
Standard Python Lists are great, but they are too slow for processing thousands of data points or performing matrix math. This is where NumPy comes in.
NumPy: Numerical Python
NumPy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays.
Installing NumPy
pip install numpy
Creating NumPy Arrays
1import numpy as np23# Convert a standard Python list to a NumPy array4python_list = [1, 2, 3, 4, 5]5np_array = np.array(python_list)67print(type(np_array)) # <class 'numpy.ndarray'>Why NumPy is Faster
NumPy arrays are stored in contiguous memory (like C arrays), whereas Python lists are arrays of pointers to objects scattered in memory. This allows NumPy to perform operations on the entire array at once (vectorization), written in highly optimized C code under the hood.
1# Doing math on a list (requires a loop)2my_list = [1, 2, 3]3new_list = [x * 2 for x in my_list] 45# Doing math on a NumPy array (vectorized, no loop needed)6my_array = np.array([1, 2, 3])7new_array = my_array * 2 # Result: [2, 4, 6]Common NumPy Functions
1# Create an array of 10 zeros2zeros = np.zeros(10)34# Create an array from 0 to 995sequence = np.arange(100)67# Calculate statistics instantly8data = np.array([12, 15, 14, 18, 22])9print(np.mean(data))10print(np.max(data))Matplotlib: Visualizing Data
Before training a machine learning model, you must understand your data. Visualizing it is the best way to spot anomalies, noise, or patterns.
Installing Matplotlib
pip install matplotlib
Plotting a Simple Graph
1import matplotlib.pyplot as plt2import numpy as np34# Create some sample data5x = np.arange(0, 10, 0.1)6y = np.sin(x)78# Create the plot9plt.plot(x, y)10plt.title("Sine Wave")11plt.xlabel("Time")12plt.ylabel("Amplitude")1314# Show the plot15plt.show()In the upcoming Machine Learning sections, we will use NumPy to shape our sensor data and Matplotlib to visualize our model's performance.

