code: def to_sparse(self, format='csr', **kwargs):
          
  
          import scipy.sparse
  
          # check arguments
          f = {
              'bsr': scipy.sparse.bsr_matrix,
              'coo': scipy.sparse.coo_matrix,
              'csc': scipy.sparse.csc_matrix,
              'csr': scipy.sparse.csr_matrix,
              'dia': scipy.sparse.dia_matrix,
              'dok': scipy.sparse.dok_matrix,
              'lil': scipy.sparse.lil_matrix
          }
          if format not in f:
              raise ValueError('invalid format: %r' % format)
  
          # create sparse matrix
          m = f[format](self, **kwargs)
  
          return m
docs: Convert into a sparse matrix.
  
          Parameters
          ----------
          format : {'coo', 'csc', 'csr', 'dia', 'dok', 'lil'}
              Sparse matrix format.
          kwargs : keyword arguments
              Passed through to sparse matrix constructor.
  
          Returns
          -------
          m : scipy.sparse.spmatrix
              Sparse matrix
  
          Examples
          --------
  
          >>> import allel
          >>> h = allel.HaplotypeArray([[0, 0, 0, 0],
          ...                           [0, 1, 0, 1],
          ...                           [1, 1, 0, 0],
          ...                           [0, 0, -1, -1]], dtype='i1')
          >>> m = h.to_sparse(format='csr')
          >>> m
          <4x4 sparse matrix of type '<class 'numpy.int8'>'
              with 6 stored elements in Compressed Sparse Row format>
          >>> m.data
          array([ 1,  1,  1,  1, -1, -1], dtype=int8)
          >>> m.indices
          array([1, 3, 0, 1, 2, 3], dtype=int32)
          >>> m.indptr
          array([0, 0, 2, 4, 6], dtype=int32)