我有一个与 Sean Laws 示例非常相似的问题,您可以在这里找到: https: //seanlaw.github.io/2019/02/27/set-values-in-sparse-matrix/
就我而言,我想删除稀疏 csr 矩阵中的所有元素,这些元素的绝对值小于某个 epsilon。
首先我尝试了类似的东西
x[abs(x) < 3] = 0
Run Code Online (Sandbox Code Playgroud)
但是 SciPy 关于效率低下的警告让我看到了上面链接中 Sean Laws 的解释。然后我尝试操作他的示例代码,但找不到解决我的问题的方法。
这是代码,添加了一些否定条目。示例代码将删除所有小于 3 的负数条目。我尝试使用 np.abs() 并添加第二个逻辑运算符,但到目前为止尚未成功。
import numpy as np
from scipy.sparse import csr_matrix
x = csr_matrix(np.array([[1, 0.1, -2, 0, 3],
[0, -4, -1, 5, 0]]))
nonzero_mask = np.array(x[x.nonzero()] < 3)[0]
rows = x.nonzero()[0][nonzero_mask]
cols = x.nonzero()[1][nonzero_mask]
x[rows, cols] = 0
print(x.todense())
Run Code Online (Sandbox Code Playgroud)
给出
[[0. 0. 0. 0. 3.]
[0. 0. 0. 5. 0.]]
Run Code Online (Sandbox Code Playgroud)
但我想要的是
[[0. 0. 0. 0. 3.] …Run Code Online (Sandbox Code Playgroud) 我想n x n使用 NumPy 生成随机二进制矩阵,其中:
0或者111例如,一个有效的矩阵可能是
[[1 0 0]
[0 0 1]
[0 1 0]]
Run Code Online (Sandbox Code Playgroud)
而无效的是
[[1 0 0]
[0 0 1]
[0 0 1]]
Run Code Online (Sandbox Code Playgroud)
我尝试执行以下操作,但我无法弄清楚如何使用唯一索引对每列中的值进行混洗。如何生成符合上述约束的矩阵?
N = 10
a = np.zeros((N,N))
a[0,:] = 1
Run Code Online (Sandbox Code Playgroud) 我编写了这段代码来从一系列dom元素中删除一个稀疏数组.当在一个维度中完成代码时,但是在2维中,它会失败.有什么我想念的吗?
23 function initCellHover(){
24 $cells.each(function(){
25 var arrayX = $(this).position().left/cellWidth;
26 var arrayY = $(this).position().top/cellHeight;
27 var arrayValue = $(this);
28 cellLookup[arrayX][arrayY] = arrayValue;
29 });
30 }
Run Code Online (Sandbox Code Playgroud) 我需要一个命令来检查零稀疏矩阵,isempty(..)不起作用.是否有一些稀疏版本的isempty(..)?
>> mlf2=sparse([],[],[],2^31+1,1)
mlf2 =
All zero sparse: 2147483649-by-1
>> isempty(mlf2)
ans =
0 % I waited for 1 here with the zero sparse matrix...
Run Code Online (Sandbox Code Playgroud) 假设我创建了这个稀疏矩阵,其中非零元素由布尔"true"组成:
s =稀疏([3 2 3 3 3 3 2 34 3 6 3 2 3 3 3 3 2 3 3 6],[10235 11470 21211 33322 49297 88361 91470 127422 152383 158751 166485 171471 181211 193321 205548 244609 251470 283673 312384 318752],真正);
其中包含20个元素.Matlab应该分配不超过(4 + 4 + 1)*20 = 180字节的内存(看起来索引长度为4个字节).然而
谁的
说矩阵在内存中占用1275112字节,这是一个问题,因为我需要存储数千个这样的字节.
知道为什么会这样吗?
干杯!
我试图找到一种有效的方法,让我通过一些常数值增加稀疏矩阵的前k值.我目前正在使用以下代码,这对于非常大的矩阵来说非常慢:
a = csr_matrix((2,2)) #just some sample data
a[1,1] = 3.
a[0,1] = 2.
y = a.tocoo()
idx = y.data.argsort()[::-1][:1] #k is 1
for i, j in izip(y.row[idx], y.col[idx]):
a[i,j] += 1
Run Code Online (Sandbox Code Playgroud)
实际上排序似乎很快,问题在于我的最后一个循环,我通过索引排序索引来增加值.希望有人知道如何加快速度.
在编写某个函数的上下文中,我有以下示例矩阵:
temp =
1 2 0 0 1 0
1 0 0 0 0 0
0 1 0 0 0 1
Run Code Online (Sandbox Code Playgroud)
我想获得一个数组,其中每个元素指示从该列开始的所有非零元素中的元素数.如果列为空,则该元素应对应于下一个非空列.对于矩阵temp,结果将是:
result = [1 3 5 5 5 6]
Run Code Online (Sandbox Code Playgroud)
因为第一个非零元素开始第一列,第三列开始第二列,第五列开始第五列,第六列开始第六列.
如何以矢量化方式对任何通用矩阵(可能包含或不包含空列的矩阵)执行此操作?
我一直在尝试将python scipy稀疏矩阵除以其行的矢量和。这是我的代码
sparse_mat = bsr_matrix((l_data, (l_row, l_col)), dtype=float)
sparse_mat = sparse_mat / (sparse_mat.sum(axis = 1)[:,None])
Run Code Online (Sandbox Code Playgroud)
但是,无论我如何尝试,都会引发错误
sparse_mat = sparse_mat / (sparse_mat.sum(axis = 1)[:,None])
File "/usr/lib/python2.7/dist-packages/scipy/sparse/base.py", line 381, in __div__
return self.__truediv__(other)
File "/usr/lib/python2.7/dist-packages/scipy/sparse/compressed.py", line 427, in __truediv__
raise NotImplementedError
NotImplementedError
Run Code Online (Sandbox Code Playgroud)
有人知道我要去哪里哪里吗?
有没有办法将密集张量转换为稀疏张量?显然,Tensorflow的Estimator.fit不接受SparseTensors作为标签.我想将SparseTensors传递给Tensorflow的Estimator.fit的一个原因是能够使用tensorflow ctc_loss.这是代码:
import dataset_utils
import tensorflow as tf
import numpy as np
from tensorflow.contrib import grid_rnn, learn, layers, framework
def grid_rnn_fn(features, labels, mode):
input_layer = tf.reshape(features["x"], [-1, 48, 1596])
indices = tf.where(tf.not_equal(labels, tf.constant(0, dtype=tf.int32)))
values = tf.gather_nd(labels, indices)
sparse_labels = tf.SparseTensor(indices, values, dense_shape=tf.shape(labels, out_type=tf.int64))
cell_fw = grid_rnn.Grid2LSTMCell(num_units=128)
cell_bw = grid_rnn.Grid2LSTMCell(num_units=128)
bidirectional_grid_rnn = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, input_layer, dtype=tf.float32)
outputs = tf.reshape(bidirectional_grid_rnn[0], [-1, 256])
W = tf.Variable(tf.truncated_normal([256,
80],
stddev=0.1, dtype=tf.float32), name='W')
b = tf.Variable(tf.constant(0., dtype=tf.float32, shape=[80], name='b'))
logits = tf.matmul(outputs, W) + b …Run Code Online (Sandbox Code Playgroud) 我在MATLAB中有一个矩阵,我想确定哪一行包含最多的零.也就是说,我想在矩阵中找到最稀疏的行(和列).有mat(sum(mat==0,i)==i,:)什么办法比循环更有效地做到这一点?或者这是首选方法?
我通过使用"最小度排序启发式"来解决线性系统,将其用于LU分解的实现.
sparse-matrix ×10
python ×5
matlab ×4
numpy ×3
scipy ×3
matrix ×2
arrays ×1
javascript ×1
jquery ×1
math ×1
sorting ×1
tensorflow ×1