def create_sparse_mapping(id_array, unique_ids=None):
    """
    Will create a scipy.sparse compressed-sparse-row matrix that maps
    each row represented by an element in id_array to the corresponding
    value of the unique ids in id_array.

    Parameters
    ----------
    id_array : 1D ndarray of ints.
        Each element should represent some id related to the corresponding row.
    unique_ids : 1D ndarray of ints, or None, optional.
        If not None, each element should be present in `id_array`. The elements
        in `unique_ids` should be present in the order in which one wishes them
        to appear in the columns of the resulting sparse array. For the
        `row_to_obs` and `row_to_mixers` mappings, this should be the order of
        appearance in `id_array`. If None, then the unique_ids will be created
        from `id_array`, in the order of their appearance in `id_array`.

    Returns
    -------
    mapping : 2D scipy.sparse CSR matrix.
        Will contain only zeros and ones. `mapping[i, j] == 1` where
        `id_array[i] == unique_ids[j]`. The id's corresponding to each column
        are given by `unique_ids`. The rows correspond to the elements of
        `id_array`.
    """
    # Create unique_ids if necessary
    if unique_ids is None:
        unique_ids = get_original_order_unique_ids(id_array)

    # Check function arguments for validity
    assert isinstance(unique_ids, np.ndarray)
    assert isinstance(id_array, np.ndarray)
    assert unique_ids.ndim == 1
    assert id_array.ndim == 1

    # Figure out which ids in id_array are represented in unique_ids
    represented_ids = np.in1d(id_array, unique_ids)
    # Determine the number of rows in id_array that are in unique_ids
    num_non_zero_rows = represented_ids.sum()
    # Figure out the dimensions of the resulting sparse matrix
    num_rows = id_array.size
    num_cols = unique_ids.size
    # Specify the non-zero values that will be present in the sparse matrix.
    data = np.ones(num_non_zero_rows, dtype=int)
    # Specify which rows will have non-zero entries in the sparse matrix.
    row_indices = np.arange(num_rows)[represented_ids]
    # Map the unique id's to their respective columns
    unique_id_dict = dict(zip(unique_ids, np.arange(num_cols)))
    # Figure out the column indices of the non-zero entries, and do so in a way
    # that avoids a key error (i.e. only look up ids that are represented)
    col_indices =\
        np.array([unique_id_dict[x] for x in id_array[represented_ids]])

    # Create and return the sparse matrix
    return csr_matrix((data, (row_indices, col_indices)),
                      shape=(num_rows, num_cols))
Will create a scipy.sparse compressed-sparse-row matrix that maps
    each row represented by an element in id_array to the corresponding
    value of the unique ids in id_array.

    Parameters
    ----------
    id_array : 1D ndarray of ints.
        Each element should represent some id related to the corresponding row.
    unique_ids : 1D ndarray of ints, or None, optional.
        If not None, each element should be present in `id_array`. The elements
        in `unique_ids` should be present in the order in which one wishes them
        to appear in the columns of the resulting sparse array. For the
        `row_to_obs` and `row_to_mixers` mappings, this should be the order of
        appearance in `id_array`. If None, then the unique_ids will be created
        from `id_array`, in the order of their appearance in `id_array`.

    Returns
    -------
    mapping : 2D scipy.sparse CSR matrix.
        Will contain only zeros and ones. `mapping[i, j] == 1` where
        `id_array[i] == unique_ids[j]`. The id's corresponding to each column
        are given by `unique_ids`. The rows correspond to the elements of
        `id_array`.