def ranking_rprecision_score(y_true, y_score, k=10):
    'Precision at rank k\n    Parameters\n    ----------\n    y_true : array-like, shape = [n_samples]\n        Ground truth (true relevance labels).\n    y_score : array-like, shape = [n_samples]\n        Predicted scores.\n    k : int\n        Rank.\n    Returns\n    -------\n    precision @k : float\n    '
    unique_y = np.unique(y_true)
    if (len(unique_y) == 1):
        return ValueError('The score cannot be approximated.')
    elif (len(unique_y) > 2):
        raise ValueError('Only supported for two relevance levels.')
    pos_label = unique_y[1]
    n_pos = np.sum((y_true == pos_label))
    order = np.argsort(y_score)[::(- 1)]
    y_true = np.take(y_true, order[:k])
    n_relevant = np.sum((y_true == pos_label))
    return (float(n_relevant) / min(k, n_pos))
Precision at rank k
Parameters
----------
y_true : array-like, shape = [n_samples]
    Ground truth (true relevance labels).
y_score : array-like, shape = [n_samples]
    Predicted scores.
k : int
    Rank.
Returns
-------
precision @k : float
def ranking_rprecision_score(y_true, y_score, k=10):
    'Precision at rank k\n    Parameters\n    ----------\n    y_true : array-like, shape = [n_samples]\n        Ground truth (true relevance labels).\n    y_score : array-like, shape = [n_samples]\n        Predicted scores.\n    k : int\n        Rank.\n    Returns\n    -------\n    precision @k : float\n    '
    unique_y = np.unique(y_true)
    if (len(unique_y) == 1):
        return ValueError('The score cannot be approximated.')
    elif (len(unique_y) > 2):
        raise ValueError('Only supported for two relevance levels.')
    pos_label = unique_y[1]
    n_pos = np.sum((y_true == pos_label))
    order = np.argsort(y_score)[::(- 1)]
    y_true = np.take(y_true, order[:k])
    n_relevant = np.sum((y_true == pos_label))
    return (float(n_relevant) / min(k, n_pos))