Split a sparse matrix into chunks without converting to dense2019 Community Moderator ElectionError Converting Sparse Matrix to Array with scipy.sparse.csc_matrix.toarray()How do you split a list into evenly sized chunks?How to elementwise-multiply a scipy.sparse matrix by a broadcasted dense 1d array?Load sparse scipy matrix into existing numpy dense matrixAccess value, column index, and row_ptr data from scipy CSR sparse matrixScipy Sparse Matrix - Dense Vector Multiplication Performance - Blocks vs Large Matrixstacking sparse and dense matricesNumpy / Scipy - Sparse matrix to vectorPopulate a Pandas SparseDataFrame from a SciPy Sparse Coo MatrixMultiply each row of a scipy sparse matrix by an element of an array, without using multiplySparse matrix matrix power in python
Differential and Linear trail propagation in Noekeon
Violin - Can double stops be played when the strings are not next to each other?
How to generate binary array whose elements with values 1 are randomly drawn
HP P840 HDD RAID 5 many strange drive failures
Variable completely messes up echoed string
In what cases must I use 了 and in what cases not?
What should I install to correct "ld: cannot find -lgbm and -linput" so that I can compile a Rust program?
How to terminate ping <dest> &
What can I do if I am asked to learn different programming languages very frequently?
What is the significance behind "40 days" that often appears in the Bible?
Does multi-classing into Fighter give you heavy armor proficiency?
I seem to dance, I am not a dancer. Who am I?
What favor did Moody owe Dumbledore?
Four married couples attend a party. Each person shakes hands with every other person, except their own spouse, exactly once. How many handshakes?
Fewest number of steps to reach 200 using special calculator
Relation between independence and correlation of uniform random variables
Is there a term for accumulated dirt on the outside of your hands and feet?
Matrix using tikz package
Usage and meaning of "up" in "...worth at least a thousand pounds up in London"
How does one measure the Fourier components of a signal?
How is the partial sum of a geometric sequence calculated?
Maths symbols and unicode-math input inside siunitx commands
Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?
Worshiping one God at a time?
Split a sparse matrix into chunks without converting to dense
2019 Community Moderator ElectionError Converting Sparse Matrix to Array with scipy.sparse.csc_matrix.toarray()How do you split a list into evenly sized chunks?How to elementwise-multiply a scipy.sparse matrix by a broadcasted dense 1d array?Load sparse scipy matrix into existing numpy dense matrixAccess value, column index, and row_ptr data from scipy CSR sparse matrixScipy Sparse Matrix - Dense Vector Multiplication Performance - Blocks vs Large Matrixstacking sparse and dense matricesNumpy / Scipy - Sparse matrix to vectorPopulate a Pandas SparseDataFrame from a SciPy Sparse Coo MatrixMultiply each row of a scipy sparse matrix by an element of an array, without using multiplySparse matrix matrix power in python
I want to split a sparse matrix (type: scipy.sparse.csr.csr_matrix) into N parts in the right order and iterative over them to use them as inputs for classification prediction.
However, if I try to convert the sparse matrix to a dense matrix with csr_matrix.toarray(), I get a MemoryError. The size of the converted array would take up 70gb of RAM, tested with the method in this thread (Error Converting Sparse Matrix to Array with scipy.sparse.csc_matrix.toarray())
So I can't use numpy.array_split() to split the array, because it only works on dense matrices.
Is there another way to split/slice the sparse matrix into N sparse matrices?
Thanks.
Additional edit:
So the chunking goes like this with a sparse array X_test:
# X_test is a sparse matrix with feature vectors
chunk_results = []
X_dense = csr_matrix.toarray(X_test)
X_test_chunks = np.array_split(X_dense, 20)
for chunk in X_test_chunks:
chunk_results.append(classifier.predict(chunk))
prediction = np.concatenate(chunk_results)
Here an example of a conversion for a small sparse matrix to dense:
# sparse
(0, 0) -0.5
(0, 1) 3.8570557155110414
(0, 2) -1.975755301731886
(1, 0) -3.5
(1, 1) 6.54336961554629
(1, 2) -3.311314222363026
# dense
[[-0.5 3.85705572 -1.9757553 ]
[-3.5 6.54336962 -3.31131422]]
Each of those two inner arrays in the dense matrix is a Feature-Vector representing one object. Basically, assuming we would try to classify those by doing the chunk split technique with conversion on this example, we would take n=2 and get [-0.5 3.85705572 -1.9757553] and [-3.5 6.54336962 -3.31131422] as two chunks. In case we would have a bigger matrix with more entries, there would be several of these vector-arrays per chunk.
SOLUTION
I made a workaround by dividing the number of rows of the sparse matrix with the desired number of chunks, and slice the matrix in chunks of rows of that number as hpaulj suggested:
Blockquote
np.array_split does sliced indexing of the dense array, [arr[i:j] for i,j in ....]. So you could do the same sort of indexing on a csr matrix. Sparse slicing isn't as fast as the dense version, but it works (for the right sparse format).
python numpy matrix scipy sparse-matrix
|
show 4 more comments
I want to split a sparse matrix (type: scipy.sparse.csr.csr_matrix) into N parts in the right order and iterative over them to use them as inputs for classification prediction.
However, if I try to convert the sparse matrix to a dense matrix with csr_matrix.toarray(), I get a MemoryError. The size of the converted array would take up 70gb of RAM, tested with the method in this thread (Error Converting Sparse Matrix to Array with scipy.sparse.csc_matrix.toarray())
So I can't use numpy.array_split() to split the array, because it only works on dense matrices.
Is there another way to split/slice the sparse matrix into N sparse matrices?
Thanks.
Additional edit:
So the chunking goes like this with a sparse array X_test:
# X_test is a sparse matrix with feature vectors
chunk_results = []
X_dense = csr_matrix.toarray(X_test)
X_test_chunks = np.array_split(X_dense, 20)
for chunk in X_test_chunks:
chunk_results.append(classifier.predict(chunk))
prediction = np.concatenate(chunk_results)
Here an example of a conversion for a small sparse matrix to dense:
# sparse
(0, 0) -0.5
(0, 1) 3.8570557155110414
(0, 2) -1.975755301731886
(1, 0) -3.5
(1, 1) 6.54336961554629
(1, 2) -3.311314222363026
# dense
[[-0.5 3.85705572 -1.9757553 ]
[-3.5 6.54336962 -3.31131422]]
Each of those two inner arrays in the dense matrix is a Feature-Vector representing one object. Basically, assuming we would try to classify those by doing the chunk split technique with conversion on this example, we would take n=2 and get [-0.5 3.85705572 -1.9757553] and [-3.5 6.54336962 -3.31131422] as two chunks. In case we would have a bigger matrix with more entries, there would be several of these vector-arrays per chunk.
SOLUTION
I made a workaround by dividing the number of rows of the sparse matrix with the desired number of chunks, and slice the matrix in chunks of rows of that number as hpaulj suggested:
Blockquote
np.array_split does sliced indexing of the dense array, [arr[i:j] for i,j in ....]. So you could do the same sort of indexing on a csr matrix. Sparse slicing isn't as fast as the dense version, but it works (for the right sparse format).
python numpy matrix scipy sparse-matrix
What's the shape of your matrix, and desire shape and order of the chunks?
– hpaulj
Mar 7 at 18:20
np.array_splitdoes sliced indexing of the dense array,[arr[i:j] for i,j in ....]. So you could do the same sort of indexing on acsrmatrix. Sparse slicing isn't as fast as the dense version, but it works (for the right sparse format).
– hpaulj
Mar 7 at 18:23
Shape is something similar to (50000, 250000). The idea is to split the matrix into around 20 chunks andmodel.predict(chunk)for each of them, adding the resulting arrays of predicted targets to a temporary list and finally concatenate them into ay_predictedlist after all predictions. The order of chunks must be kept the same as in the original sparse matrix for the predicted targets to be in the same order as the true ones when building theclassification_report(y_predicted, y_true).
– danldsk
Mar 7 at 19:22
Chunks - by row, column or a combination?
– hpaulj
Mar 7 at 20:01
I believe a combination(?). I don't exactly understand how the sparse matrix is constructed by a FeatureUnion. However it contains the feature-vectors of the input, so at least their integrity has to be kept. I̶f̶ ̶c̶o̶n̶v̶e̶r̶t̶i̶n̶g̶ ̶t̶h̶e̶ ̶s̶p̶a̶r̶s̶e̶ ̶m̶a̶t̶r̶i̶x̶ ̶t̶o̶ ̶a̶ ̶d̶e̶n̶s̶e̶ ̶o̶n̶e̶,̶ ̶i̶t̶ ̶y̶i̶e̶l̶d̶s̶ ̶a̶ ̶2̶D̶ ̶a̶r̶r̶a̶y̶ ̶w̶i̶t̶h̶ ̶̶[̶ ̶[̶x̶1̶]̶ ̶[̶x̶2̶]̶.̶.̶ ̶.̶[̶x̶n̶]̶ ̶]̶.̶̶ ̶a̶n̶d̶ ̶x̶i̶ ̶b̶e̶i̶n̶g̶ ̶a̶ ̶f̶e̶a̶t̶u̶r̶e̶-̶v̶e̶c̶t̶o̶r̶ ̶w̶i̶t̶h̶ ̶m̶u̶l̶t̶i̶p̶l̶e̶ ̶v̶a̶l̶u̶e̶s̶ ̶f̶o̶r̶ ̶o̶n̶e̶ ̶i̶n̶p̶u̶t̶-̶o̶b̶j̶e̶c̶t̶.̶
– danldsk
Mar 7 at 20:23
|
show 4 more comments
I want to split a sparse matrix (type: scipy.sparse.csr.csr_matrix) into N parts in the right order and iterative over them to use them as inputs for classification prediction.
However, if I try to convert the sparse matrix to a dense matrix with csr_matrix.toarray(), I get a MemoryError. The size of the converted array would take up 70gb of RAM, tested with the method in this thread (Error Converting Sparse Matrix to Array with scipy.sparse.csc_matrix.toarray())
So I can't use numpy.array_split() to split the array, because it only works on dense matrices.
Is there another way to split/slice the sparse matrix into N sparse matrices?
Thanks.
Additional edit:
So the chunking goes like this with a sparse array X_test:
# X_test is a sparse matrix with feature vectors
chunk_results = []
X_dense = csr_matrix.toarray(X_test)
X_test_chunks = np.array_split(X_dense, 20)
for chunk in X_test_chunks:
chunk_results.append(classifier.predict(chunk))
prediction = np.concatenate(chunk_results)
Here an example of a conversion for a small sparse matrix to dense:
# sparse
(0, 0) -0.5
(0, 1) 3.8570557155110414
(0, 2) -1.975755301731886
(1, 0) -3.5
(1, 1) 6.54336961554629
(1, 2) -3.311314222363026
# dense
[[-0.5 3.85705572 -1.9757553 ]
[-3.5 6.54336962 -3.31131422]]
Each of those two inner arrays in the dense matrix is a Feature-Vector representing one object. Basically, assuming we would try to classify those by doing the chunk split technique with conversion on this example, we would take n=2 and get [-0.5 3.85705572 -1.9757553] and [-3.5 6.54336962 -3.31131422] as two chunks. In case we would have a bigger matrix with more entries, there would be several of these vector-arrays per chunk.
SOLUTION
I made a workaround by dividing the number of rows of the sparse matrix with the desired number of chunks, and slice the matrix in chunks of rows of that number as hpaulj suggested:
Blockquote
np.array_split does sliced indexing of the dense array, [arr[i:j] for i,j in ....]. So you could do the same sort of indexing on a csr matrix. Sparse slicing isn't as fast as the dense version, but it works (for the right sparse format).
python numpy matrix scipy sparse-matrix
I want to split a sparse matrix (type: scipy.sparse.csr.csr_matrix) into N parts in the right order and iterative over them to use them as inputs for classification prediction.
However, if I try to convert the sparse matrix to a dense matrix with csr_matrix.toarray(), I get a MemoryError. The size of the converted array would take up 70gb of RAM, tested with the method in this thread (Error Converting Sparse Matrix to Array with scipy.sparse.csc_matrix.toarray())
So I can't use numpy.array_split() to split the array, because it only works on dense matrices.
Is there another way to split/slice the sparse matrix into N sparse matrices?
Thanks.
Additional edit:
So the chunking goes like this with a sparse array X_test:
# X_test is a sparse matrix with feature vectors
chunk_results = []
X_dense = csr_matrix.toarray(X_test)
X_test_chunks = np.array_split(X_dense, 20)
for chunk in X_test_chunks:
chunk_results.append(classifier.predict(chunk))
prediction = np.concatenate(chunk_results)
Here an example of a conversion for a small sparse matrix to dense:
# sparse
(0, 0) -0.5
(0, 1) 3.8570557155110414
(0, 2) -1.975755301731886
(1, 0) -3.5
(1, 1) 6.54336961554629
(1, 2) -3.311314222363026
# dense
[[-0.5 3.85705572 -1.9757553 ]
[-3.5 6.54336962 -3.31131422]]
Each of those two inner arrays in the dense matrix is a Feature-Vector representing one object. Basically, assuming we would try to classify those by doing the chunk split technique with conversion on this example, we would take n=2 and get [-0.5 3.85705572 -1.9757553] and [-3.5 6.54336962 -3.31131422] as two chunks. In case we would have a bigger matrix with more entries, there would be several of these vector-arrays per chunk.
SOLUTION
I made a workaround by dividing the number of rows of the sparse matrix with the desired number of chunks, and slice the matrix in chunks of rows of that number as hpaulj suggested:
Blockquote
np.array_split does sliced indexing of the dense array, [arr[i:j] for i,j in ....]. So you could do the same sort of indexing on a csr matrix. Sparse slicing isn't as fast as the dense version, but it works (for the right sparse format).
python numpy matrix scipy sparse-matrix
python numpy matrix scipy sparse-matrix
edited Mar 10 at 19:05
danldsk
asked Mar 7 at 17:04
danldskdanldsk
62
62
What's the shape of your matrix, and desire shape and order of the chunks?
– hpaulj
Mar 7 at 18:20
np.array_splitdoes sliced indexing of the dense array,[arr[i:j] for i,j in ....]. So you could do the same sort of indexing on acsrmatrix. Sparse slicing isn't as fast as the dense version, but it works (for the right sparse format).
– hpaulj
Mar 7 at 18:23
Shape is something similar to (50000, 250000). The idea is to split the matrix into around 20 chunks andmodel.predict(chunk)for each of them, adding the resulting arrays of predicted targets to a temporary list and finally concatenate them into ay_predictedlist after all predictions. The order of chunks must be kept the same as in the original sparse matrix for the predicted targets to be in the same order as the true ones when building theclassification_report(y_predicted, y_true).
– danldsk
Mar 7 at 19:22
Chunks - by row, column or a combination?
– hpaulj
Mar 7 at 20:01
I believe a combination(?). I don't exactly understand how the sparse matrix is constructed by a FeatureUnion. However it contains the feature-vectors of the input, so at least their integrity has to be kept. I̶f̶ ̶c̶o̶n̶v̶e̶r̶t̶i̶n̶g̶ ̶t̶h̶e̶ ̶s̶p̶a̶r̶s̶e̶ ̶m̶a̶t̶r̶i̶x̶ ̶t̶o̶ ̶a̶ ̶d̶e̶n̶s̶e̶ ̶o̶n̶e̶,̶ ̶i̶t̶ ̶y̶i̶e̶l̶d̶s̶ ̶a̶ ̶2̶D̶ ̶a̶r̶r̶a̶y̶ ̶w̶i̶t̶h̶ ̶̶[̶ ̶[̶x̶1̶]̶ ̶[̶x̶2̶]̶.̶.̶ ̶.̶[̶x̶n̶]̶ ̶]̶.̶̶ ̶a̶n̶d̶ ̶x̶i̶ ̶b̶e̶i̶n̶g̶ ̶a̶ ̶f̶e̶a̶t̶u̶r̶e̶-̶v̶e̶c̶t̶o̶r̶ ̶w̶i̶t̶h̶ ̶m̶u̶l̶t̶i̶p̶l̶e̶ ̶v̶a̶l̶u̶e̶s̶ ̶f̶o̶r̶ ̶o̶n̶e̶ ̶i̶n̶p̶u̶t̶-̶o̶b̶j̶e̶c̶t̶.̶
– danldsk
Mar 7 at 20:23
|
show 4 more comments
What's the shape of your matrix, and desire shape and order of the chunks?
– hpaulj
Mar 7 at 18:20
np.array_splitdoes sliced indexing of the dense array,[arr[i:j] for i,j in ....]. So you could do the same sort of indexing on acsrmatrix. Sparse slicing isn't as fast as the dense version, but it works (for the right sparse format).
– hpaulj
Mar 7 at 18:23
Shape is something similar to (50000, 250000). The idea is to split the matrix into around 20 chunks andmodel.predict(chunk)for each of them, adding the resulting arrays of predicted targets to a temporary list and finally concatenate them into ay_predictedlist after all predictions. The order of chunks must be kept the same as in the original sparse matrix for the predicted targets to be in the same order as the true ones when building theclassification_report(y_predicted, y_true).
– danldsk
Mar 7 at 19:22
Chunks - by row, column or a combination?
– hpaulj
Mar 7 at 20:01
I believe a combination(?). I don't exactly understand how the sparse matrix is constructed by a FeatureUnion. However it contains the feature-vectors of the input, so at least their integrity has to be kept. I̶f̶ ̶c̶o̶n̶v̶e̶r̶t̶i̶n̶g̶ ̶t̶h̶e̶ ̶s̶p̶a̶r̶s̶e̶ ̶m̶a̶t̶r̶i̶x̶ ̶t̶o̶ ̶a̶ ̶d̶e̶n̶s̶e̶ ̶o̶n̶e̶,̶ ̶i̶t̶ ̶y̶i̶e̶l̶d̶s̶ ̶a̶ ̶2̶D̶ ̶a̶r̶r̶a̶y̶ ̶w̶i̶t̶h̶ ̶̶[̶ ̶[̶x̶1̶]̶ ̶[̶x̶2̶]̶.̶.̶ ̶.̶[̶x̶n̶]̶ ̶]̶.̶̶ ̶a̶n̶d̶ ̶x̶i̶ ̶b̶e̶i̶n̶g̶ ̶a̶ ̶f̶e̶a̶t̶u̶r̶e̶-̶v̶e̶c̶t̶o̶r̶ ̶w̶i̶t̶h̶ ̶m̶u̶l̶t̶i̶p̶l̶e̶ ̶v̶a̶l̶u̶e̶s̶ ̶f̶o̶r̶ ̶o̶n̶e̶ ̶i̶n̶p̶u̶t̶-̶o̶b̶j̶e̶c̶t̶.̶
– danldsk
Mar 7 at 20:23
What's the shape of your matrix, and desire shape and order of the chunks?
– hpaulj
Mar 7 at 18:20
What's the shape of your matrix, and desire shape and order of the chunks?
– hpaulj
Mar 7 at 18:20
np.array_split does sliced indexing of the dense array, [arr[i:j] for i,j in ....]. So you could do the same sort of indexing on a csr matrix. Sparse slicing isn't as fast as the dense version, but it works (for the right sparse format).– hpaulj
Mar 7 at 18:23
np.array_split does sliced indexing of the dense array, [arr[i:j] for i,j in ....]. So you could do the same sort of indexing on a csr matrix. Sparse slicing isn't as fast as the dense version, but it works (for the right sparse format).– hpaulj
Mar 7 at 18:23
Shape is something similar to (50000, 250000). The idea is to split the matrix into around 20 chunks and
model.predict(chunk) for each of them, adding the resulting arrays of predicted targets to a temporary list and finally concatenate them into a y_predicted list after all predictions. The order of chunks must be kept the same as in the original sparse matrix for the predicted targets to be in the same order as the true ones when building the classification_report(y_predicted, y_true).– danldsk
Mar 7 at 19:22
Shape is something similar to (50000, 250000). The idea is to split the matrix into around 20 chunks and
model.predict(chunk) for each of them, adding the resulting arrays of predicted targets to a temporary list and finally concatenate them into a y_predicted list after all predictions. The order of chunks must be kept the same as in the original sparse matrix for the predicted targets to be in the same order as the true ones when building the classification_report(y_predicted, y_true).– danldsk
Mar 7 at 19:22
Chunks - by row, column or a combination?
– hpaulj
Mar 7 at 20:01
Chunks - by row, column or a combination?
– hpaulj
Mar 7 at 20:01
I believe a combination(?). I don't exactly understand how the sparse matrix is constructed by a FeatureUnion. However it contains the feature-vectors of the input, so at least their integrity has to be kept. I̶f̶ ̶c̶o̶n̶v̶e̶r̶t̶i̶n̶g̶ ̶t̶h̶e̶ ̶s̶p̶a̶r̶s̶e̶ ̶m̶a̶t̶r̶i̶x̶ ̶t̶o̶ ̶a̶ ̶d̶e̶n̶s̶e̶ ̶o̶n̶e̶,̶ ̶i̶t̶ ̶y̶i̶e̶l̶d̶s̶ ̶a̶ ̶2̶D̶ ̶a̶r̶r̶a̶y̶ ̶w̶i̶t̶h̶ ̶
̶[̶ ̶[̶x̶1̶]̶ ̶[̶x̶2̶]̶.̶.̶ ̶.̶[̶x̶n̶]̶ ̶]̶.̶̶ ̶a̶n̶d̶ ̶x̶i̶ ̶b̶e̶i̶n̶g̶ ̶a̶ ̶f̶e̶a̶t̶u̶r̶e̶-̶v̶e̶c̶t̶o̶r̶ ̶w̶i̶t̶h̶ ̶m̶u̶l̶t̶i̶p̶l̶e̶ ̶v̶a̶l̶u̶e̶s̶ ̶f̶o̶r̶ ̶o̶n̶e̶ ̶i̶n̶p̶u̶t̶-̶o̶b̶j̶e̶c̶t̶.̶– danldsk
Mar 7 at 20:23
I believe a combination(?). I don't exactly understand how the sparse matrix is constructed by a FeatureUnion. However it contains the feature-vectors of the input, so at least their integrity has to be kept. I̶f̶ ̶c̶o̶n̶v̶e̶r̶t̶i̶n̶g̶ ̶t̶h̶e̶ ̶s̶p̶a̶r̶s̶e̶ ̶m̶a̶t̶r̶i̶x̶ ̶t̶o̶ ̶a̶ ̶d̶e̶n̶s̶e̶ ̶o̶n̶e̶,̶ ̶i̶t̶ ̶y̶i̶e̶l̶d̶s̶ ̶a̶ ̶2̶D̶ ̶a̶r̶r̶a̶y̶ ̶w̶i̶t̶h̶ ̶
̶[̶ ̶[̶x̶1̶]̶ ̶[̶x̶2̶]̶.̶.̶ ̶.̶[̶x̶n̶]̶ ̶]̶.̶̶ ̶a̶n̶d̶ ̶x̶i̶ ̶b̶e̶i̶n̶g̶ ̶a̶ ̶f̶e̶a̶t̶u̶r̶e̶-̶v̶e̶c̶t̶o̶r̶ ̶w̶i̶t̶h̶ ̶m̶u̶l̶t̶i̶p̶l̶e̶ ̶v̶a̶l̶u̶e̶s̶ ̶f̶o̶r̶ ̶o̶n̶e̶ ̶i̶n̶p̶u̶t̶-̶o̶b̶j̶e̶c̶t̶.̶– danldsk
Mar 7 at 20:23
|
show 4 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049275%2fsplit-a-sparse-matrix-into-chunks-without-converting-to-dense%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049275%2fsplit-a-sparse-matrix-into-chunks-without-converting-to-dense%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's the shape of your matrix, and desire shape and order of the chunks?
– hpaulj
Mar 7 at 18:20
np.array_splitdoes sliced indexing of the dense array,[arr[i:j] for i,j in ....]. So you could do the same sort of indexing on acsrmatrix. Sparse slicing isn't as fast as the dense version, but it works (for the right sparse format).– hpaulj
Mar 7 at 18:23
Shape is something similar to (50000, 250000). The idea is to split the matrix into around 20 chunks and
model.predict(chunk)for each of them, adding the resulting arrays of predicted targets to a temporary list and finally concatenate them into ay_predictedlist after all predictions. The order of chunks must be kept the same as in the original sparse matrix for the predicted targets to be in the same order as the true ones when building theclassification_report(y_predicted, y_true).– danldsk
Mar 7 at 19:22
Chunks - by row, column or a combination?
– hpaulj
Mar 7 at 20:01
I believe a combination(?). I don't exactly understand how the sparse matrix is constructed by a FeatureUnion. However it contains the feature-vectors of the input, so at least their integrity has to be kept. I̶f̶ ̶c̶o̶n̶v̶e̶r̶t̶i̶n̶g̶ ̶t̶h̶e̶ ̶s̶p̶a̶r̶s̶e̶ ̶m̶a̶t̶r̶i̶x̶ ̶t̶o̶ ̶a̶ ̶d̶e̶n̶s̶e̶ ̶o̶n̶e̶,̶ ̶i̶t̶ ̶y̶i̶e̶l̶d̶s̶ ̶a̶ ̶2̶D̶ ̶a̶r̶r̶a̶y̶ ̶w̶i̶t̶h̶ ̶
̶[̶ ̶[̶x̶1̶]̶ ̶[̶x̶2̶]̶.̶.̶ ̶.̶[̶x̶n̶]̶ ̶]̶.̶̶ ̶a̶n̶d̶ ̶x̶i̶ ̶b̶e̶i̶n̶g̶ ̶a̶ ̶f̶e̶a̶t̶u̶r̶e̶-̶v̶e̶c̶t̶o̶r̶ ̶w̶i̶t̶h̶ ̶m̶u̶l̶t̶i̶p̶l̶e̶ ̶v̶a̶l̶u̶e̶s̶ ̶f̶o̶r̶ ̶o̶n̶e̶ ̶i̶n̶p̶u̶t̶-̶o̶b̶j̶e̶c̶t̶.̶– danldsk
Mar 7 at 20:23