def matthews_corrcoef(y_true, y_pred, sample_weight=None):
 (y_type, y_true, y_pred) = _check_targets(y_true, y_pred)
 if (y_type != 'binary'):
  raise ValueError(('%s is not supported' % y_type))
 lb = LabelEncoder()
 lb.fit(np.hstack([y_true, y_pred]))
 y_true = lb.transform(y_true)
 y_pred = lb.transform(y_pred)
 mean_yt = np.average(y_true, weights=sample_weight)
 mean_yp = np.average(y_pred, weights=sample_weight)
 y_true_u_cent = (y_true - mean_yt)
 y_pred_u_cent = (y_pred - mean_yp)
 cov_ytyp = np.average((y_true_u_cent * y_pred_u_cent), weights=sample_weight)
 var_yt = np.average((y_true_u_cent ** 2), weights=sample_weight)
 var_yp = np.average((y_pred_u_cent ** 2), weights=sample_weight)
 mcc = (cov_ytyp / np.sqrt((var_yt * var_yp)))
 if np.isnan(mcc):
  return 0.0
 else:
  return mcc

def matthews_corrcoef ( y_true , y_pred , sample_weight = None ) : ( y_type , y_true , y_pred ) = _check_targets ( y_true , y_pred ) if ( y_type != 'binary' ) : raise ValueError ( ( '%s is not supported' % y_type ) ) lb = LabelEncoder ( ) lb . fit ( np . hstack ( [ y_true , y_pred ] ) ) y_true = lb . transform ( y_true ) y_pred = lb . transform ( y_pred ) mean_yt = np . average ( y_true , weights = sample_weight ) mean_yp = np . average ( y_pred , weights = sample_weight ) y_true_u_cent = ( y_true - mean_yt ) y_pred_u_cent = ( y_pred - mean_yp ) cov_ytyp = np . average ( ( y_true_u_cent * y_pred_u_cent ) , weights = sample_weight ) var_yt = np . average ( ( y_true_u_cent ** 2 ) , weights = sample_weight ) var_yp = np . average ( ( y_pred_u_cent ** 2 ) , weights = sample_weight ) mcc = ( cov_ytyp / np . sqrt ( ( var_yt * var_yp ) ) ) if np . isnan ( mcc ) : return 0.0 else : return mcc
compute the matthews correlation coefficient for binary classes the matthews correlation coefficient is used in machine learning as a measure of the quality of binary classifications .