No history yet

NumPy Basics

Why NumPy?

At its core, Python is a flexible language. You can store numbers in lists, but performing mathematical operations across those lists can be slow and clunky. For data science and machine learning, you need to perform calculations on large sets of numbers quickly. This is where NumPy comes in.

NumPy (Numerical Python) is the foundational package for numerical computing in Python.

Think of NumPy as a powerful upgrade for handling numbers. It introduces a new data structure called an array. A NumPy array is a grid of values, all of the same type. While it looks similar to a Python list, it's fundamentally different under the hood. NumPy arrays are stored more efficiently and allow for mathematical operations to be performed on the entire set of values at once, rather than one by one in a loop. This makes the code cleaner and dramatically faster, since the heavy lifting is done by pre-compiled C code.

Getting Started

NumPy isn't built into Python, so you'll likely need to install it first. You can do this using pip, Python's package installer, from your terminal or command prompt.

pip install numpy

Once installed, you can import it into your Python scripts. By convention, developers almost always import NumPy with the alias np. This saves typing and makes the code recognizable to others.

import numpy as np

Creating Arrays

The most straightforward way to create a NumPy array is by converting a Python list. Let's create a simple one-dimensional array.

import numpy as np

# Create an array from a Python list
my_list = [1, 5, 10, 15]
arr1d = np.array(my_list)

print(arr1d)
# Output: [ 1  5 10 15]

Arrays can have more than one dimension. To create a two-dimensional array, you can pass a list of lists. This is useful for representing matrices or tables of data.

import numpy as np

# A list of lists becomes a 2D array
my_nested_list = [[1, 2, 3], [4, 5, 6]]
arr2d = np.array(my_nested_list)

print(arr2d)
# Output:
# [[1 2 3]
#  [4 5 6]]

NumPy also provides handy functions for creating common types of arrays without starting from a list. For instance, you can create arrays pre-filled with zeros or ones, which is useful for creating placeholders.

import numpy as np

# Create a 2x3 array of all zeros
zeros_arr = np.zeros((2, 3))
print(zeros_arr)
# Output:
# [[0. 0. 0.]
#  [0. 0. 0.]]

# Create a 3x2 array of all ones
ones_arr = np.ones((3, 2))
print(ones_arr)
# Output:
# [[1. 1.]
#  [1. 1.]
#  [1. 1.]]

Simple Array Math

The real power of NumPy becomes clear when you start doing math. With Python lists, you'd need a for loop to add a number to every element. With NumPy, you can apply operations to the entire array at once. This is called an element-wise operation.

import numpy as np

arr = np.array([10, 20, 30])

# Add 5 to every element
result = arr + 5
print(result)
# Output: [15 25 35]

# Multiply every element by 2
result = arr * 2
print(result)
# Output: [20 40 60]

You can also perform these operations between two arrays of the same shape. The operation is performed between the elements at corresponding positions.

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([10, 20, 30])

# Add the arrays element-wise
sum_arr = arr1 + arr2
print(sum_arr)
# Output: [11 22 33]

# Subtract the arrays element-wise
diff_arr = arr2 - arr1
print(diff_arr)
# Output: [ 9 18 27]

This ability to perform fast, element-wise math without writing explicit loops is a cornerstone of scientific computing in Python.

Let's check what you've learned.

Quiz Questions 1/5

What is the primary advantage of using a NumPy array over a standard Python list for numerical computations?

Quiz Questions 2/5

By convention, how is the NumPy library imported into a Python script?

This is just the beginning. NumPy offers a deep well of functionality for data manipulation, linear algebra, and more, which you'll explore as you continue your journey into machine learning.