src/rime/util/__init__.py
def create_matrix(event_df, user_index, item_index, return_type='csr'):
    """ create matrix and prune unknown indices """
    user2ind = {k: i for i, k in enumerate(user_index)}
    item2ind = {k: i for i, k in enumerate(item_index)}
    event_df = event_df[
        event_df['USER_ID'].isin(set(user_index)) &
        event_df['ITEM_ID'].isin(set(item_index))
    ]
    i = [user2ind[k] for k in event_df['USER_ID']]
    j = [item2ind[k] for k in event_df['ITEM_ID']]

    if return_type == 'ij':
        return (i, j)

    data = np.ones(len(event_df))
    shape = (len(user_index), len(item_index))
    csr = sp.sparse.coo_matrix((data, (i, j)), shape=shape).tocsr()

    if return_type == 'csr':
        return csr
    elif return_type == 'df':
        return pd.DataFrame.sparse.from_spmatrix(csr, user_index, item_index)
 create matrix and prune unknown indices 
create matrix and prune unknown indices
create matrix and prune unknown indices
def create_matrix(event_df, user_index, item_index, return_type='csr'):
    user2ind = {k: i for i, k in enumerate(user_index)}
    item2ind = {k: i for i, k in enumerate(item_index)}
    event_df = event_df[
        event_df['USER_ID'].isin(set(user_index)) &
        event_df['ITEM_ID'].isin(set(item_index))
    ]
    i = [user2ind[k] for k in event_df['USER_ID']]
    j = [item2ind[k] for k in event_df['ITEM_ID']]
    if return_type == 'ij':
        return (i, j)
    data = np.ones(len(event_df))
    shape = (len(user_index), len(item_index))
    csr = sp.sparse.coo_matrix((data, (i, j)), shape=shape).tocsr()
    if return_type == 'csr':
        return csr
    elif return_type == 'df':
        return pd.DataFrame.sparse.from_spmatrix(csr, user_index, item_index)
def create_matrix ( event_df , user_index , item_index , return_type = 'csr' ) : user2ind = { k : i for i , k in enumerate ( user_index ) } item2ind = { k : i for i , k in enumerate ( item_index ) } event_df = event_df [ event_df [ 'USER_ID' ] . isin ( set ( user_index ) ) & event_df [ 'ITEM_ID' ] . isin ( set ( item_index ) ) ] i = [ user2ind [ k ] for k in event_df [ 'USER_ID' ] ] j = [ item2ind [ k ] for k in event_df [ 'ITEM_ID' ] ] if return_type == 'ij' : return ( i , j ) data = np . ones ( len ( event_df ) ) shape = ( len ( user_index ) , len ( item_index ) ) csr = sp . sparse . coo_matrix ( ( data , ( i , j ) ) , shape = shape ) . tocsr ( ) if return_type == 'csr' : return csr elif return_type == 'df' : return pd . DataFrame . sparse . from_spmatrix ( csr , user_index , item_index )
create matrix and prune unknown indices
create matrix and prune unknown indices
""" create matrix and prune unknown indices """
event_df
user_index
item_index
return_type