Search code examples
pythonmathmatrixfactors

How can I factor matrices into specific shapes?


I have a matrix with a shape of (30000, 10). I want to factor this into matrices with the shapes (30000, 512) and (512, 10). Is this possible, and if so, how would I go about doing so in code (ideally python)?

For example, if there was a matrix such as this:

c = np.array([[39, 21],
       [87 , 54],
       [135, 87],
       [183, 120]])

I would want to be able to get two matrices that multiply to get c while specifying the shapes. Say the I wanted to find two matrices with shapes (4,3) and (3,2), I would get these arrays:

a = np.array([[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]])

b = np.array([[2,3],
          [5,6],
          [9,2]])

Solution

  • If your matrix is non-negative, you can use non-negative matrix factorization (NMF) to obtain such a solution. You can refer to sklearn's NMF implementation

    Example:

    import numpy as np 
    from sklearn.decomposition import NMF
    
    x = np.random.rand(30000,10)
    model = NMF(n_components=512, init='random', random_state=0)
    W = model.fit_transform(x) # W.shape = (30000,512)
    H = model.components_ # W.shape = (512,10)