Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

apk calculate python

def apk(actual, predicted, k=10):
    """
    Computes the average precision at k.

    This function computes the average precision at k between two lists of items.

    Parameters
    ----------
    actual: list
            A list of elements that are to be predicted (order doesn't matter)
    predicted : list
            A list of predicted elements (order does matter)
    k: int, optional

    Returns
    -------
    score : double
            The average precision at k over the input lists

    """
    if len(predicted) > k:
        predicted = predicted[:k]

    score = 0.0
    num_hits = 0.0

    for i,p in enumerate(predicted):
        if p in actual and p not in predicted[:i]:
            num_hits += 1.0
            score += num_hits / (i + 1.0)

    if not actual:
        return 1.0
    if min(len(actual), k) == 0:
        return 0.0
    else:
        return score / min(len(actual), k)
Comment

PREVIOUS NEXT
Code Example
Python :: Pandas column of lists, create a row for each list element 
Python :: pyqt grid layout 
Python :: python laplace expansion 
Python :: know functionality of any function using help 
Python :: # generators 
Python :: install requests-html modlule click on the link to learn more about requests-html 
Python :: KeyError: 0 
Python :: list cwd python 
Python :: python math.trunc 
Python :: splitting Feature and target using iloc 
Python :: python online compiler with libraries 
Python :: how-to-add-new-column-to-an-dataframe-to-the-front-not-end 
Python :: Set symmetric Using the Symmetric Difference Operator (^) Method 
Python :: Command to install Voluptuous Python Library 
Python :: negative list slicing 
Python :: numpy random sin 
Python :: Using **kwargs to pass the variable keyword arguments to the function 
Python :: pandas version for python 3.9 
Python :: python adding an item 
Python :: build numpy array 
Python :: Python NumPy squeeze function Syntax 
Python :: no definition 
Python :: Python3: Deleting even and only showing uneven numbers from, set list. 
Python :: Python NumPy row_stack Function Syntax 
Python :: fpdf latin-1 
Python :: Python __ge__ magic method 
Python :: funcs_and_args for loop python 
Python :: lambda function in python to shut ec2 at the time zone 
Python :: jenkins crumb python request 
Python :: call a Python range() using range(start, stop) 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =