Calculate Matrix Rank using scipy?

The answer is no—there is currently no function dedicated to calculating the matrix rank of an array/matrix in scipy. Adding one has been discussed before, but if it's going to happen, I don't believe it has yet.

Up vote 5 down vote favorite share g+ share fb share tw.

I'd like to calculate the mathematical rank of a matrix using scipy. The most obvious function numpy. Rank calculates the dimension of an array (ie.

Scalars have dimension 0, vectors 1, matrices 2, etc...). I am aware that the numpy.linalg. Lstsq module has this capability, but I was wondering if such a fundamental operation is built into the matrix class somewhere.

Here is an explicit example: from numpy import matrix, rank A = matrix(1,3,7,2,8,3,7,8,1) print rank(A) This gives 2 the dimension, where I'm looking for an answer of 3. Python matrix numpy scipy link|improve this question asked Mar 18 '10 at 23:10Hooked2,059621 98% accept rate.

I checked the rank using Mathematica - it's indeed 3. The function you're calling in Python is either incorrect or you're using it wrong. – duffymo Mar 18 '10 at 23:37 2 The usage is correct - this is what baffled me in the first place.

In the post I explain what rank does: it calculates the dimensionality of the array. A "rank-3" array would be a list-of-lists-of-lists. – Hooked Mar 18 '10 at 23:44 Note that the term "rank" is somewhat ambiguous.

For a tensor, the rank tells you the number of indices (e.g. A scalar is a rank-0 tensor, a vector rank-1 and a matrix rank-2). For linear algebra there is also the definition you cite above. From the docstring, it's clear that Numpy uses the former.

– Rupert Nash Mar 29 '10 at 13:17.

1 Incidentally, mail.scipy.org/pipermail/numpy-discussio... is the first post of a short newsgroup discussion about this. – Mike Graham Mar 19 '10 at 0:24 1 Nowadays, there is numpy.linalg. Matrix_rank().

See my answer. – Simon Dec 2 '11 at 5:17.

Numpy provides numpy.linalg. Matrix_rank(): >>> import numpy >>> numpy. __version__ '1.5.1' >>> A = numpy.

Matrix(1,3,7,2,8,3,7,8,1) >>> numpy.linalg. Matrix_rank(A) 3.

To provide a rough code snippet for people who need to get this done in practice. Feel free to improve. U, s, v = np.linalg.

Svd(A) rank = np. Sum(s > 1e-10).

An efficient way to compute the rank is via the Singular Value Decomposition - the rank of the matrix is equal to the number of non-zero singular values. Def rank(A, eps=1e-12): u, s, vh = numpy.linalg. Svd(A) return len(x for x in s if abs(x) > eps) Notice that eps depends in your application - most would agree that 1e-12 corresponds to zero, but you may witness numerical instability even for eps=1e-9.

Using your example, the answer is three. If you change the second row to 2, 6, 14 (linearly dependent with row one) the answer is two (the "zero" eigenvalue is 4.9960E-16).

I don't know about Numpy in particular, but that's unlikely to be a built-in operation on a matrix; it involves fairly intensive numerical computations (and associated concerns about floating-point roundoff error and so forth) and threshold selections that may or may not be appropriate in a given context, and algorithm selection is important to computing it accurately and quickly. Things that are built into the basic classes tend to be things that can be performed in a unique and straightforward manner, such as matrix multiplications at the most complex.

This is a good point, a numerically unstable matrix could cause the rank to change due to roundoff errors. However, this is a known problem and I was wondering if the scipy/numpy libraries directly have a function. If the answer is no - that's fine too, I can always go with a SVD.

– Hooked Mar 18 '10 at 23:52 1 It's not just numerically-unstable ones. How about {{1.0, 3.0}, {1.0/3.0, 1.0}}? The division can't produce an exact answer, so should this get counted as rank 1, or rank 2?

– Brooks Moses Mar 19 '10 at 18:17.

The linear algebra functions are generally grouped in numpy.linalg. (They're also available from scipy. Linalg, which has more functionality.

) This allows polymorphism: the functions can accept any of the types that SciPy handles. So, yes, the numpy.linalg. Lstsq function does what you're asking.

Why is that insufficient?

1 It does what I'm asking - but it does a lot more unnecessarily, and with a large amount of baggage. The same could have been accomplished with a LU decomposition and then a row sort. The intent of the question - if this wasn't clear, was if a function existed whose sole purpose was to calculate the rank.

Ie. Take in a matrx, spit out an int. – Hooked Mar 18 '10 at 23:48.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions