【问题标题】:How can I factor matrices into specific shapes?如何将矩阵分解为特定形状?
【发布时间】:2021-04-02 06:50:32
【问题描述】:

我有一个形状为 (30000, 10) 的矩阵。我想将其分解为形状为 (30000, 512) 和 (512, 10) 的矩阵。这是否可能,如果可以,我将如何在代码(理想情况下是 python)中这样做?

例如,如果有这样的矩阵:

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

我希望能够在指定形状的同时获得两个相乘的矩阵以获得c。假设我想找到两个形状为(4,3)(3,2) 的矩阵,我会得到这些数组:

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

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

【问题讨论】:

  • 你能给我们举个小矩阵的例子吗?

标签: python math matrix factors


【解决方案1】:

如果您的矩阵是非负的,您可以使用非负矩阵分解 (NMF) 来获得这样的解决方案。可以参考sklearn's NMF implementation

例子:

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)

【讨论】:

  • W = model.fit_transform(a) 应该是 W = model.fit_transform(x) 吗?
  • 是的。抱歉打错了。已更正!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
  • 2020-01-15
  • 1970-01-01
  • 2011-05-09
相关资源
最近更新 更多