sparse-matrix

In Matlab, is there a way to copy lower triangular half of a matrix to upper triangular half?

落花浮王杯 提交于 2019-12-13 01:23:06
问题 In Matlab, is there a way to copy lower triangular half of a matrix to upper triangular half? For a square matrix A, I want to be able to do triu(A)=tril(A)'; in order to set all A(i,j) as A(j,i) for i > j. Is there a convenient/efficient way to do this? Note: Preferably, the answer can apply to sparse matrices. On the topic of efficiency, I did some testing regarding the relative time cost for accessing part of a matrix. I used version R2014a Some results: logical indexing is painfully slow

Sparse matrix multiplication in MATLAB with spfun

主宰稳场 提交于 2019-12-13 00:49:02
问题 I have a dense column matrix y of size (m,1) and a sparse matrix x of size (m,n) . I want to do element-wise multiplication using y and every column of x . The resultant sparse matrix is still of size (m,n) . Sparse matrix x , when loaded into memory, is about 10GB. Can spfun help me accomplish my goal in a memory efficient manner? I am having difficulties understanding the logic behind it. Thank you. 回答1: Have you tried bsxfun? out = bsxfun( @times, x, y ); spfun is more suitable for element

Extending / converting a sparse matrix into a larger sparse matrix

你离开我真会死。 提交于 2019-12-13 00:40:28
问题 I am aware that the title of this question is confusing, if not wrong. Sorry for this, let me explain what I try to do: # I have a population of individuals: population <- c("Adam", "Bob", "Chris", "Doug", "Emily", "Frank", "George","Harry", "Isaac", "Jim", "Kyle", "Louis") population_size <- length(population) # this is 12 # I then draw a sample from this population mysample_size <- 5 mysample <- sample(population,mysample_size, replace=FALSE) # I then simulate a network among the people in

How to get the blocks back from a scipy sparse block matrix?

前提是你 提交于 2019-12-13 00:24:55
问题 After some vectorized calculations, I get a sparse block matrix with all my results stacked in blocks of same size. >>> A = [[1, 1], ... [1, 1]] >>> B = [[2, 2], ... [2, 2]] >>> C = [[3, 3], ... [3, 3]] >>> results = scipy.sparse.block_diag(A, B, C) >>> print(results.toarray()) [[1 1 0 0 0 0] [1 1 0 0 0 0] [0 0 2 2 0 0] [0 0 2 2 0 0] [0 0 0 0 3 3] [0 0 0 0 3 3]] How can I get back these arrays A,B,C in an efficient way, if necessery by providing their shape (2,2)? 回答1: In [177]: >>> A = [[1,

MATLAB How to fill individual entries of a sparse matrix using vectorised form?

╄→гoц情女王★ 提交于 2019-12-13 00:14:54
问题 I have a sparse matrix and I need to fill certain entries with a specific value, I am using a for loop right now but I know its not the correct way to do it so I was wondering if its possible to vectorise this for loop? K = sparse(N); for i=vectorofrandomintegers K(i,i) = 1; end If I vectorise it normally as so: K(A,A) = 1; then it fills all the entries in each row denoted by A whereas I want individual entries (i.e. K(1,1) = 1 or K(6,6)=1 ). Also, the entries are not diagonally adjacent so I

Tensorflow: Gradient Calculation with sparse tensors on GPU

穿精又带淫゛_ 提交于 2019-12-12 21:23:05
问题 I built up a tensorflow model similar to the GPU Implementation of CIFAR10. I have a basic model that is executed on every GPU while the variables for the network are on the CPU. Everything works fine as long as I don't use sparse tensors as weight matrices in the layers. My sparse weight matrices are constructed with the function tf.sparse_to_dense() or tf.diag() . When I run it on the CPU everything works fine, but when I run it on the GPU I get the message that there is no GPU

Element wise exp() of scipy sparse matrix

被刻印的时光 ゝ 提交于 2019-12-12 16:27:31
问题 I have a very big sparse csc_matrix x . I want to do elementwise exp() on it. Basically what I want is to get the same result as I would have got with numpy.exp(x.toarray()) . But I can't do that(my memory won't allow me to convert the sparse matrix into an array). Is there any way out? Thanks in advance! 回答1: If you don't have the memory to hold x.toarray() , you don't have the memory to hold the output you're asking for. The output won't be sparse; in fact, unless your input has negative

Commute numpy sparse matrix dot product

别等时光非礼了梦想. 提交于 2019-12-12 16:23:13
问题 To my understanding, numpy.sparse.csr_sparse.dot(other) does multiply other to my sparse matrix from the right : A = numpy.sparse.csr_sparse(something) B = numpy.matrix(something) C = A.dot(B) # C = A*B How do I commute the two matrices to get B*A without losing the benefits of saving my matrix as a sparse one (i.e. .todense() etc.)? 回答1: A little refresher of matrix multiplication properties: D = B * A D.T = A.T * B.T D = (A.T * B.T).T Which then leads to the obvious: D = A.T.dot(B.T).T Note

Efficiently compute columnwise sum of sparse array where every non-zero element is 1

半城伤御伤魂 提交于 2019-12-12 15:57:53
问题 I have a bunch of data in SciPy compressed sparse row (CSR) format. Of course the majority of elements is zero, and I further know that all non-zero elements have a value of 1. I want to compute sums over different subsets of rows of my matrix. At the moment I am doing the following: import numpy as np import scipy as sp import scipy.sparse # create some data with sparsely distributed ones data = np.random.choice((0, 1), size=(1000, 2000), p=(0.95, 0.05)) data = sp.sparse.csr_matrix(data,

non-NDFFrame object error using pandas.SparseSeries.from_coo() function

喜欢而已 提交于 2019-12-12 13:04:00
问题 I am trying to convert a COO type sparse matrix (from Scipy.Sparse) to a Pandas sparse series. From the documentation(http://pandas.pydata.org/pandas-docs/stable/sparse.html) it says to use the command SparseSeries.from_coo(A) . This seems to be OK, but when I try to see the series' attributes, this is what happens. 10x10 seems OK. import pandas as pd import scipy.sparse as ss import numpy as np row = (np.random.random(10)*10).astype(int) col = (np.random.random(10)*10).astype(int) val = np