<s> from __future__ import division <EOL> import numpy as np <EOL> from sklearn . utils . linear_assignment_ import linear_assignment <EOL> from sklearn . utils . validation import check_consistent_length , check_array <EOL> __all__ = [ "<STR_LIT>" ] <EOL> def _check_rows_and_columns ( a , b ) : <EOL> """<STR_LIT>""" <EOL> check_consistent_length ( * a ) <EOL> check_consistent_length ( * b ) <EOL> checks = lambda x : check_array ( x , ensure_2d = False ) <EOL> a_rows , a_cols = map ( checks , a ) <EOL> b_rows , b_cols = map ( checks , b ) <EOL> return a_rows , a_cols , b_rows , b_cols <EOL> def _jaccard ( a_rows , a_cols , b_rows , b_cols ) : <EOL> """<STR_LIT>""" <EOL> intersection = ( ( a_rows * b_rows ) . sum ( ) * <EOL> ( a_cols * b_cols ) . sum ( ) ) <EOL> a_size = a_rows . sum ( ) * a_cols . sum ( ) <EOL> b_size = b_rows . sum ( ) * b_cols . sum ( ) <EOL> return intersection / ( a_size + b_size - intersection ) <EOL> def _pairwise_similarity ( a , b , similarity ) : <EOL> """<STR_LIT>""" <EOL> a_rows , a_cols , b_rows , b_cols = _check_rows_and_columns ( a , b ) <EOL> n_a = a_rows . shape [ <NUM_LIT:0> ] <EOL> n_b = b_rows . shape [ <NUM_LIT:0> ] <EOL> result = np . array ( list ( list ( similarity ( a_rows [ i ] , a_cols [ i ] , <EOL> b_rows [ j ] , b_cols [ j ] ) <EOL> for j in range ( n_b ) ) <EOL> for i in range ( n_a ) ) ) <EOL> return result <EOL> def consensus_score ( a , b , similarity = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if similarity == "<STR_LIT>" : <EOL> similarity = _jaccard <EOL> matrix = _pairwise_similarity ( a , b , similarity ) <EOL> indices = linear_assignment ( <NUM_LIT:1.> - matrix ) <EOL> n_a = len ( a [ <NUM_LIT:0> ] ) <EOL> n_b = len ( b [ <NUM_LIT:0> ] ) <EOL> return matrix [ indices [ : , <NUM_LIT:0> ] , indices [ : , <NUM_LIT:1> ] ] . sum ( ) / max ( n_a , n_b ) </s>
