Q:

Need function to re-construct matrix E from the partial product

The task is this:
Let assume that components with low FOV come from an experimental noise. We would like to remove this noise to achieve a higher signal-to-noise ratio. In order to do so we can re-construct matrix E from the partial product
E ̄ = U (f) D (f) V^T (f), 
where the subscript (f) referes to dropping the last columns. Complete function refine expression which, using SVD, select only those first principal components which explain together at least 90% of the variance and reconstruct matrix E with formula above.
I tried it like this:
def refine_expression(F):
"""
Call:
   Fbar = refine_expression(F)
Input argument:
   F: numpy array (2-d matrix; centered)
Output arguments:
   Fbar: numpy array (2-d matrix) 
Example:
   E,rn,cn = load_data('expressionSet1.dat')
   F = transform(E)
   Fbar = refine_expression(F)
   =>
   Fbar:
   array([[ -0.26696566,  5.27928198,  0.03159005, ...,  0.65700363,
     0.26819583,  0.1807512],
           ...
          [ 0.24213939, -0.48004957, -1.2858063 , ..., -1.18645038,
    -2.01918948,  1.34124707]])

   Ebar.shape == E.shape % test for correctness
   =>
   True
"""
U,d,V = svd(E,full_matrices=False)
n = len(d)
Fbar = dot(dot(U[:,1:n],diag(d[1:n])),V[:,1:n].T)
return(Fbar)

But this refers to E ̄ = U (-1) D (-1) V^T (-1). So I don't know how to integrate the f in my original function, can someone help me out?

A:

The singular values are given in descending order, so all you need to do is index a slice of the first f values along the 'inner' dimensions of each matrix:
# E is (M, N), U is (M, K), s is (K,) and Vt is (K, N), where K = min(M, N)
# f is an integer such that 0 < f < K

U, s, Vt = np.linalg.svd(E, full_matrices=False)
Fbar = U[:, :f].dot(np.diag(s[:f])).dot(Vt[:f, :])

Note that np.linalg.svd returns the second unitary matrix V in a transposed form, so there is no need to transpose it again before computing the dot product.

