Last active
December 8, 2017 19:24
-
-
Save simonbyrne/2714e387420507639ffb34ad5ac15602 to your computer and use it in GitHub Desktop.
column/row normalization of sparse matrices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
normalizecols!(X, p) | |
Normalize the non-empty columns of `X` using a `p`-norm. | |
Overwrites the contents of `X`. | |
""" | |
function normalizecols!(X::SparseMatrixCSC, p=2) | |
for col = 1:X.n | |
rr = X.colptr[col]:X.colptr[col+1]-1 | |
n = norm(@view(X.nzval[rr]),p) | |
X.nzval[rr] ./= n | |
end | |
X | |
end | |
""" | |
normalizerows!(X, p) | |
Normalize the non-empty rows of `X` using a `p`-norm. | |
Overwrites the contents of `X`. | |
""" | |
function normalizerows!(X::SparseMatrixCSC, p=2) | |
ns = zeros(X.m) | |
if p == 2 | |
for (i,v) in zip(X.rowval, X.nzval) | |
ns[i] += abs2(v) | |
end | |
ns .= sqrt.(ns) | |
elseif p == 1 | |
for (i,v) in zip(X.rowval, X.nzval) | |
ns[i] += abs(v) | |
end | |
elseif p == Inf | |
for (i,v) in zip(X.rowval, X.nzval) | |
maxabs = ns[i] | |
vnorm = abs(v) | |
ns[i] = ifelse(isnan(maxabs) | (maxabs > vnorm), maxabs, vnorm) | |
end | |
elseif p == 0 | |
for (i,v) in zip(X.rowval, X.nzval) | |
ns[i] += v != 0 | |
end | |
elseif p == -Inf | |
for (i,v) in zip(X.rowval, X.nzval) | |
minabs = ns[i] | |
vnorm = abs(v) | |
ns[i] = ifelse(isnan(minabs) | (maxabs < vnorm), minabs, vnorm) | |
end | |
else | |
for (i,v) in zip(X.rowval, X.nzval) | |
ns[i] += abs(v)^p | |
end | |
ns .= ns.^(1/p) | |
end | |
for (k,i) in enumerate(X.rowval) | |
X.nzval[k] /= ns[i] | |
end | |
X | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment