Implementation of cosine similarity based on the user's recommendation algorithm-PHP source code

Source: Internet
Author: User
0

1. [File]Cosine. py
#-*-Coding: UTF-8-*-''' Created on 2012-9-3 @ author: Jekey cosine correlation. if the data is sparse, consider using this algorithm '''import codecsfrom math import sqrtusers = {"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 }}# cosine distance from def cosine (rate1, rate2): sum_xy = 0 sum_x = 0 sum_y = 0 n = 0 for key in rate1: if key in rate2: n + = 1 x = rate1 [key] y = rate2 [key] sum_xy + = x * y sum_x + = x * x sum_y + = y * y # calculate the distance from if n = = 0: return 0 else: sx = pow (sum_x, 1/2) sy = pow (sum_y, 1/2) if sum_xy <> 0: denominator = sx * sy/sum_xy else: denominator = 0 return denominator # return the closest user def computeNearestNeighbor (username, users): distances = [] for key in users: if key <> username: distance = cosine (users [username], users [key]) distances. append (distance, key) distances. sort () return distances # recommended 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)

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.