-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.jl
67 lines (60 loc) · 1.27 KB
/
util.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using LinearAlgebra
using Statistics
using SliceMap
using Arpack
using ProgressMeter
using Random
using Distributions
import Seaborn
import Pandas
function pca(A, k)
@assert k <= minimum(size(A))
A_centered = A .- mean(A; dims=2)
if k == minimum(size(A))
U, = svd(A_centered)
else
(U,), = svds(A_centered; nsv=k)
end
return U' * A_centered, U'
end
# TODO: Use a Cholesky factorization
function svd_pow(A, p)
LinearAlgebra.checksquare(A)
U, S, V = svd(A)
@assert all(S .> 0)
return U * diagm(0=>S .^ p) * V'
end
function k_lowest_ind(A, k)
@assert 0 <= k
@assert k <= length(A)
if k == 0
return falses(size(A))
end
for (i, cut) in enumerate(sort(A[:]))
if i >= k
return A .<= cut
end
end
@assert false
end
function step_vec(n, k)
v = falses(n)
v[1:k] .= 1
return v
end
function sb_pairplot(A, clean=5000)
d, n = size(A)
df = Pandas.DataFrame(A')
df["poison"] = .! step_vec(n, clean)
Seaborn.pairplot(df, diag_kind="kde", hue="poison")
end
function ♭(A::AbstractMatrix)
LinearAlgebra.checksquare(A)
return A[:]
end
function ♯(v::AbstractArray)
n = length(v)
m = isqrt(n)
@assert m*m == n
return reshape(v, (m, m))
end