Python lists are great, but they are too slow for the massive datasets used in Machine Learning. Enter NumPy.
NumPy (Numerical Python) is a library that allows us to do math at the speed of AI. The secret is the NumPy Array, which is like a Python list but heavily optimized for speed.
First, we always import the library, usually as np.
Watch how the same 6 elements are stored and represented in different dimensions.
.ndim)2.shape)(2, 3).size)6In standard Python, if you want to add 1 to every item in a list, you need a for loop. In NumPy, you can do it instantly with "vectorized" operations. This is the magic behind fast AI!
See the scalar operation broadcasted across all vector elements simultaneously. No loops, purely parallel.
See how NumPy automatically expands (broadcasts) different shapes (3, 1) and (1, 3) to match each other.
NumPy makes analyzing data a breeze with built-in functions:
np.mean(arr) - Averagenp.max(arr) - Maximum valuenp.std(arr) - Standard DeviationA very common preprocessing step in ML is standardizing features so they have a mean of 0. Let's do it with NumPy!
prices = np.array([100, 200, 300, 400, 500]).prices and store it in mean_price.std_price.normalized_prices by subtracting the mean from prices, and then dividing by the standard deviation. (Do it all in one vectorized line!).normalized_prices.