【问题标题】:Creating a COO matrix from a 2D numpy array从 2D numpy 数组创建 COO 矩阵
【发布时间】:2017-12-18 05:07:54
【问题描述】:

我有一个像这样的二维 numpy 数组,

[[3, 4, 5, 6], [4, 5, 6, 7], [9, 10, 3, 5]]

我使用以下代码将其转换为 COO 矩阵:

# Flatten 2D array
data = np.asarray(twod_array).flatten()
row = np.arange(0, len(data))
col = np.arange(0, len(row))
# Make COO matrix
mat = coo_matrix((data, (row, col)), shape=(len(row), len(row)))

这是将 2D numpy 数组转换为 COO 矩阵的正确方法吗?

编辑

我想要做的是,我在一个列上有零件,在另一列上有项目。

parts                                 item
processor, display, sensor            temp. monitoring system
fan baldes, motor, sensor             motion detecting fan
        .                                       .
        .                                       .

我已将上述数据转换为数字,以便进一步处理。

parts         items
1, 2, 3       1
4, 5, 3       2

所以现在,我想将上述数据输入 LightFM,所以我创建了一个像这样的 2D 数组。

[[1, 2, 3, 1], [4, 5, 3, 2]]

但是由于 LightFM 的 fit 方法只接受形状为 [n_users, n_items] 的 np.float32 coo_matrix,它是一个包含用户-项目交互的矩阵。我使用上述方法转换了二维数组。

【问题讨论】:

  • 为什么你觉得这不正确?这会导致错误吗?
  • @cᴏʟᴅsᴘᴇᴇᴅ 没有错误。我正在用它来训练一个 LightFM 模型,模型生成的建议很奇怪。
  • 可以使用mat.A查看。您的预期输出是什么?
  • 我有一种感觉,你正在跳入 LightFM,却不了解它的预期,甚至不了解稀疏矩阵是什么。在您之前的问题中,您尝试从字典中制作一个 6d 稀疏矩阵。
  • @hpaulj 是的,你是对的,但是当我检查 LightFM 的文档时,它只说它需要一个稀疏矩阵作为 fit 方法的参数。我是新手,请原谅我

标签: python arrays numpy matrix


【解决方案1】:
In [301]: A = np.array([[3, 4, 5, 6], [4, 5, 6, 7], [9, 10, 3, 5]])
In [302]: A
Out[302]: 
array([[ 3,  4,  5,  6],
       [ 4,  5,  6,  7],
       [ 9, 10,  3,  5]])

您创建矩阵的方式:

In [305]: data =A.flatten()
In [306]: M = sparse.coo_matrix((data,(np.arange(len(data)),np.arange(len(data))
     ...: )))
In [307]: M
Out[307]: 
<12x12 sparse matrix of type '<class 'numpy.int32'>'
    with 12 stored elements in COOrdinate format>

print(M) 将显示这 12 个值及其坐标。

如果不是太大,我喜欢将矩阵显示为数组。 M.AM.toarray() 的捷径:

In [308]: M.A
Out[308]: 
array([[ 3,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  4,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  5,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  6,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  4,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  5,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  6,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  7,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  9,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0, 10,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  3,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  5]])

查看对角线 - 这是原始数组的 12 个值。那是你要的吗? A 的原始 3x4 布局完全丢失了。它也可能是这 12 个数字的一​​维列表。

或者,您可以将数组传递给稀疏构造函数,生成原始的稀疏副本

In [309]: M1 = sparse.coo_matrix(A)
In [310]: M1
Out[310]: 
<3x4 sparse matrix of type '<class 'numpy.int32'>'
    with 12 stored elements in COOrdinate format>
In [311]: M1.A
Out[311]: 
array([[ 3,  4,  5,  6],
       [ 4,  5,  6,  7],
       [ 9, 10,  3,  5]])

这是一个没有任何 0 的 3x4 数组,而不是 12x12 对角线。如果 A 已经有很多 0,这就更有意义了。

你真的知道你需要什么样的稀疏矩阵吗?

【讨论】:

  • 我试过了,但我得到了ValueError("Not all estimated parameters are finite," ValueError: Not all estimated parameters are finite, your model may have diverged. Try decreasing the learning rate or normalising feature values and sample weights
猜你喜欢
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多