sparse-matrix

Row Division in Scipy Sparse Matrix

百般思念 提交于 2020-01-28 10:55:31
问题 I want to divide a sparse matrix's rows by scalars given in an array. For example : I have a csr_matrix C : C = [[2,4,6], [5,10,15]] D = [2,5] I want the result of C after division to be : result = [[1, 2, 3], [1, 2, 3]] I have tried this using the method that we use for numpy arrays : result = C / D[:,None] But this seems really slow. How to do this efficiently in sparse matrices ? 回答1: Approach #1 Here's a sparse matrix solution using manual replication with indexing - from scipy.sparse

pandas dataframe to coo matrix and to lil matix

♀尐吖头ヾ 提交于 2020-01-25 06:50:10
问题 I have following series: groups['combined'] 0 (28, 1) 1 1 (32, 1) 1 2 (36, 1) 1 3 (37, 1) 1 4 (84, 1) 1 .... Name: combined, Length: 14476, dtype: object How can I convert this dataframe into .tocoo() matrix and .tolil() ? Reference how combined column is formed from Original Pandas DataFrame: import pandas as pd pd.DataFrame ({0:[28,32,36,37,84],1: [1,1,1,1,1], 2: [1,1,1,1,1]}) . col 0 has over 10K unique features, col 1 has 39 groups and col 2 is just 1. 回答1: Formation of COOrdinate format

Sparse Matrix in AMPL

天涯浪子 提交于 2020-01-25 03:53:26
问题 I have a sparse matrix in AMPL. As a result, it includes a lot of values that are coded as ".". The "." value in AMPL means "no value specified here." When I try to solve the optimization problem I get a message reading "no value specified for..." in reference to the cells containing the "." consequently, it won't solve the problem. However, when I try to specify a default value to replace the ".", the problem churns and churns and doesn't solve. Is there any way I can set restrictions on the

Fast slicing and multiplication of scipy sparse CSR matrix

北慕城南 提交于 2020-01-24 01:46:48
问题 I have a scipy sparse CSR matrix of size 2M x 50k with 200M non-zero values (100 per row). I need to slice 120k rows of it by a (randomly distributed) index (which is a pandas Series ) and then multiply that submatrix by a sparse vector of size 1x50k (with 100 non-zero values as well). I do this: slice = matrix[index.tolist(), :] result = slice.dot(vector.T).T.toarray()[0] # returns 1x120k array The slicing takes 0.7s (slow) and then multiplication takes 0.05s . Instead, I can multiply the

In Julia, How can I column-normalize a sparse matrix?

怎甘沉沦 提交于 2020-01-23 08:34:26
问题 If I have constructed a sparse matrix using the sparse(i, j, k) constructor, how can I then normalize the columns of the matrix (so that each column sums to 1)? I cannot efficiently normalize the entries before I create the matrix, so any help is appreciated. Thanks! 回答1: The easiest way would be a broadcasting division by the sum of the columns: julia> A = sprand(4,5,.5) A./sum(A,1) 4x5 Array{Float64,2}: 0.0 0.0989976 0.0 0.0 0.0795486 0.420754 0.458653 0.0986313 0.0 0.0 0.0785525 0.442349 0

scipy sparse matrix to cvxopt spmatrix?

佐手、 提交于 2020-01-23 06:32:49
问题 I need to convert a scipy sparse matrix to cvxopt's sparse matrix format, spmatrix, and haven't come across anything yet (the matrix is too big to be converted to dense, of course). Any ideas how to do this? 回答1: The more robust answer is a combination of hpaulj's answer and OferHelman's answer. def scipy_sparse_to_spmatrix(A): coo = A.tocoo() SP = spmatrix(coo.data.tolist(), coo.row.tolist(), coo.col.tolist(), size=A.shape) return SP Defining the shape variable preserves the dimensionality

scipy sparse matrix to cvxopt spmatrix?

。_饼干妹妹 提交于 2020-01-23 06:31:25
问题 I need to convert a scipy sparse matrix to cvxopt's sparse matrix format, spmatrix, and haven't come across anything yet (the matrix is too big to be converted to dense, of course). Any ideas how to do this? 回答1: The more robust answer is a combination of hpaulj's answer and OferHelman's answer. def scipy_sparse_to_spmatrix(A): coo = A.tocoo() SP = spmatrix(coo.data.tolist(), coo.row.tolist(), coo.col.tolist(), size=A.shape) return SP Defining the shape variable preserves the dimensionality

Division of sparse matrix

寵の児 提交于 2020-01-19 17:39:16
问题 I have a scipy.sparse matrix with 45671x45671 elements. In this matrix, some rows contain only '0' value. My question is, how to divide each row values by the row sum. Obviously, with for loop it's work, but I look for an efficient method... I already tried : matrix / matrix.sum(1) but I have MemoryError issue. matrix / scs.csc_matrix((matrix.sum(axis=1))) but ValueError: inconsistent shapes Other wacky things... Moreover, I want to skip rows with only '0' values. So, if you have any solution

Division of sparse matrix

半城伤御伤魂 提交于 2020-01-19 17:39:12
问题 I have a scipy.sparse matrix with 45671x45671 elements. In this matrix, some rows contain only '0' value. My question is, how to divide each row values by the row sum. Obviously, with for loop it's work, but I look for an efficient method... I already tried : matrix / matrix.sum(1) but I have MemoryError issue. matrix / scs.csc_matrix((matrix.sum(axis=1))) but ValueError: inconsistent shapes Other wacky things... Moreover, I want to skip rows with only '0' values. So, if you have any solution

sklearn tsne with sparse matrix

纵然是瞬间 提交于 2020-01-17 07:06:25
问题 I'm trying to display tsne on a very sparse matrix with precomputed distances values but I'm having trouble with it. It boils down to this: row = np.array([0, 2, 2, 0, 1, 2]) col = np.array([0, 0, 1, 2, 2, 2]) distances = np.array([.1, .2, .3, .4, .5, .6]) X = csc_matrix((distances, (row, col)), shape=(3, 3)) Y = TSNE(metric='precomputed').fit_transform(X) However, I get this error: TypeError: A sparse matrix was passed, but dense data is required for method="barnes_hut". Use X.toarray() to