In the realm of data science and machine learning, hyperplanes play a crucial role in separating data points, especially in classification tasks. In this post, we’ll explore how hyperplanes can separate data points in a simple 2D space, using Python and NumPy to visualize the results. We’ll delve into the concepts of weight vectors, orthogonality, and dot products to understand how data points can be categorized relative to a hyperplane.
To illustrate this, let's create a simple dataset with 100 randomly generated points in 2D space. By plotting these points in relation to a hyperplane, we can begin to see how each point’s position relative to the hyperplane can indicate whether it's above, below, or on the hyperplane.
To generate this data, we use NumPy’s randn
function, which gives us 100 vectors with two coordinates (x, y), each normally distributed:
import numpy as np
# Generate synthetic data: 100 data points in 2D
np.random.seed(0)
data_points = np.random.randn(100, 2) # 100 vectors, each in 2D
A hyperplane in 2D space can be thought of as a line that divides the space into two halves. In this example, we’ll use a weight vector w
to define the hyperplane. The weight vector is orthogonal (perpendicular) to the hyperplane and determines its orientation. By adjusting w
, we can change the direction of the hyperplane.
For our first example, let’s choose a weight vector w = [1, 1]
, which will yield a hyperplane along the line y = -x
.
To determine whether a point lies above or below the hyperplane, we calculate the dot product between the weight vector w
and each data point. The dot product helps us project each point onto the weight vector, which gives us insight into its position relative to the hyperplane:
Here's the code snippet for calculating the dot product and classifying points:
# Define a weight vector for the hyperplane
w = np.array([1, 1])
# Calculate dot products
dot_products = np.dot(data_points, w)
# Classification of points
on_hyperplane = np.isclose(dot_products, 0)
above_hyperplane = dot_products > 0
below_hyperplane = dot_products < 0
Once we’ve calculated the dot products, we can plot the points and visualize the hyperplane along with the areas above and below it. Additionally, by plotting the weight vector, we can see its orthogonality to the hyperplane.