小编Ken*_*ste的帖子

为什么我不能在第一个“try:”中将数据分配给稀疏矩阵的一部分?

我想为 crs 稀疏矩阵的一部分分配一个值(我知道它很昂贵,但在我的项目中并不重要)。我尝试将浮点变量分配给稀疏矩阵的一部分,但第一次不起作用。但是,如果我在“除了”中做完全相同的事情,它将完美地工作。

然后我尝试检查稀疏矩阵及其一部分的数据类型,由于某种原因它们是不同的。整个矩阵的数据类型是我指定的 float16,但部分矩阵的数据类型是 float32。

这是两个问题的一个小例子:

from scipy.sparse import csr_matrix
import numpy as np

frame = csr_matrix((10, 10),dtype=np.float16)

print "================\n================ Part 1\n================"
print "Let's assign a value to part of the sparse matrix:"
try:
    frame[0:3,0:3] = np.float16(0.6)
    print "The first attempt worked!"
except:
    print "The first attempt didn't work"

print "let's try again :"

try:
    frame[0:3,0:3] = np.float16(0.6)
    print "The second attempt worked!"
except:
    print "The second attempt didn't work"

print "================\n================ Part 2\n================"
print "Let's check the datatype:" …
Run Code Online (Sandbox Code Playgroud)

numpy scipy sparse-matrix

4
推荐指数
1
解决办法
2118
查看次数

标签 统计

numpy ×1

scipy ×1

sparse-matrix ×1