Python user recommendation system full code of Manhattan Algorithm Implementation, python Manhattan

Source: Internet
Author: User

Python user recommendation system full code of Manhattan Algorithm Implementation, python Manhattan

Manhattan Distance, a taxi ry or Manhattan Distance, was created by Herman min kovski in the 19th century. It is a geometric term used in geometric measurements, used to indicate the total absolute wheelbase of two points in the standard coordinate system.

The red lines in the figure represent the distance between Manhattan, while green represents the Euclidean distance, that is, the straight line distance, while blue and yellow represent the equivalent distance between Manhattan. Manhattan distance-the distance between two points in the north and south plus the distance between the East and the West, that is, d (I, j) = | xi-xj | + | yi-yj |. For a town street with regular layout in the north and south directions, the distance from one point to the other is the distance between the north and the south plus the distance between the East and the West. Therefore, manhattan distance is also known as taxi distance. The distance between Manhattan is not a constant of distance. When the coordinate axes change, the distance between points is different. In early computer graphics, the screen consists of pixels and is an integer. the coordinate of a point is also generally an integer, because floating-point operations are expensive and very slow with errors, if we use the AB Euclidean distance (Euclidean distance: the Euclidean distance between two points in 2D and 3D space), we must perform floating point operations. If we use AC and CB, you only need to calculate addition and subtraction, which greatly increases the computing speed, and no error exists regardless of the number of accumulated operations.

Python user recommendation system Manhattan Algorithm Implementation

#-*-Coding: UTF-8-*-import codecsfrom math import sqrt users = {"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5, "Phoenix": 5.0, "Slightly Stoopid": 1.5, "The Strokes": 2.5, "Vampire Weekend": 2.0}, "Bill ": {"Blues Traveler": 2.0, "Broken Bells": 3.5, "Deadmau5": 4.0, "Phoenix": 2.0, "Slightly Stoopid": 3.5, "Vampire Weekend ": 3.0}, "Chan": {"Blues Traveler": 5.0, "Broken Bells": 1.0, "Deadmau5": 1.0, "Norah Jones": 3.0, "Phoenix ": 5, "Slightly Stoopid": 1.0}, "Dan": {"Blues Traveler": 3.0, "Broken Bells": 4.0, "Deadmau5": 4.5, "Phoenix ": 3.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 2.0}, "Hailey": {"Broken Bells": 4.0, "Deadmau5 ": 1.0, "Norah Jones": 4.0, "The Strokes": 4.0, "Vampire Weekend": 1.0}, "Jordyn": {"Broken Bells": 4.5, "Deadmau5 ": 4.0, "Norah Jones": 5.0, "Phoenix": 5.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 4.0 }, "Sam": {"Blues Traveler": 5.0, "Broken Bells": 2.0, "Norah Jones": 3.0, "Phoenix": 5.0, "Slightly Stoopid": 4.0, "The Strokes": 5.0}, "Veronica": {"Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0, "Slightly Stoopid": 2.5, "The Strokes": 3.0 }}# Python calculates The distance from Manhattan to www. iplaypy. comdef manhattan (rate1, rate2): distance = 0 commonRating = False for key in rate1: if key in rate2: distance + = abs (rate1 [key]-rate2 [key]) commonRating = True if commonRating: return distance else: return-1 # python returns the latest def computeNearestNeighbor (username, users): distances = [] for key in users: if key <> username: distance = manhattan (users [username], users [key]) distances. append (distance, key) distances. sort () return distances # recommended python implementation def recommend (username, users): # obtain the name nearest = computeNearestNeighbor (username, users) of the nearest user) [0] [1] recommendations = [] # obtain the neighborRatings = users [nearest] for key in neighborRatings: if not key in users [username]: recommendations. append (key, neighborRatings [key]) recommendations. sort (key = lambda rat: rat [1], reverse = True) return recommendations if _ name _ = '_ main __': print recommend ('haily', users)

Summary

The above is all about the complete code of the Manhattan algorithm recommended System for Python users in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.