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

bash
pip install numpy

Creating NumPy Arrays

python
1import numpy as np
2
3# Convert a standard Python list to a NumPy array
4python_list = [1, 2, 3, 4, 5]
5np_array = np.array(python_list)
6
7print(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.

python
1# Doing math on a list (requires a loop)
2my_list = [1, 2, 3]
3new_list = [x * 2 for x in my_list]
4
5# 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

python
1# Create an array of 10 zeros
2zeros = np.zeros(10)
3
4# Create an array from 0 to 99
5sequence = np.arange(100)
6
7# Calculate statistics instantly
8data = 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

bash
pip install matplotlib

Plotting a Simple Graph

python
1import matplotlib.pyplot as plt
2import numpy as np
3
4# Create some sample data
5x = np.arange(0, 10, 0.1)
6y = np.sin(x)
7
8# Create the plot
9plt.plot(x, y)
10plt.title("Sine Wave")
11plt.xlabel("Time")
12plt.ylabel("Amplitude")
13
14# Show the plot
15plt.show()

In the upcoming Machine Learning sections, we will use NumPy to shape our sensor data and Matplotlib to visualize our model's performance.

Previous
File I/O