liveBook · Manning

Get_score

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

get_score is a book-specific Python function that computes an authorship-comparison score from weighted differences between signatures.

get_score compares two authorship signatures and returns a single score. Lower scores indicate greater similarity, supporting identification of the closest known signature. section 11.3.1

The function works by:

  • Taking two signatures and a list of feature weights. section 11.3.1
  • Finding the absolute difference between corresponding feature values. section 11.5.12
  • Multiplying each difference by its weight and adding the contributions into one score. section 11.5.12

Comparing two signatures

For signatures [4.6, 0.1, 0.05, 10, 2] and [4.3, 0.1, 0.04, 16, 4], with weights [11, 33, 50, 0.4, 4], get_score returns 14.2. Matching feature values contribute zero; larger weighted differences contribute more. section 11.3.1 section 11.5.12

Listing 11.15 Compare two signatures
def get_score(signature1, signature2, weights):
    '''
    signature1 and signature2 are signatures.
    weights is a list of five weights.
        
    Return the score for signature1 and signature2.
        
    >>> get_score([4.6, 0.1, 0.05, 10, 2],\
                  [4.3, 0.1, 0.04, 16, 4],\
                  [11, 33, 50, 0.4, 4])       #1
    14.2
    '''
    score = 0
    for i in range(len(signature1)):                #2
        score += abs(signature1[i] - signature2[i]) \   #3
                 * weights[i]                           #3
    return score

Inspect the complete Python implementation, including its parameters, loop, weighted absolute differences, return value, and doctest. section 11.5.12

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

How deep it goes
Implements the weighted absolute-difference loop and demonstrates it with a doctest, without developing the metric mathematically.
Examples
Uses five-feature signatures, explicit weights, a tabulated 14.2 calculation, and a Python doctest.
What it assumes
Assumes lists, loops, functions, signatures, and weights while defining the comparison process through a practical example.
What you can do after
Build it from scratch in Python and test it with a concrete signature comparison.
How it connects
Connects signatures, feature weights, authorship identification, and lowest-score selection.

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.