We've established that vectors are ordered lists of numbers, useful for representing data points or features. Now, let's bridge the gap between the mathematical concept and its practical implementation in Python. For numerical computing in Python, especially linear algebra, the NumPy library is the standard tool.
Representing vectors using NumPy is quite natural. A mathematical vector like v=[v1,v2,…,vn] corresponds directly to a one-dimensional NumPy array (ndarray
). This array structure is optimized for numerical operations, making it efficient for the calculations common in machine learning.
np.array
The primary method for creating a vector in NumPy is by using the np.array()
function, passing it a Python list containing the vector's elements.
First, ensure you have NumPy imported. The conventional way to do this is:
import numpy as np
Now, let's create a few vectors. If we want to represent a point in a 2D plane, say (2,5), we can create a vector like this:
# A 2-element vector
vec_2d = np.array([2, 5])
print(vec_2d)
This code creates a NumPy array vec_2d
containing the elements 2 and 5. The output will be:
[2 5]
Similarly, a vector in 3D space, like (1.5,−3.2,0.7), can be created:
# A 3-element vector with floating-point numbers
vec_3d = np.array([1.5, -3.2, 0.7])
print(vec_3d)
Output:
[ 1.5 -3.2 0.7]
NumPy automatically infers the data type for the array's elements (e.g., int64
for integers, float64
for floating-point numbers) based on the input list. If your list contains a mix of integers and floats, NumPy will typically promote the integers to floats to maintain consistency within the array. You can also explicitly control the data type using the dtype
argument in np.array()
. Specifying dtype
can be useful for controlling memory usage or ensuring numerical precision.
# Create a vector explicitly as 32-bit integers
vec_int32 = np.array([10, 20, 30], dtype=np.int32)
print(vec_int32)
print(vec_int32.dtype)
Output:
[10 20 30]
int32
As we saw when learning about NumPy arrays in general (Chapter 2), you can also use helper functions to initialize vectors. This is often handy when you know the size of the vector you need but don't have the specific values yet.
# Create a vector containing four zeros
zero_vector = np.zeros(4)
print("Zero vector:", zero_vector)
# Create a vector containing three ones
one_vector = np.ones(3)
print("One vector:", one_vector)
# Create a vector with a sequence of numbers (0 to 4)
sequence_vector = np.arange(5)
print("Sequence vector:", sequence_vector)
Output:
Zero vector: [0. 0. 0. 0.]
One vector: [1. 1. 1.]
Sequence vector: [0 1 2 3 4]
Note that np.zeros
and np.ones
create vectors with floating-point numbers by default.
Working with these NumPy vectors feels similar to working with Python lists. You can access individual elements using zero-based indexing (the first element is at index 0, the second at index 1, and so on).
# Create a sample vector
data_vector = np.array([11, 22, 33, 44, 55])
# Get the first element
first_element = data_vector[0]
print(f"First element: {first_element}") # Output: 11
# Get the third element
third_element = data_vector[2]
print(f"Third element: {third_element}") # Output: 33
# Modify an element
data_vector[1] = 25 # Change the second element
print(f"Modified vector: {data_vector}") # Output: [11 25 33 44 55]
Being comfortable with creating these 1D NumPy arrays is the first step towards applying linear algebra concepts in code. These array objects are not just containers for numbers; they are the foundation upon which NumPy builds efficient mathematical operations, as we will see in the upcoming sections covering vector addition, scalar multiplication, norms, and dot products.
© 2025 ApX Machine Learning