Last active
November 4, 2016 01:03
-
-
Save koorukuroo/2e1d3dca779f0a22263e to your computer and use it in GitHub Desktop.
Singular Value Decomposition Test
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
Created on Mon Jul 14 14:45:55 2014 | |
@author: Kyunghoon | |
""" | |
import numpy as np | |
# Original Data Matrix | |
Original = np.matrix([[1,1,0,1],[7,0,0,7],[1,1,0,1],[2,5,3,6]]) | |
print Original | |
U, Sigma, VT = np.linalg.svd(Original) | |
# Calculation with all singular value | |
Result = np.matrix(U)*np.matrix(np.diag(Sigma))*np.matrix(VT) | |
print np.round(Result).astype(np.int64) | |
# Calculation with 3 singular value | |
mU = U | |
mSigma = np.diag(np.append(Sigma[0:3],0)) | |
mVT = VT | |
Result = mU*mSigma*mVT | |
print np.round(Result).astype(np.int64) | |
# Calculation with 2 singular value | |
mU = U | |
mSigma = np.diag(np.append(Sigma[0:2],[0,0])) | |
mVT = VT | |
Result = mU*mSigma*mVT | |
print np.round(Result).astype(np.int64) | |
# Calculation with 1 singular value | |
mU = U | |
mSigma = np.diag(np.append(Sigma[0:1],[0,0,0])) | |
mVT = VT | |
Result = mU*mSigma*mVT | |
print np.round(Result).astype(np.int64) |
Author
koorukuroo
commented
Jul 14, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment