k-Nearest-Neighbor
KNN graph
feature engineering
data visualization
machine learning

Plot k-Nearest-Neighbor graph with 8 features?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

With eight features, you cannot directly "plot the graph" in the original feature space the way you would with two-dimensional data. The usual approach is to compute the k-nearest-neighbor relationships in the full 8-dimensional space, then visualize either a 2D projection of the points or the neighbor graph itself on top of that projection.

Build the KNN Graph in the Original 8D Space

The key idea is that distance calculations should happen in the real feature space, not in the 2D picture you create later for humans. That keeps the neighbor relationships faithful to the actual data.

python
1import numpy as np
2from sklearn.datasets import make_blobs
3from sklearn.neighbors import kneighbors_graph
4
5X, _ = make_blobs(n_samples=40, n_features=8, centers=3, random_state=42)
6A = kneighbors_graph(X, n_neighbors=3, mode="connectivity", include_self=False)
7
8print(A.shape)
9print(A.nnz)

A is a sparse adjacency matrix describing which points are connected to their nearest neighbors.

Project the Data to 2D for Visualization

Once the graph is built in eight dimensions, project the points to two dimensions with PCA, t-SNE, or UMAP. PCA is a good default because it is simple and deterministic.

python
1from sklearn.decomposition import PCA
2
3pca = PCA(n_components=2)
4X_2d = pca.fit_transform(X)
5
6print(X_2d[:3])

This 2D array is only for display. It is not what defined the original nearest-neighbor relationships.

Draw the Neighbor Edges on Top of the Projection

Now combine the projected coordinates with the adjacency matrix.

python
1import matplotlib.pyplot as plt
2
3rows, cols = A.nonzero()
4
5plt.figure(figsize=(8, 6))
6plt.scatter(X_2d[:, 0], X_2d[:, 1], s=50, color="tab:blue")
7
8for i, j in zip(rows, cols):
9    plt.plot(
10        [X_2d[i, 0], X_2d[j, 0]],
11        [X_2d[i, 1], X_2d[j, 1]],
12        color="lightgray",
13        linewidth=0.8,
14    )
15
16plt.title("3-Nearest-Neighbor Graph from 8D Data")
17plt.xlabel("PCA component 1")
18plt.ylabel("PCA component 2")
19plt.tight_layout()
20plt.show()

This gives you a visual graph where the edges reflect 8D neighbor relationships and the node positions come from a 2D projection.

Standardize Features Before Computing Distances

If the eight features are on different scales, KNN distances can become misleading because large-scale features dominate the metric. Standardization is usually the right first step.

python
1from sklearn.preprocessing import StandardScaler
2from sklearn.decomposition import PCA
3from sklearn.neighbors import kneighbors_graph
4
5scaler = StandardScaler()
6X_scaled = scaler.fit_transform(X)
7
8A = kneighbors_graph(X_scaled, n_neighbors=3, mode="connectivity", include_self=False)
9X_2d = PCA(n_components=2).fit_transform(X_scaled)

If you skip scaling on mixed-unit data, the plotted graph may look mathematically correct but still be conceptually wrong for the problem.

Use NetworkX If You Want a Graph Object

If you want graph algorithms or richer graph drawing controls, convert the adjacency matrix to a NetworkX graph.

python
1import networkx as nx
2
3G = nx.from_scipy_sparse_array(A)
4pos = {i: (X_2d[i, 0], X_2d[i, 1]) for i in range(len(X_2d))}
5
6plt.figure(figsize=(8, 6))
7nx.draw(G, pos, node_size=80, width=0.8, edge_color="gray", node_color="tab:orange")
8plt.title("KNN Graph")
9plt.show()

This is helpful if the goal is more graph analysis than point-cloud visualization.

Interpret the Plot Carefully

The final 2D picture is an approximation. Even if you computed neighbors correctly in 8D, the projection can visually distort distances. Two points that look close in the PCA chart are not guaranteed to be neighbors in the original feature space, and two real neighbors may look farther apart after projection.

So the graph is best interpreted as:

  • edges come from the real 8D neighbor computation
  • node positions are a 2D visualization aid

That distinction prevents a lot of misreading.

Common Pitfalls

The biggest mistake is computing KNN after projecting to 2D, which changes the actual neighbor structure and answers a different question. Another common issue is forgetting to scale the eight features before computing Euclidean distances. Developers also sometimes try to plot eight raw features directly, which is not meaningful as a static 2D chart. Finally, PCA or t-SNE can visually distort the geometry, so the display should be treated as an interpretation aid rather than proof of exact neighborhood geometry.

Summary

  • Compute nearest neighbors in the original 8-dimensional feature space.
  • Project the points to 2D only for visualization.
  • Draw edges from the real KNN adjacency matrix on top of the 2D projection.
  • Standardize features first if the feature scales differ.
  • Treat the final plot as a visualization of 8D relationships, not as the original geometry itself.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.