scipy.sparse.

hstack#

scipy.sparse.hstack(blocks,format=None,dtype=None)[source]#

Stack sparse matrices horizontally (column wise)

Parameters:
blocks

sequence of sparse matrices with compatible shapes

formatstr

sparse format of the result (e.g., “csr”)by default an appropriate sparse matrix format is returned.This choice is subject to change.

dtypedtype, optional

The data-type of the output matrix. If not given, the dtype isdetermined from that ofblocks.

Returns:
new_arraysparse matrix or array

If any block in blocks is a sparse array, return a sparse array.Otherwise return a sparse matrix.

If you want a sparse array built from blocks that are not sparsearrays, useblock(hstack(blocks)) or convert one blocke.g.blocks[0]=csr_array(blocks[0]).

See also

vstack

stack sparse matrices vertically (row wise)

Examples

>>>fromscipy.sparseimportcoo_matrix,hstack>>>A=coo_matrix([[1,2],[3,4]])>>>B=coo_matrix([[5],[6]])>>>hstack([A,B]).toarray()array([[1, 2, 5],       [3, 4, 6]])
On this page