Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Vectors and Matrices | Basic Mathematical Concepts and Definitions
Mathematics for Data Analysis and Modeling
course content

Course Content

Mathematics for Data Analysis and Modeling

Mathematics for Data Analysis and Modeling

1. Basic Mathematical Concepts and Definitions
2. Linear Algebra
3. Mathematical Analysis

book
Vectors and Matrices

What is vector?

A vector is a mathematical object representing a quantity with both magnitude and direction. To understand the essence of what a vector is, let's first understand what is a single number.
A single number is a simple quantitative characteristic of some property that describes its magnitude. At the same time, a vector is a collection of such characteristics and can be used to describe complex objects and processes that a single number cannot describe.

So the vector can be interpreted as an ordered list of numbers. These numbers are also called coordinates.
Vectors can describe velocity, force, colors, complex data structures, etc. Also, with the help of vectors, the position of a point in space is usually set.

Example: determining the position of a point in space

12345678910111213141516171819202122232425262728
import matplotlib.pyplot as plt def plot_vectors(vectors): origin = [0, 0] # The point at which the vector begins # Plot each vector in the list for vector in vectors: # Plot a single vector from the origin to the specified point plt.quiver(*origin, *vector, angles='xy', scale_units='xy', scale=1) # Set the limits for the x-axis and y-axis plt.xlim(-5, 5) # Set the x-axis limits plt.ylim(-5, 5) # Set the y-axis limits # Label the x-axis and y-axis plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show grid lines on the plot plt.grid(True) # Display the plot plt.show() # Example usage vectors = [[3, 4], [-3, 4], [3, -4], [-3, -4]] # Four different vectors plot_vectors(vectors)
copy

We can see 4 different arrows: each has the same length but different directions. As a result, we can't describe the arrow using only magnitude as a single number - we have to use a vector to provide additional information about the direction of the arrow.

Example: Data representation for machine learning models

Data for machine learning models are also represented using vectors:

1234567891011
import numpy as np from sklearn.datasets import load_iris # Load the Iris dataset iris = load_iris() # Access the data attribute data = iris.data # Print the first few samples of the data print(f'Data samples:\n{data[:5]}')
copy

We can see that information about each Iris flower is stored as a vector with 4 coordinates. Each coordinate represents a particular feature of the flower: length/ width of a sepal/petal.
Obviously, if we use a single number instead of a vector, we will not be able to provide enough information about the characteristics of one particular plant.

What is matrix?

Another important mathematical object is a matrix - a rectangular array of numbers, symbols, or expressions arranged in rows and columns.
Matrices are used to represent and manipulate linear transformations, systems of linear equations, and other mathematical operations. We can interpret a matrix as a vector whose coordinates are vectors too. Matrices are commonly used to describe and model different processes.

Image representation using matrix

12345678910111213
import numpy as np import matplotlib.pyplot as plt # Generate a grayscale image image = np.array([[50, 100, 150, 200], [75, 125, 175, 225], [100, 150, 200, 250], [125, 175, 225, 255]]) # Display the grayscale image plt.imshow(image, cmap='gray') plt.axis('off') plt.show()
copy

Each number in the matrix represents a pixel's intensity or brightness value in the greyscale image.

Figure manipulations

We can also provide different manipulations with figures using matrices. For example, we can rotate a figure using matrix multiplication (we will consider this operation in more detail in the next section):

12345678910111213141516171819202122232425262728293031323334353637383940414243
import numpy as np import matplotlib.pyplot as plt # Define the coordinates of the figure x = np.array([0, 1, 1, 0, 0]) y = np.array([0, 0, 1, 1, 0]) # Plot the original figure plt.plot(x, y, color='blue', label='Original') # Define the rotation angle in degrees angle_deg = 45 # Convert the angle to radians angle_rad = np.radians(angle_deg) # Create the rotation matrix rotation_matrix = np.array([[np.cos(angle_rad), -np.sin(angle_rad)], [np.sin(angle_rad), np.cos(angle_rad)]]) # Perform the rotation transformation vertices = np.row_stack((x, y)) vertices_rotated = np.dot(rotation_matrix, vertices) # Extract the rotated coordinates x_rotated = vertices_rotated.T[:, 0] y_rotated = vertices_rotated.T[:, 1] # Plot the rotated figure plt.plot(x_rotated, y_rotated, color='red', label='Rotated') # Set the aspect ratio and limits of the plot plt.gca().set_aspect('equal') plt.xlim(-2, 2) plt.ylim(-2, 2) # Add labels and legend plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.legend() # Display the plot plt.show()
copy

We used a specific rotation matrix and applied it to our figure. This matrix determines the way we rotate our figure. You can change the angle_deg variable in the code above and look at the result of rotation.
As a result, we got the same figure with the same characteristics but rotated by 45 degrees counterclockwise.

Note

There are many other ways to use vectors and matrices in real-life tasks. Enumerating them will take a long time, so we will not do it in this course.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
We're sorry to hear that something went wrong. What happened?
some-alt