---
title: "Magnetic materials embedding and dimensionality reduction"
url: "https://ouro.foundation/posts/mmoderwell/magnetic-materials-embedding-and-dimensionality-reduction"
id: "01964df5-b2eb-7b35-84e0-272edf51b61c"
type: "post"
---

# Magnetic materials embedding and dimensionality reduction

Post by @mmoderwell in #permanent-magnets (all) · 2025-04-19

- Asset id: `01964df5-b2eb-7b35-84e0-272edf51b61c`
- Page: https://ouro.foundation/posts/mmoderwell/magnetic-materials-embedding-and-dimensionality-reduction

# Magnetic materials embedding and dimensionality reduction

Here we have an HTML file generated from Python and [Plotly](https://dash.plotly.com/) that displays ~5,000 magnetic materials in 3 dimensions. To generate this visual, I took each material and "embedded" it by running it through the Orb v2 MLIP model and extracted the latent space part of the GNN.

I then reduce 256 dimensional output into just 3 with UMAP and visualized each, colored by their magnetic density.

> Embedded file: [magnetic_materials_umap_visualization](https://ouro.foundation/files/mmoderwell/magnetic-materials-umap-visualization)

The visualization is interactive. You can zoom around, find clusters, and see what Materials Project material each is. I've found some interesting clusters of high density while still seemingly different chemical systems.

The idea is to use these dimensions and the "location" of known permanent magnets and find similar materials. It's another layer of candidate filtering. We're basically looking for ranges on the values of the latent features that may lead to good magnets. Ideally, it will encode anisotropy or coercivity, two important features we haven't been able to build a model for yet.

If would could find a material **like** NdFeB but without rare-earths, that would be amazing.

---

@will I'll work on a dataset similar to the recent one I shared of magnetic materials with Curie temperature and magnetic density, with an added column of cosine similarity or L2 distance to a couple known permanent magnets.

---

I tried the same thing with superconductors to see if we could find clusters of high-Tc materials. Check that out here:

> Embedded file: [Interactive 3D latent space exploration with Tc](https://ouro.foundation/files/mmoderwell/interactive-3d-latent-space-exploration-with-tc)

---

### Code

```python
#!/usr/bin/env python3
import pandas as pd
import numpy as np
import plotly.express as px
from umap import UMAP
import plotly.io as pio

# Set plotly to render in browser for better interaction
pio.renderers.default = "browser"

# Load the data
print("Loading data...")
df = pd.read_csv("magnetic_features_with_density.csv")

# Separate features and target
target = "magnetic_density"
# total_magnetic_moment
feature_cols = [col for col in df.columns if col.startswith("orb_feat_")]
X = df[feature_cols].values
y = df[target].values

print(f"Performing UMAP on {len(feature_cols)} features...")
# Initialize and fit UMAP
umap_3d = UMAP(
    n_components=3,
    n_neighbors=15,
    min_dist=0.1,
    metric="euclidean",
    target_metric="euclidean",
    target_weight=0.5,
    random_state=42,
)

# Fit and transform the data
embedding = umap_3d.fit_transform(
    X,
)

# Create a new dataframe with the embeddings
viz_df = pd.DataFrame(
    {
        "UMAP1": embedding[:, 0],
        "UMAP2": embedding[:, 1],
        "UMAP3": embedding[:, 2],
        target: y,
        "MP ID": df["mp_id"],
        "formula": df["cif_path"].str.replace("cif_path/", ""),
    }
)

print("Creating 3D visualization...")
# Create an interactive 3D scatter plot
fig = px.scatter_3d(
    viz_df,
    x="UMAP1",
    y="UMAP2",
    z="UMAP3",
    color=target,
    hover_data=["formula"],
    color_continuous_scale="Viridis",
    title=f"UMAP visualization colored by {target}",
)

# Update the layout for better visualization
fig.update_layout(
    scene=dict(xaxis_title="UMAP1", yaxis_title="UMAP2", zaxis_title="UMAP3"),
    width=1000,
    height=800,
)

# Show the plot
fig.show()

# Save the plot as HTML for later viewing
fig.write_html("magnetic_umap_visualization.html")
print("Visualization saved as 'magnetic_umap_visualization.html'")
```

### Dataset

> Embedded file: [magnetic_features_with_density.csv](https://ouro.foundation/files/mmoderwell/magnetic-features-with-densitycsv)

## Assets in this post

- [magnetic_materials_umap_visualization](https://ouro.foundation/files/mmoderwell/magnetic-materials-umap-visualization) (file, id `6258f985-16bc-44bc-aef9-04bde615bd34`)
- [Interactive 3D latent space exploration with Tc](https://ouro.foundation/files/mmoderwell/interactive-3d-latent-space-exploration-with-tc) (file, id `7ce90639-e4c1-4654-b406-8daf155eba25`)
- [magnetic_features_with_density.csv](https://ouro.foundation/files/mmoderwell/magnetic-features-with-densitycsv) (file, id `85fbc2ca-9eab-4169-a335-e605bab58d15`)
