# Singular Value Decomposition (SVD)

`In : import numpy as npIn : a = np.random.rand(20000, 100)In : %time U, s, V = np.linalg.svd(a)CPU times: user 4min 3s, sys: 10.2 s, total: 4min 13sWall time: 1min 18s`
`In : import mars.tensor as mtIn : a = mt.random.rand(20000, 100, chunk_size=100)In : %time U, s, V = mt.linalg.svd(a).execute()CPU times: user 5.42 s, sys: 1.49 s, total: 6.91 sWall time: 1.87 s`

# Principal Component Analysis (PCA)

`import numpy as npa = np.random.randint(0, 256, size=(10000, 100))a_mean = a.mean(axis=1, keepdims=True)a_new = a - a_meancov_a = (a_new.dot(a_new.T)) / (a.shape - 1)# Use SVD to find the first 20 feature values in the covariance matrixU, s, V = np.linalg.svd(cov_a)V = V.Tvecs = V[:, :20]# Use low-dimensional feature vectors to represent the original dataa_transformed = a.dot(vecs)`
`import mars.tensor as mta = mt.random.randint(0, 256, size=(10000, 100))a_mean = a.mean(axis=1, keepdims=True)a_new = a - a_meancov_a = (a_new.dot(a_new.T)) / (a.shape - 1)# Use SVD to find the first 20 feature values in the covariance matrixU, s, V = mt.linalg.svd(cov_a)V = V.Tvecs = V[:, :20]# Use low-dimensional feature vectors to represent the original dataa_transformed = a.dot(vecs).execute()`

# Facial Recognition

`import mars.tensor as mtfrom mars.session import new_sessionsession = new_session()def cov(x):    x_new = x - x.mean(axis=1, keepdims=True)    return x_new.dot(x_new.T) / (x_new.shape - 1)def pca_compress(data_mat, k):    data_mean = mt.mean(data_mat, axis=0, keepdims=True)    data_new = data_mat - data_mean        cov_data = cov(data_new)    U, s, V = mt.linalg.svd(cov_data)    V = V.T    vecs = V[:, :k]    data_transformed = vecs.T.dot(data_new)    return session.run(data_transformed, data_mean, vecs)`
`# Find the Euclidean distancedef compare(vec1, vec2):    distance = mt.dot(vec1, vec2) / (mt.linalg.norm(vec1) * mt.linalg.norm(vec2))    return distance.execute()`

--

--