Question: Python Source File: http://resources.codingthematrix.com/ in Lab: Comparing voting records using dot-product Lab: Comparing voting records using dot-product Task 2.12.1: Write a procedure create voting dict(strlist)

Python

Source File: http://resources.codingthematrix.com/ in Lab: Comparing voting records using dot-product

Lab: Comparing voting records using dot-product

Task 2.12.1: Write a procedure create voting dict(strlist) that, given a list of strings (voting records from the source file), returns a dictionary that maps the last name of a senator to a list of numbers representing that senators voting record. You will need to use the built-in procedure int() to convert a string representation of an integer (e.g. 1) to the actual integer (e.g. 1)

2.12.4 Policy comparison We would like to determine just how like-minded two given senators are. We will use the dot-product of vectors u and v to judge how often two senators are in agreement.

Task 2.12.2: Write a procedure policy compare(sen a, sen b, voting dict) that, given two names of senators and a dictionary mapping senator names to lists representing voting records, returns the dot-product representing the degree of similarity between two senators voting policies.

Task 2.12.3: Write a procedure most similar(sen, voting dict) that, given the name of a senator and a dictionary mapping senator names to lists representing voting records, returns the name of the senator whose political mindset is most like the input senator (excluding, of course, the input senator him/herself).

Task 2.12.4: Write a very similar procedure least similar(sen, voting dict) that returns the name of the senator whose voting record agrees the least with the senator whose name is sen.

Task 2.12.5: Use these procedures to figure out which senator is most like Rhode Island legend Lincoln Chafee. Then use these procedures to see who disagrees most with Pennsylvanias Rick Santorum. Give their names.

Task 2.12.6: How similar are the voting records of the two senators from your favorite state?

Task 2.12.7: Write a procedure find average similarity(sen, sen set, voting dict) that, given the name sen of a senator, compares that senators voting record to the voting records of all senators whose names are in sen set, computing a dot-product for each, and then returns the average dot-product. Use your procedure to compute which senator has the greatest average similarity with the set of Democrats (you can extract this set from the input file). In the last task, you had to compare each senators record to the voting record of each Democrat senator. If you were doing the same computation with, say, the movie preferences of all Netflix subscribers, it would take far too long to be practical. Next we see that there is a computational shortcut, based on an algebraic property of the dot-product: the distributive property:

(v1 + v2) x = v1 x + v2 x

Task 2.12.8: Write a procedure find average record(sen set, voting dict) that, given a set of names of senators, finds the average voting record. That is, perform vector addition on the lists representing their voting records, and then divide the sum by the number of vectors. The result should be a vector. Use this procedure to compute the average voting record for the set of Democrats, and assign the result to the variable average Democrat record. Next find which senators voting record is most similar to the average Democrat voting record. Did you get the same result as in Task 2.12.7? Can you explain? 2.12.6 Bitter Rivals

Task 2.12.9: Write a procedure bitter rivals(voting dict) to find which two senators disagree the most. This task again requires comparing each pair of voting records. Can this be done faster than the obvious way? There is a slightly more efficient algorithm, using fast matrix multiplication. We will study matrix multiplication later, although we wont cover the theoretically fast algorithms.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!