sparse-matrix

Matrix power for sparse matrix in python

大憨熊 提交于 2019-11-27 08:18:30
问题 I am trying to find out a way to do a matrix power for a sparse matrix M: M^k = M*...*M k times where * is the matrix multiplication (numpy.dot), and not element-wise multiplication . I know how to do it for a normal matrix: import numpy as np import scipy as sp N=100 k=3 M=(sp.sparse.spdiags(np.ones(N), 0, N, N)-sp.sparse.spdiags(np.ones(N), 2, N, N)).toarray() np.matrix_power(M,k) How can I do it for sparse M: M=(sp.sparse.spdiags(np.ones(N), 0, N, N)-sp.sparse.spdiags(np.ones(N), 2, N, N))

Creating (and Accessing) a Sparse Matrix with NA default entries

こ雲淡風輕ζ 提交于 2019-11-27 07:40:57
After learning about the options for working with sparse matrices in R , I want to use the Matrix package to create a sparse matrix from the following data frame and have all other elements be NA . s r d 1 1089 3772 1 2 1109 190 1 3 1109 2460 1 4 1109 3071 2 5 1109 3618 1 6 1109 38 7 I know I can create a sparse matrix with the following, accessing elements as usual: > library(Matrix) > Y <- sparseMatrix(s,r,x=d) > Y[1089,3772] [1] 1 > Y[1,1] [1] 0 but if I want to have the default value to be NA, I tried the following: M <- Matrix(NA,max(s),max(r),sparse=TRUE) for (i in 1:nrow(X)) M[s[i],r[i]

How to convert a sparse matrix into a matrix of index and value of non-zero element

血红的双手。 提交于 2019-11-27 06:39:27
问题 We can construct a sparse matrix from an index and value of non-zero element with the sparseMatrix or spMatrix . Is there any function convert a sparse matrix back to an index and value of all non-zero element? For example i <- c(1,3,5); j <- c(1,3,4); x <- 1:3 A <- sparseMatrix(i, j, x = x) B <- sparseToVector(A) ## test case: identical(B,cbind(i,j,x)) Is there any function do a similar job as sparseToVector ? 回答1: summary(A) # 5 x 4 sparse Matrix of class "dgCMatrix", with 3 entries # i j x

Iterating through a scipy.sparse vector (or matrix)

ぃ、小莉子 提交于 2019-11-27 06:37:30
I'm wondering what the best way is to iterate nonzero entries of sparse matrices with scipy.sparse. For example, if I do the following: from scipy.sparse import lil_matrix x = lil_matrix( (20,1) ) x[13,0] = 1 x[15,0] = 2 c = 0 for i in x: print c, i c = c+1 the output is 0 1 2 3 4 5 6 7 8 9 10 11 12 13 (0, 0) 1.0 14 15 (0, 0) 2.0 16 17 18 19 so it appears the iterator is touching every element, not just the nonzero entries. I've had a look at the API http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.lil_matrix.html and searched around a bit, but I can't seem to find a solution

Efficiently create sparse pivot tables in pandas?

廉价感情. 提交于 2019-11-27 06:30:45
I'm working turning a list of records with two columns (A and B) into a matrix representation. I have been using the pivot function within pandas, but the result ends up being fairly large. Does pandas support pivoting into a sparse format? I know I can pivot it and then turn it into some kind of sparse representation, but isn't as elegant as I would like. My end goal is to use it as the input for a predictive model. Alternatively, is there some kind of sparse pivot capability outside of pandas? edit: here is an example of a non-sparse pivot import pandas as pd frame=pd.DataFrame() frame[

Sum over rows in scipy.sparse.csr_matrix

China☆狼群 提交于 2019-11-27 06:27:41
问题 I have a big csr_matrix and I want to add over rows and obtain a new csr_matrix with the same number of columns but reduced number of rows. (Context: The matrix is a document-term matrix obtained from sklearn CountVectorizer and I want to be able to quickly combine documents according to codes associated with these documents) For a minimal example, this is my matrix: import numpy as np from scipy.sparse import csr_matrix from scipy.sparse import vstack row = np.array([0, 4, 1, 3, 2]) col = np

large-scale regression in R with a sparse feature matrix

风格不统一 提交于 2019-11-27 05:30:17
问题 I'd like to do large-scale regression (linear/logistic) in R with many (e.g. 100k) features, where each example is relatively sparse in the feature space---e.g., ~1k non-zero features per example. It seems like the SparseM package slm should do this, but I'm having difficulty converting from the sparseMatrix format to a slm -friendly format. I have a numeric vector of labels y and a sparseMatrix of features X \in {0,1}. When I try model <- slm(y ~ X) I get the following error: Error in model

Sparse Matrix from a dense one Tensorflow

非 Y 不嫁゛ 提交于 2019-11-27 04:43:24
问题 I am creating a convolutional sparse autoencoder and I need to convert a 4D matrix full of values (whose shape is [samples, N, N, D] ) into a sparse matrix. For each sample, I have D NxN feature maps. I want to convert each NxN feature map to a sparse matrix, with the maximum value mapped to 1 and all the others to 0. I do not want to do this at run time but during the Graph declaration (because I need to use the resulting sparse matrix as an input to other graph operations), but I do not

Pandas sparse dataFrame to sparse matrix, without generating a dense matrix in memory

末鹿安然 提交于 2019-11-27 04:39:21
Is there a way to convert from a pandas.SparseDataFrame to scipy.sparse.csr_matrix , without generating a dense matrix in memory? scipy.sparse.csr_matrix(df.values) doesn't work as it generates a dense matrix which is cast to the csr_matrix . Thanks in advance! Pandas docs talks about an experimental conversion to scipy sparse, SparseSeries.to_coo: http://pandas-docs.github.io/pandas-docs-travis/sparse.html#interaction-with-scipy-sparse ================ edit - this is a special function from a multiindex, not a data frame. See the other answers for that. Note the difference in dates. =========

Scipy.sparse.csr_matrix: How to get top ten values and indices?

六月ゝ 毕业季﹏ 提交于 2019-11-27 04:37:38
问题 I have a large csr_matrix and I am interested in the top ten values and their indices each row. But I did not find a decent way to manipulate the matrix. Here is my current solution and the main idea is to process them row by row: row = csr_matrix.getrow(row_number).toarray()[0].ravel() top_ten_indicies = row.argsort()[-10:] top_ten_values = row[row.argsort()[-10:]] By doing this, the advantages of csr_matrix is not fully used. It's more like a brute force solution. 回答1: I don't see what the