i trying calculate pseudo inverse of matrix should not difficult. problem inverting matrix.
i using following code:
a=numpy.random.random_sample((4,5,)) a=mat(a) b=pseudoinverse(a) def pseudoinverse(a): helper=a.t*a print helper*helper.i pi=helper.i*a.t return pi` to test included print line. helper*helper.i should give unity. output is:
[[ 2. -1. 0. 0. 3. ] [ 0. 2. 0. 0. 3.5 ] [ 0. -0.5 1.125 -1. 2.25 ] [ 2. 0. 0.25 -2. 3. ] [ 0. 0. 0.5 -2. 4. ]] which not unity. don't know did wrong , know.
your matrix a not have full column rank. in consequence helper singular , not invertible (if print helper.i see large numbers).
the solution compute right inverse instead of left inverse:
helper = * a.t pi = a.t * helper.i see wikipedia more details.
unless doing exercise, use numpy's built in implementation of pseudeinverse.
edit
>>> numpy.random.seed(42) >>> = mat(numpy.random.random_sample((3, 4))) # smaller matrix nicer output >>> h = * a.t >>> h * h.i matrix([[ 1.00000000e+00, 1.33226763e-15, 0.00000000e+00], [ -1.77635684e-15, 1.00000000e+00, 0.00000000e+00], [ 0.00000000e+00, 1.33226763e-15, 1.00000000e+00]]) up numeric precision looks pretty identity matrix me.
the problem in code a.t * a not invertible. if try invert such matrix wrong results. in contrast, a * a.t invertible.
you have 2 options:
- change direction of multiplication
- call
pseudoinverse(a.t)
Comments
Post a Comment