Skip to content

Instantly share code, notes, and snippets.

@zacstewart
Created July 15, 2013 15:36
Show Gist options
  • Save zacstewart/6000932 to your computer and use it in GitHub Desktop.
Save zacstewart/6000932 to your computer and use it in GitHub Desktop.
Linear kernel matrix inversion
>>> K = X.dot(X.transpose())
>>> K
array([[ 2, 3, 3, 5, 5, 6],
[ 3, 5, 4, 8, 7, 9],
[ 3, 4, 5, 7, 8, 9],
[ 5, 8, 7, 13, 12, 15],
[ 5, 7, 8, 12, 13, 15],
[ 6, 9, 9, 15, 15, 18]])
>>> np.linalg.inv(K)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/zacstewart/Code/kaggle_belkin/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 445, in inv
return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
File "/Users/zacstewart/Code/kaggle_belkin/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 328, in solve
raise LinAlgError('Singular matrix')
numpy.linalg.linalg.LinAlgError: Singular matrix
>>> np.linalg.pinv(K)
array([[ 0.00068587, 0.00102881, 0.00102881, 0.00171468, 0.00171468,
0.00205761],
[ 0.00102881, 0.12654321, -0.12345679, 0.12757202, -0.12242798,
0.00308642],
[ 0.00102881, -0.12345679, 0.12654321, -0.12242798, 0.12757202,
0.00308642],
[ 0.00171468, 0.12757202, -0.12242798, 0.12928669, -0.12071331,
0.00514403],
[ 0.00171468, -0.12242798, 0.12757202, -0.12071331, 0.12928669,
0.00514403],
[ 0.00205761, 0.00308642, 0.00308642, 0.00514403, 0.00514403,
0.00617284]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment