> ## Documentation Index
> Fetch the complete documentation index at: https://servamind.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Visualize `.serva` files

> Plot t-SNE projections of .serva encoded datasets to inspect class structure in the encoded byte space.

Visualizing `.serva` files in a low-dimensional projection confirms that the encoded bytes carry learnable class structure. This tutorial uses **t-SNE** on the raw encoded bytes — no decoding, no preprocessing.

***

## t-SNE of encoded `.serva` bytes

Load a directory of `.serva` files, pad to the maximum file size, reduce with PCA to 50 dimensions (standard t-SNE speedup), then project to 3D with t-SNE. Color by class label.

**Dependencies:** `numpy`, `scikit-learn`, `matplotlib`.

```python theme={null}
import math
from pathlib import Path

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE

data_dir = Path("data/serva_mnist/val")
serva_files = sorted(data_dir.rglob("*.serva"))

# Pad all files to max size so every vector has the same length
max_size = max(p.stat().st_size for p in serva_files)

vectors, labels = [], []
for p in serva_files:
    cls = int(p.parent.name)  # class from folder name
    raw = p.read_bytes()
    buf = bytearray(max_size)
    buf[: len(raw)] = raw
    vectors.append(np.frombuffer(bytes(buf), dtype=np.uint8).astype(np.float32))
    labels.append(cls)

X = np.stack(vectors)
y = np.array(labels)

# PCA pre-reduction (50 dims) then t-SNE to 3D
X_50 = PCA(n_components=50, random_state=42).fit_transform(X)
tsne = TSNE(n_components=3, perplexity=30, random_state=42, max_iter=1000, init="random")
X_tsne = tsne.fit_transform(X_50)

fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection="3d")
scatter = ax.scatter(
    X_tsne[:, 0], X_tsne[:, 1], X_tsne[:, 2],
    c=y, cmap="tab10", alpha=0.4, s=5, edgecolors="none",
)
cbar = fig.colorbar(scatter, ax=ax, ticks=range(10), shrink=0.6, pad=0.1)
cbar.set_label("Digit")
ax.set_xlabel("t-SNE 1")
ax.set_ylabel("t-SNE 2")
ax.set_zlabel("t-SNE 3")
ax.set_title(f"t-SNE of .serva encoded MNIST — {len(serva_files):,} digits")
ax.view_init(elev=25, azim=135)
plt.show()
```

![3D t-SNE scatter plot of .serva encoded MNIST digits colored by class, showing cluster structure in the encoded byte space.](https://mintcdn.com/servamind/Q-XDPBlBrb1gXCbf/assets/viz-tsne-classes.png?fit=max\&auto=format\&n=Q-XDPBlBrb1gXCbf\&q=85\&s=08124c4ca9263d126860adf002b33181)

> [!TIP]
>
> The PCA pre-reduction from raw bytes to 50 dimensions keeps t-SNE runtime under a minute on 10k samples. For larger datasets, increase `perplexity` proportionally.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.