I have a very big 500,000 X 500,000 but sparse matrix. I want to find its norm using Python, I tried using:
%timeit numpy.linalg.norm(a, axis=1)
where a is the matrix
I also tried this:
b = numpy.asarray(a, numpy.float32)
numpy.linalg.norm(b, axis=1)
But the Google Colabalways crashes.
I also tried doing this:
import numpy as np
a = np.random.rand(1000000,100)
print(np.linalg.norm(a, axis =1).shape)
def g(d, out=None):
bs = 2000
if out is None:
r = np.empty(d.shape[0])
else:
r = out
for i in range(0, d.shape[0], bs):
u = min(i + bs, d.shape[0])
r[i:u] = np.linalg.norm(d[i:u], axis=1)
return r
print((g(a) == numpy.linalg.norm(a, axis =1)).all())
print("blocked")
%timeit -n 10 g(a)
print("normal")
%timeit -n 10 numpy.linalg.norm(a, axis =1)
from one of the previous stack overflow questions, but still the kernel crashes, Is there a way I can do that?
If I make a modest sized sparse matrix:
I can't use
np.linalg.normon it:But I can do it on its dense equivalent.
M.Awill produce a memory error isMdimensions are large.sparsehas a norm that works onM:For this
L2norm I can do the sparse calculation directly:This sum produces a (1,10) dense matrix. Using
A1to make a 1d array, I get the same numbers:However it's done we need a tempory array/matrix that's just as large to hold the square values. The sum reduces the dimension etc.
sparse array
I don't need the
A1step if I make asparse_array(whichsparseis moving towards slowly).