Skip to content

Instantly share code, notes, and snippets.

@tdunning
Created January 29, 2025 22:04
Show Gist options
  • Save tdunning/bf2081b74a0435e88ff378dd40488297 to your computer and use it in GitHub Desktop.
Save tdunning/bf2081b74a0435e88ff378dd40488297 to your computer and use it in GitHub Desktop.
function normalize_rows(x)
n = sqrt.(sum(x .* x, dims=1))
x ./ (n + (n .== 0))
end
"""
Project and plot high-dimensional data.
Data is n x d where each row is a point. The projection p is n x 2.
"""
project(data, p, col) = let d = data * p
scatter(d[:,1], d[:,2], color=Int.(col))
end
# the data are random corners on a high dimensional hypercube
data = hcat(rand([0,1], 4, 1000)', zeros(1000, 100));
# the locations are slightly perturbed versions
locations = (2*data .- 1) + 0.1 * randn(1000,104);
# color is just an enumeration of the 4-d corners
col = Int.(round.(data * [1,2,4,8,zeros(100)...]))
# random projections
p1 = normalize_rows(randn(104,2));
p2 = normalize_rows(randn(104,2))
# axis-aligned projection
p3 = [1 0; 0 1; zeros(102,2)];
# random projection of first 4, 5 or 6 dims, ignores all others
p4 = normalize_rows([randn(4,2); zeros(100,2)]);
p5 = normalize_rows([randn(5,2); zeros(99,2)]);
p6 = normalize_rows([randn(6,2); zeros(98,2)]);
# vary which projection for different views
project(locations, p1, color=col)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment