小编CBo*_*man的帖子

可以将 numpy 数组添加到 python 集合中吗?

我知道为了将一个元素添加到集合中,它必须是可散列的,而 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 数组的切片转换为元组,以便将它们添加到集合中。

所以我一直在试图找出以下内容

  • 有没有什么方法可以使 numpy 数组或类似 numpy 数组(具有向量加法)功能的东西可哈希,以便可以将它们添加到集合中?
  • 如果没有,有没有办法可以加快元组转换的过程?

谢谢你的帮助!

python casting tuples numpy set

7
推荐指数
1
解决办法
5632
查看次数

使用 scipy.sparse.linalg 线性系统求解器的问题

我有一个线性系统要解决,它由大型稀疏矩阵组成。

我一直在使用该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)

python linear-algebra scipy sparse-matrix

5
推荐指数
1
解决办法
850
查看次数

标签 统计

python ×2

casting ×1

linear-algebra ×1

numpy ×1

scipy ×1

set ×1

sparse-matrix ×1

tuples ×1