- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有这样一本字典,
{(0, 1, 2, 3, 0, 0): 0, (19, 49, 0, 0, 0, 0): 12, (85, 1, 87, 0, 0, 0): 22, (78, 79, 80, 81, 0, 0): 20, (0, 17, 18, 19, 0, 0): 8, (24, 25, 26, 27, 0, 0): 6, (62, 63, 64, 65, 0, 0): 16}
如何将其转换为 coo_matrix?我尝试了以下但我得到了 Error: int object is not subscriptable
data,row, col = [], [], []
for k, v in diction.items():
r = int(k[0][1:])
c = int(k[1][1:])
data.append(v)
row.append(r-1)
col.append(c-1)
# Create the COO-matrix
coo = coo_matrix((data,(row,col)))
我需要这样做,因为 LightFM.fit 方法只接受 coo 矩阵作为参数。
预期输出(coo矩阵):
(0, 1, 2, 3, 0, 0) 0
(19, 49, 0, 0, 0, 0) 12
(85, 1, 87, 0, 0, 0) 22
最佳答案
正如其他人在评论中指出的那样,coo_matrix()
期望坐标在2 维 中:行
和列
。 data
值存储实际数据值,位于相应坐标 中。这也反射(reflect)在 LightFM.fit() 中。文档。
这个概念可能不清楚,我将尝试做出不同于文档中给出的解释的另一种解释:三个输入 data、row 和 column 必须有相同的长度;并且是一维的。
每个坐标通常分别通过索引i 和j、行索引和列索引来引用,因为它们表示第 i 行和 j'第 列(matrix_row[i]
和 matrix_column[j]
)。
借用 coo_matrix() 中的示例文档:
row = np.array([0, 3, 1, 0])
col = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])
for value, i, j in zip(data, row, col):
print("In the {}'th row, on the {}'th column, insert the value {}"
.format(i, j, value))
print("All other values are 0, because it's sparse.")
coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
输出:
In the 0'th row, on the 0'th column, insert the value 4
In the 3'th row, on the 3'th column, insert the value 5
In the 1'th row, on the 1'th column, insert the value 7
In the 0'th row, on the 2'th column, insert the value 9
All other values are 0, because it's sparse.
array([
[4, 0, 9, 0],
[0, 7, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 5]
])
代码注释:
Error: int object is not subscriptable
错误很可能来自您的代码,您在其中尝试下标 k
,这是您的键,例如您的第一个 k
将是 (0, 1, 2, 3, 0, 0)
。
当你执行 r=int(k[0][1:])
时,你会尝试获取 0[1:]
(因为 中的 zero'eth 条目code>k
is 0
. Similarily for c = int(k[1][1:])
, k[1]
是 1
,所以 k[1][1:]
正在尝试执行 1[1:]
。
此外,执行 int()
将不起作用。如果您想做的是转换列表中的每个元素,请使用 numpy.array.astype()
。例如。 np.array([1.2, 3, 4.4]).astype(int)
将给出 array([1, 3, 4])
。
关于python - 将字典转换为 coo_matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47852335/
我尝试使用另一个矩阵实例化一个 coo_matrix。当我尝试打印 coo_matrix 时,输出为: (1, 9) 1.0 (1, 10) 1.0 (1, 11) 1.0 (1, 25) 1.0
我有这样一本字典, {(0, 1, 2, 3, 0, 0): 0, (19, 49, 0, 0, 0, 0): 12, (85, 1, 87, 0, 0, 0): 22, (78, 79, 80, 8
我有 coo_matrix X 和索引 trn_idx,我想通过它们访问该 maxtrix print (type(X ), X.shape) print (type(trn_idx), trn_i
我使用 coo_matrix 格式在 Python 中构建了一些稀疏矩阵 M。我想找到一种有效的计算方法: A = M + M.T - D 其中D是M对其对角线的限制(M可能非常大)。我找不到一种在保
这是一个非常简单的问题。对于像 coo_matrix 这样的 SciPy 稀疏矩阵,如何访问单个元素? 类比本征线性代数库。可以使用 coeffRef 访问元素 (i,j),如下所示: myMatri
我有一个文档术语矩阵,是用语料库中术语的共现构建的,如所解释的 here : vocabulary = {} # map terms to column indices data = []
我有一个包含 10 万个项目的列表,每个项目都有一个索引列表。我试图将其放入一个用于向量乘法的 bool 稀疏矩阵。我的代码运行速度没有我希望的那么快,因此我正在寻找性能提示或者可能是将这些数据放入矩
我正在尝试从一系列存在一些重叠索引的数组创建一个稀疏向量。对于矩阵来说,有一个非常convenient object in scipy这正是这样做的: coo_matrix((data, (i, j)
你好,Python 社区, 我有关于 numpy 稀疏矩阵 COO 格式的问题。如下: 我有一个csv 4 列文件 a , b , c , d我需要从这个 csv 文件形成 SciPy COO_Mat
我正在致力于将 CUSP 求解器集成到现有的 FORTRAN 代码中。作为第一步,我只是尝试从 FORTRAN 中传入一对整数数组和一个 float (FORTRAN 中的实数 * 4),它们将用于构
我有一个形状为 (40106, 2048) 的 coo_matrix a 和一个列 numpy 数组 b形状 (40106,)。 我想做的是简单地连接矩阵和数组(即生成的数据结构的形状为 (40106
给定一个形状为 1.000.000 x 70.000 的 scipy.sparse.coo_matrix 类型的稀疏矩阵R 我想通了 row_maximum = max(R.getrow(i).dat
我有一个 coo_matrix: from scipy.sparse import coo_matrix coo = coo_matrix((3, 4), dtype = "int8") 我想转换为
我想到了使用 coo_matrix.nonzero(),它返回两个数组的元组,其中包含给定矩阵中非零条目的索引。文档中的示例指出: >>> from scipy.sparse import coo_m
对于一个矩阵,我想找到全为零的列并用1填充,然后按列对矩阵进行归一化。我知道如何使用 np.arrays 做到这一点 [[0 0 0 0 0] [0 0 1 0 0] [1 0 0 1 0] [
我正在尝试制作一个 cusp::coo_matrix 的 vector ,但似乎不能以这种方式使用 thrust::host_vector。考虑这段代码: int main(void) { t
我是一名优秀的程序员,十分优秀!