Question

I have an excel file that is 126 rows and 5 columns full of numbers, I have to use that data and SVD methods to predict 5-10 more rows of data. I have implemented SVD in Python successfully using numpy:

import numpy as np from numpy import genfromtxt

my_data = genfromtxt('data.csv', delimiter=',')

U, s, V = np.linalg.svd(my_data)

print ("U:")
print (U)
print ("\nSigma:")
print (s)
print ("\nVT:")
print (V)

which outputs:

U:
[[-0.03339497  0.10018171  0.01013636 ..., -0.10076323 -0.09740801
  -0.08901366]
 [-0.02881809  0.0992715  -0.01239945 ..., -0.02920558 -0.04133748
  -0.06100236]
 [-0.02501102  0.10637736 -0.0528663  ..., -0.0885227  -0.05408083
  -0.01678337]
 ..., 
 [-0.02418483  0.10993637  0.05200962 ...,  0.9734676  -0.01866914
  -0.00870467]
 [-0.02944344  0.10238372  0.02009676 ..., -0.01948701  0.98455034
  -0.00975614]
 [-0.03109401  0.0973963  -0.0279125  ..., -0.01072974 -0.0109425
   0.98929811]]

Sigma:
[ 252943.48015512   74965.29844851   15170.76769244    4357.38062076
    3934.63212778]

VT:
[[-0.16143572 -0.22105626 -0.93558846 -0.14545156 -0.16908786]
 [ 0.5073101   0.40240734 -0.34460639  0.45443181  0.50541365]
 [-0.11561044  0.87141558 -0.07426656 -0.26914744 -0.38641073]
 [ 0.63320943 -0.09361249  0.00794671 -0.75788695  0.12580436]
 [-0.54977724  0.14516905 -0.01849291 -0.35426346  0.74217676]]

But I am not sure how to use this data to preidct my values. I am using this link http://datascientistinsights.com/2013/02/17/single-value-decomposition-a-golfers-tutotial/ as a reference but that is in R. At the end they use R to predict values but they use this command in R:

approxGolf_1 <- golfSVD$u[,1] %*% t(golfSVD$v[,1]) * golfSVD$d[1]

Here is the IdeOne link to the entire R code: http://ideone.com/Yj3y6j

I'm not really familiar with R so can anyone let me know if there is a similar function in Python to the command above or explain what that command is doing exactly?

Thanks.

Was it helpful?

Solution

I will use the golf course example data you linked, to set the stage:

import numpy as np
A=np.matrix((4,4,3,4,4,3,4,2,5,4,5,3,5,4,5,4,4,5,5,5,2,4,4,4,3,4,5))
A=A.reshape((3,9)).T

This gives you the original 9 rows, 3 columns table with scores of 9 holes for 3 players:

matrix([[4, 4, 5],
        [4, 5, 5],
        [3, 3, 2],
        [4, 5, 4],
        [4, 4, 4],
        [3, 5, 4],
        [4, 4, 3],
        [2, 4, 4],
        [5, 5, 5]])

Now the singular value decomposition:

U, s, V = np.linalg.svd(A)

The most important thing to investigate is the vector s of singular values:

array([ 21.11673273,   2.0140035 ,   1.423864  ])

It shows that the first value is much bigger than the others, indicating that the corresponding Truncated SVD with only one value represents the original matrix A quite well. To calculate this representation, you take column 1 of U multiplied by the first row of V, multiplied by the first singular value. This is what the last cited command in R does. Here is the same in Python:

U[:,0]*s[0]*V[0,:]

And here is the result of this product:

matrix([[ 3.95411864,  4.64939923,  4.34718814],
        [ 4.28153222,  5.03438425,  4.70714912],
        [ 2.42985854,  2.85711772,  2.67140498],
        [ 3.97540054,  4.67442327,  4.37058562],
        [ 3.64798696,  4.28943826,  4.01062464],
        [ 3.69694905,  4.3470097 ,  4.06445393],
        [ 3.34185528,  3.92947728,  3.67406114],
        [ 3.09108399,  3.63461111,  3.39836128],
        [ 4.5599837 ,  5.36179782,  5.0132808 ]])

Concerning the vector factors U[:,0] and V[0,:]: Figuratively speaking, U can be seen as a representation of a hole's difficulty, while V encodes a player's strength.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top