liveBook · Manning

Lowest_score

As explained in Learn AI-Assisted Python Programming, Second Edition

lowest_score is a book-specific Python function that compares an unknown signature with each known signature and returns the known signature having the smallest score.

lowest_score chooses the known signature closest to an unknown signature. It receives a dictionary of known signatures, the unknown signature, and five weights; it compares each candidate and returns the key with the smallest score. section 11.3.1 section 11.5.13

The function follows a simple input–process–output pattern:

  • Input: author names mapped to known signatures, one unknown signature, and a list of weights. section 11.5.13
  • Process: loop through each known signature, call get_score, and keep the key whose score is lowest. section 11.3.1 section 11.5.13
  • Output: return the key associated with the closest known signature. section 11.5.13

Choosing Dan

Given {'Dan': [1, 1, 1, 1, 1]} and {'Leo': [3, 3, 3, 3, 3]}, the unknown signature [1, 0.8, 0.9, 1.3, 1.4], and weights [11, 33, 50, 0.4, 4], lowest_score returns 'Dan'. section 11.5.13

Listing 11.16 Closest known signature
def lowest_score(signatures_dict, unknown_signature, weights):
    '''
    signatures_dict is a dictionary mapping keys to signatures.
    unknown_signature is a signature.
    weights is a list of five weights.
    Return the key whose signature value has the lowest 
    score with unknown_signature.
    
    >>> d = {'Dan': [1, 1, 1, 1, 1],\      #1
             'Leo': [3, 3, 3, 3, 3]}       #1
    >>> unknown = [1, 0.8, 0.9, 1.3, 1.4]
    >>> weights = [11, 33, 50, 0.4, 4]
    >>> lowest_score(d, unknown, weights)     #2
    'Dan'
    '''
    lowest = None
    for key in signatures_dict:                         #3
        score = get_score(signatures_dict[key],           #4
                          unknown_signature, weights)    
        if lowest is None or score < lowest[1]:      #5
            lowest = (key, score)       #6
    return lowest[0]   #7

Inspect the complete Python listing, including the minimum-score tracking logic and its doctest example. section 11.5.13

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains inputs, iteration, scoring, minimum tracking, and return value, but leaves feature extraction to helper functions.
Examples
Uses Dan and Leo signatures, an unknown five-feature signature, and weighted comparison scores.
What it assumes
Assumes dictionaries, lists, loops, functions, and get_score; teaches the decomposition of closest-match selection.
What you can do after
Build the selection step by iterating through signatures and retaining the candidate with the smallest get_score result.
How it connects
Connects signatures with get_score, make_signature, get_all_signatures, and process_data.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.