SVD Toy Examples
SVD Toy Examples
1. Introduction.
We can view a matrix A mxn (m rows and n columns) as a tranformation from
Row space (nxn) into Column space (mxm) or viceversa . This is illustrated in the figure below.
nxn
row space
=At*A
We can go from rowspace into col space with A and return back into row space with At*A.
So At A describes the row space with the col space effects. Similarly A*At describes
the col space with row effects.
Here U is the eigen basis of col space space , V is the col space and Sig connects these spaces
via a diagonal matrix .
The col space is U given by 5 orthogonal eigen basis vector.In other words
any vector in the column space can be expressed as a linear combination of
the orthogonal basis cols of U below.
The sigma matrix is able to take any row vextor (4x1) into col space (5x1)
S(5x4) = 94.025126 0. 0. 0.
0. 6.1053811 0. 0.
0. 0. 3.221D-16 0.
0. 0. 0. 5.804D-17
0. 0. 0. 0.
Then the above matrix A can be viwed as their ratings simply by labeling the cols and rows as
below. A(5x4 ) = Snt Te Da Ac
G1 12. 12. 18. 36.
G2 11. 11. 13. 40.
G3 17. 17. 25. 52.
G4 2. 2. 4. 4.
G5 8. 8. 10. 28
Possible interpretation of SVD.
Oc = Snt Te Da Ac
Snt 622. 622. 872. 1988.
Te 622. 622. 872. 1988.
Da 872. 872. 1234. 2764.
Ac 1988. 1988. 2764. 6400.
V(4x4)= 94.025126
S(5x4) = 94.025126 0. 0. 0.
0. 6.1053811 0. 0.
0. 0. 3.221D-16 0.
0. 0. 0. 5.804D-17
0. 0. 0. 0.
The S matrix tells us that there are really only two impt sets of ratings given
by the girls viz (ignore neg sign)
S(1,1)= 94.025126 this is the max weightage ,given to first vector of ratings of the girls:
V(:,1) = Snt
Snt - 0.2649413
Te - 0.2649413
Da - 0.3696952
Ac - 0.8502574
From the vector V(:,1) we can infer that the girls preferred occupation is in descending
order :
Actor , Dancer Teacher and Scientist equal preference.
U(5x5) = G1 G2 G3 G4 G5
G1 - 0.4639438 0.3694793 0.0623360 0.7586594 0.2622811
G2 - 0.4748203 - 0.6914212 0.390426 - 0.1111717 0.3628923
G3 0.6643306 0.3762704 0.1423082 - 0.4481469 - 0.4427167
G4 - 0.0631701 0.3558972 - 0.2852295 - 0.4525373 0.7636759
G5 - 0.3376036 - 0.3423150 - 0.8614373 0.0803186 - 0.1425443
However only the first coloumn girl set U(:,1) is impt because its associated
sig value = 94 is the highest.
Moreover this set has corresponding pref of occupations as given by V(:,1)
V(:,1) =
- 0.2649413 Snt
- 0.2649413 Ta
- 0.3696952 Da
- 0.8502574 Ac
We can obtain the rating of each girl for this set with A*V(:,1) as below:
->A*V(:,1)
ans =
- 43.622372
- 44.645043
- 62.46377
- 5.9395756
- 31.74322
This tells girl 3 strongly prefers this rating (Actor , Dancer Teacher and Scientist equal
preference )as her score is 62.4 highest in the projection on V(:,1) axis.
A =
Snt Ta Da Ac G3 has most similar rating
G1 12. 12. 18. 36. as given by V(:,1)
G2 11. 11. 13. 40.
G3 17. 17. 25. 52.
G4 2. 2. 4. 4.
G5 8. 8. 10. 28.
3 Verify with published example (Movie Rating by Users)
(ref:
http://rstudio-pubs-
static.s3.amazonaws.com/335300_11d40bf12d8940f78d9661b3c63150dc.html#testing-using-svd-
in-toy-dataset
)
//xxx
https://www.academia.edu/6065432/A_SINGULAR_VALUE_DECOMPOSITION_APPROA
CH_FOR_RECOMMENDATION_SYSTEMS
Now consider user2 movie rating viz
olduser=M(2,:)
This user can be expressed in movie rating eigenspace V co-ords by taking the dot
product of olduser movie ratung and eigen co-ord matrix V
oldusereig= olduser*V
We notice U(2,6) = -.4 . We cannot get this because rank of S is 5, which means that
really only 5 distinct classifications are possible in both user space and movie rating space.
We can ignore the 6th component of U and call it noise.
How to use SVD of movie rating?
We can use SVD to predict a newuser's preference by comparing his incomplete choice
with existing users who are closest to him.
Let us assume that 0 rating means newuser has not seen the movie. Should we recommend
this to him? If so what maybe the strength of recommendation. This is explored below.
userspace=olduser*V*S5^-1
= - 0.5 - 0.3 - 0.6 0.3 0.0
userspace =[ userspace 0]
similarity= userspace*U
We can visualize other examples based on the above analyses . The intuition is that
givn any table of 2 dimension we can think of it as two spaces cols and rows , which
can be separated and connected using matrix M=USV' .
//xxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxx
2 Scilab5.4.4 code
//Expt 1
x1 = int(5*rand(5,1)) ;
x2 = int(5* rand(4,1));
x1=x1(:)
x2=x2(:)
x3 = int(5*rand(5,1)) ;
x4 = int(5* rand(4,1));
x3=x3(:)
x4=x4(:)
sig1 = 3
sig2 =2
[U ,S ,V ] =svd(A)
// Oc=A'*A
// [ oc1 , lc1] = spec(Oc)
// lcs1=sqrt(lc1)
// gg=A*A'
// [ gg1 , lc2] = spec(gg)
// lgg2=sqrt(lc2)
V(:,1)
V(:,2)
A*V(:,1) // girl rating of set V(:,1)
//xxxxxxxxxxxxxxx
//Expt 2
M= [ 1 5 0 5 4;
5 4 4 3 2;
0 4 0 0 5;
4 4 1 4 0;
0 4 3 5 0;
2 4 3 5 3]
//M is 6x 5 matrix col =users row = movies
//Expt 2
M= [ 1 5 0 5 4;
5 4 4 3 2;
0 4 0 0 5;
4 4 1 4 0;
0 4 3 5 0;
2 4 3 5 3]
k=2
Uk =U(1:6, 1:2)Vk
Vk =V(1:5, 1:2)
Sk =S(1:2, 1:2)
M2=Uk*Sk*Vk' // reduced rank 2 approx of M
nuvk(1,1)*Vk(:,1)' +nuvk(1,2)*Vk(:,2)' // recover nu in red dim
//nf=nu*V //for full component in V
//nf*V' // see recovered nu