Example #1
0
# Embed a circle in RP^4 with five "kinks".
import projective_mds
import ppca
X, n = projective_mds.circleRPn()

# Do a dimensionality reduction from X to RP^2 using PPCA.
V = ppca.ppca(X, 2)
P = V['X']
projective_mds.plot_RP2(P)

# Do the dimensionality reduction from X to RP^2 using PMDS.
M = projective_mds.pmds(X, 2)
projective_mds.plot_RP2(M)

# Now do the same with RP^3 and four "kinks".
X, n = projective_mds.circleRPn(dimn=3, num_segments=3)

# Do a dimensionality reduction from X to RP^2 using PPCA.
V = ppca.ppca(X, 2)
P = V['X']
projective_mds.plot_RP2(P)

# Do the dimensionality reduction from X to RP^2 using PMDS.
M = projective_mds.pmds(X, 2)
projective_mds.plot_RP2(M)
Example #2
0
# %% codecell
# Make a big for loop and try a bunch of parameters.
eta = pipeline.threshold_cocycle(eta, D_sub, birth + .01)
beta = pipeline.integer_lift(eta, p)
theta, nu = pipeline.harmonic_cocycle(beta, D_sub, p, birth + .01)
part_func = pipeline.partition_unity(D, death - .01, sub_ind)
CPn_coords = pipeline.CPn_coordinates(part_func, theta, nu)
D_pc = geometry.CPn_geo_distance_matrix(CPn_coords)
PH_pc = ripser(D_pc[sub_ind, :][:, sub_ind], distance_matrix=True, maxdim=2)
plot_diagrams(PH_pc['dgms'])
plt.title('Persistence with radius parameter %1.3f' % (death - .01))
plt.show()

# %% codecell
CPn_cplx = geometry.complexify(CPn_coords)
stuff = ppca.ppca(CPn_cplx.T, 1, verbose=True)
X_ppca = geometry.realify(stuff['X'].T)
print(X_ppca.shape)

#%%
D_pca = geometry.CPn_geo_distance_matrix(X_ppca)
print(D_pca.shape)
PH_pca = ripser(D_pca[sub_ind, :][:, sub_ind], distance_matrix=True, maxdim=2)
plot_diagrams(PH_pca['dgms'])
plt.show()

#%%
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
vis = geometry.hopf(X_ppca)
ax.scatter(vis[0, :], vis[1, :], vis[2, :], c=c, cmap='cividis')
# %% codecell
# Compute PH of landmarks of high-dimensional data.
print('Computing persistence of projective coordinates.')
PH_pc2 = ripser(D_geo[idx,:][:,idx], distance_matrix=True, maxdim=1, coeff=2)
plot_diagrams(PH_pc2['dgms'])
plt.title('Persistence of Projective Coordinates ($F_2$)')
plt.show()
PH_pc3 = ripser(D_geo[idx,:][:,idx], distance_matrix=True, maxdim=1, coeff=3)
plot_diagrams(PH_pc3['dgms'])
plt.title('Persistence of Projective Coordinates ($F_3$)')
plt.show()

# %% codecell
# Reduce dimension using PCA:
X_ppca = ppca(proj_coords.T, 2)['X']
# Compute persistence of PCA output.
D_ppca = geometry.RPn_geo_distance_matrix(X_ppca.T)
PH_ppca2 = ripser(D_ppca[idx,:][:,idx], distance_matrix=True, maxdim=1, coeff=2)
plot_diagrams(PH_ppca2['dgms'])
plt.title('Persistence of PPCA Embedding ($F_2$)')
plt.show()
PH_ppca3 = ripser(D_ppca[idx,:][:,idx], distance_matrix=True, maxdim=1, coeff=3)
plot_diagrams(PH_ppca3['dgms'])
plt.title('Persistence of PPCA Embedding ($F_3$)')
plt.show()

# %% codecell
# Apply MDS with squared geodesic cost function.
X_mds = geodesic_mds.rp_mds(D_geo)
proj_coords = pipeline.proj_coordinates(part_func, eta)
D_pc = geometry.RPn_geo_distance_matrix(proj_coords)
D_geo = pipeline.geo_distance_matrix(D_pc, k=8)

# %% codecell
# Compute PH of landmarks of high-dimensional data.
PH_pc = ripser(D_geo[sub_ind, :][:, sub_ind],
               distance_matrix=True,
               maxdim=2,
               coeff=2)
plot_diagrams(PH_pc['dgms'])
plt.show()

# %% codecell
# Apply PPCA to reduce to dimension 2.
stuff = ppca(proj_coords.T, 2)
X_ppca = stuff['X'].T
variance = stuff['variance']
plt.plot(range(2, 10), 1 - variance[0:8], 'bo--', linewidth=2, markersize=12)
plt.title('Percentage of cumulative variance')
plt.show()

# %% codecell
# Visualize PPCA output.
draw_circle = plt.Circle((0, 0), 1.0 + buff / 2, fill=False)
neg_idx = np.where(X_ppca[2, :] < 0)
X_ppca[:, neg_idx] *= -1
fig, ax = plt.subplots()
ax.scatter(X_ppca[0, :], X_ppca[1, :], c=c, cmap='cividis')
# ax.scatter(X_ppca[0,:], X_ppca[1,:], c=cc, cmap='viridis')
ax.add_artist(draw_circle)
Example #5
0
# Simple script for generating toy data. This is not meant to illustrate
# anything, just to have something I can run through the algorithm to
# make sure the different components work correctly.
import numpy as np
import cplx_projective
from ppca import ppca

# Create some random points on the 7-sphere in R^8.
X = np.random.rand(8, 5) - 0.5
X = X/np.linalg.norm(X, axis=0)

# Construct the distance matrix using these points to represent
# equivalence classes in CP^3. The goal will be to recover these
# distances with the points in CP^1.
D = cplx_projective.CPn_distance_matrix(X)

# Build an initial guess of a low-dimensional version
X_ppca = ppca(X.T, 3)['X'].T
D_ppca = cplx_projective.CPn_distance_matrix(X_ppca)
np.savez('examples/toy_data.npz', X=X, D=D, X_ppca=X_ppca, D_ppca=D_ppca)
               distance_matrix=True,
               maxdim=2,
               coeff=p)
plot_diagrams(PH_pc['dgms'])
plt.title('Klein Bottle Projective Coordinates ($\mathbb{F}_%d$)' % p)
plt.show()

# %% [markdown]
# ## Dimensionality Reduction Experiments
# %% [markdown]
# ### Experiment 1: PPCA Dimension
#
# First we run PPCA down to dimension one and plot the retained variance. If there is an "elbow" in that plot we have a good candidate for an embedding dimension.

# %%
ppca_data = ppca(proj_coords.T, 1)
X_ppca = ppca_data['X'].T
variance = ppca_data['variance']
plt.plot(range(1, n_landmarks),
         1 - variance,
         'bo--',
         linewidth=2,
         markersize=12)
plt.title('Percentage of cumulative variance retained')
plt.show()

# %% [markdown]
# This show a severe cutoff when we go below dimension eight. Let's get the PPCA embedding in dimension eight and check that it has good homology.

# %%
X_ppca8 = ppca(proj_coords.T, 8)['X'].T