Skip to content

Commit

Permalink
update demo files
Browse files Browse the repository at this point in the history
  • Loading branch information
hyhuang00 committed Sep 29, 2024
1 parent 8422fe6 commit be57e5b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions demo/basic_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# Initialize the pacmap instance
# By default, the n_neighbors is set to 10.
# Setting n_neighbors to "None" can enable an automatic parameter selection
# choice shown in "parameter" section of the README file.
# choice shown in "parameter" section of the README file.
# Notice that from v0.6.0 on, we rename the n_dims parameter to n_components.
reducer = pacmap.PaCMAP(n_components=2, n_neighbors=10, MN_ratio=0.5, FP_ratio=2.0)
reducer = pacmap.PaCMAP(n_components=2, n_neighbors=10, MN_ratio=0.5, FP_ratio=2.0)

# fit the data (The index of transformed data corresponds to the index of the original data)
X_transformed = reducer.fit_transform(X, init="pca")
Expand Down
4 changes: 3 additions & 1 deletion demo/demo_utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import matplotlib.pyplot as plt


def generate_figure(embedding, labels, title):
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
ax.scatter(embedding[:, 0], embedding[:, 1], s=0.5, c=labels, cmap='Spectral')
ax.axis('off')
ax.set_title(title)
plt.savefig(f"./{title}.png")


def generate_combined_figure(embeddings, labels, titles, theme_title):
len_subfigs = len(embeddings)
assert len(labels) == len_subfigs
assert len(titles) == len_subfigs
n_rows = (len_subfigs + 2) // 3
fig, axes = plt.subplots(n_rows, 3, figsize = (18, n_rows * 6))
fig, axes = plt.subplots(n_rows, 3, figsize=(18, n_rows * 6))
axes = axes.flatten()
for i in range(len_subfigs):
ax = axes[i]
Expand Down
10 changes: 7 additions & 3 deletions demo/specify_nn_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

nbrs = np.zeros((n, 20), dtype=np.int32)
for i in range(n):
nbrs_ = tree.get_nns_by_item(i, 20 + 1) # The first nbr is always the point itself
nbrs_ = tree.get_nns_by_item(i, 20 + 1) # The first nbr is always the point itself
nbrs[i, :] = nbrs_[1:]

scaled_dist = np.ones((n, n_neighbors)) # No scaling is needed
scaled_dist = np.ones((n, n_neighbors)) # No scaling is needed

# Type casting is needed for numba acceleration
X = X.astype(np.float32)
Expand All @@ -37,7 +37,11 @@

# initializing the pacmap instance
# feed the pair_neighbors into the instance
embedding = pacmap.PaCMAP(n_components=2, n_neighbors=n_neighbors, MN_ratio=0.5, FP_ratio=2.0, pair_neighbors=pair_neighbors)
embedding = pacmap.PaCMAP(n_components=2,
n_neighbors=n_neighbors,
MN_ratio=0.5,
FP_ratio=2.0,
pair_neighbors=pair_neighbors)

# fit the data (The index of transformed data corresponds to the index of the original data)
X_transformed = embedding.fit_transform(X, init="pca")
Expand Down
6 changes: 3 additions & 3 deletions demo/transform_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pacmap
import numpy as np
from sklearn.model_selection import StratifiedKFold
from demo_utils import *
import demo_utils


# MNIST
Expand All @@ -25,7 +25,7 @@
X_train, X_test = mnist[train_index], mnist[test_index]
y_train, y_test = labels[train_index], labels[test_index]
break

# Initialize the instance
reducer = pacmap.PaCMAP(n_components=2, n_neighbors=10, MN_ratio=0.5, FP_ratio=2.0, random_state=20, save_tree=False)

Expand All @@ -41,4 +41,4 @@
embeddings = [embedding, embedding_test, embedding_combined]
labelset = [y_train, y_test, y]
titles = ['Training', 'Test', 'Combined']
generate_combined_figure(embeddings, labelset, titles, f'mnist_transform_{n}')
demo_utils.generate_combined_figure(embeddings, labelset, titles, f'mnist_transform_{n}')

0 comments on commit be57e5b

Please sign in to comment.