我尝试通过sparse
包中的lil_matrix
用scipy.sparse
生成随机马尔可夫矩阵:
markov = lil_matrix((n, n), dtype=float)
for i in range(n):
entries = random.randrange(5, 10 + 1, 1)
for j in range(entries):
pos = random.randrange(0, n, 1)
while markov[pos, i] != 0:
pos = random.randrange(0, n, 1)
markov[pos, i] = 1.0 / entries
ones = lil_matrix((n, n), dtype=float)
ones.data[:] = 0.15 * (1.0 / n)
fac = lil_matrix((n, n), dtype=float)
fac.data[:] = (1 - 0.15)
left = markov.multiply(fac) #<- fails!
但是最后一行代码失败:
TypeError: 'float' object is not iterable
那是为什么?