我知道为了将一个元素添加到集合中,它必须是可散列的,而 numpy 数组似乎不是。这给我带来了一些问题,因为我有以下代码:
fill_set = set()
for i in list_of_np_1D:
vecs = i + np_2D
for j in range(N):
tup = tuple(vecs[j,:])
fill_set.add(tup)
# list_of_np_1D is a list of 1D numpy arrays
# np_2D is a 2D numpy array
# np_2D could also be converted to a list of 1D arrays if it helped.
Run Code Online (Sandbox Code Playgroud)
我需要让它运行得更快,将近 50% 的运行时间用于将 2D numpy 数组的切片转换为元组,以便将它们添加到集合中。
所以我一直在试图找出以下内容
谢谢你的帮助!
我有一个线性系统要解决,它由大型稀疏矩阵组成。
我一直在使用该scipy.sparse库及其linalg子库来执行此操作,但我无法让某些线性求解器工作。
这是一个工作示例,它为我重现了该问题:
from numpy.random import random
from scipy.sparse import csc_matrix
from scipy.sparse.linalg import spsolve, minres
N = 10
A = csc_matrix( random(size = (N,N)) )
A = (A.T).dot(A) # force the matrix to be symmetric, as required by minres
x = csc_matrix( random(size = (N,1)) ) # create a solution vector
b = A.dot(x) # create the RHS vector
# verify shapes and types are correct
print('A', A.shape, type(A))
print('x', x.shape, type(x))
print('b', b.shape, type(b)) …Run Code Online (Sandbox Code Playgroud)