Tavsiye Sistemi

Toby Segaran'in Collective Intelligence adli kitabinin 2. bolumunden esinlenerek tavsiye sistemlerine bakalim.

u.data u.item

In [1]:
##############################################################################
# A dictionary of movie critics and their ratings of a small
# set of movies
# This dictionary uses a ranking from 1 to 5
critics={
    'Lisa Rose':
        {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,
        'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,
        'The Night Listener': 3.0},
    'Gene Seymour':
        {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,
        'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,
         'You, Me and Dupree': 3.5},
    'Michael Phillips':
        {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,
        'Superman Returns': 3.5, 'The Night Listener': 4.0},
    'Claudia Puig':
        {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
         'The Night Listener': 4.5, 'Superman Returns': 4.0,
         'You, Me and Dupree': 2.5},
    'Mick LaSalle':
        {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
         'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,
         'You, Me and Dupree': 2.0},
    'Jack Matthews':
        {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
         'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
    'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}
}
#print(critics.keys())
print("\nLisa Rose critics: ", critics['Lisa Rose'])
print("Lisa Rose critics for the film Lady in the Water: ", critics['Lisa Rose']['Lady in the Water'])
Lisa Rose critics:  {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5, 'The Night Listener': 3.0}
Lisa Rose critics for the film Lady in the Water:  2.5
In [2]:
##############################################################################
from math import sqrt
# Returns a distance-based similarity score for person1 and person2
def sim_distance(prefs,person1,person2):
    # Get the list of shared_items
    si={}
    for item in prefs[person1]:
        if item in prefs[person2]:
            si[item]=1

    # if they have no ratings in common, return 0
    if len(si)==0: return 0

    # Add up the squares of all the differences
    sum_of_squares = sum([pow(prefs[person1][item] - prefs[person2][item], 2)
                          for item in prefs[person1] if item in prefs[person2]])
    return 1 / (1 + sum_of_squares)

print("\nSimilarity (using Euclidean distance)between Lisa Rose and Gene Seymour: ", sim_distance(critics,'Lisa Rose','Gene Seymour'))
Similarity (using Euclidean distance)between Lisa Rose and Gene Seymour:  0.14814814814814814
In [3]:
# This function will return a value between –1 and 1.
# Returns the Pearson correlation coefficient for p1 and p2
def sim_pearson(prefs,p1,p2):
    # Get the list of mutually rated items
    si={}
    for item in prefs[p1]:
        if item in prefs[p2]:
            si[item]=1
    # Find the number of elements
    n=len(si)
    # if they are no ratings in common, return 0
    if n==0:
        return 0
    # Add up all the preferences
    sum1=sum([prefs[p1][it] for it in si])
    sum2=sum([prefs[p2][it] for it in si])
    # Sum up the squares
    sum1Sq=sum([pow(prefs[p1][it],2) for it in si])
    sum2Sq=sum([pow(prefs[p2][it],2) for it in si])
    # Sum up the products
    pSum=sum([prefs[p1][it]*prefs[p2][it] for it in si])
    # Calculate Pearson score
    num=pSum-(sum1*sum2/n)
    den=sqrt((sum1Sq-pow(sum1,2)/n)*(sum2Sq-pow(sum2,2)/n))
    if den==0: return 0
    r=num/den
    return r


print("Similarity (using Pearson correlation) between Lisa Rose and Gene Seymour: ",
    sim_pearson(critics,'Lisa Rose','Gene Seymour'))
Similarity (using Pearson correlation) between Lisa Rose and Gene Seymour:  0.39605901719066977
In [4]:
##############################################################################
# Returns the best matches for person from the prefs dictionary.
# Number of results and similarity function are optional params.
def topMatches(prefs,person,n=5,similarity=sim_pearson):
    scores=[(similarity(prefs,person,other),other) for other in prefs if other!=person]
    # Sort the list so the highest scores appear at the top
    scores.sort( )
    scores.reverse( )
    return scores[0:n]

print("\nSimilar (top-5) person like Toby: ", topMatches(critics,'Toby',n=3));
Similar (top-5) person like Toby:  [(0.9912407071619299, 'Lisa Rose'), (0.9244734516419049, 'Mick LaSalle'), (0.8934051474415647, 'Claudia Puig')]
In [5]:
# Gets recommendations for a person by using a weighted average
# of every other user's rankings
def getRecommendations(prefs,person,similarity=sim_pearson):
    totals={}
    simSums={}
    for other in prefs:
        # don't compare me to myself
        if other==person: continue
        sim=similarity(prefs,person,other)
        # ignore scores of zero or lower
        if sim<=0: continue
        for item in prefs[other]:
            # only score movies I haven't seen yet
            if item not in prefs[person] or prefs[person][item]==0:
                # Similarity * Score
                totals.setdefault(item,0)
                totals[item]+=prefs[other][item]*sim
                # Sum of similarities
                simSums.setdefault(item,0)
                simSums[item]+=sim
    # Create the normalized list
    rankings=[(total/simSums[item],item) for item,total in totals.items()]
    # Return the sorted list
    rankings.sort( )
    rankings.reverse( )
    return rankings

print("Gets recommendations for Toby:", getRecommendations(critics,'Toby'))
Gets recommendations for Toby: [(3.3477895267131017, 'The Night Listener'), (2.8325499182641614, 'Lady in the Water'), (2.530980703765565, 'Just My Luck')]
In [6]:
##############################################################################
def transformPrefs(prefs):
    result={}
    for person in prefs:
        for item in prefs[person]:
            result.setdefault(item,{})
            # Flip item and person
            result[item][person]=prefs[person][item]
    return result


movies= transformPrefs(critics)
print("\nMovies:" , movies)
print("TopMatches:" ,topMatches(movies,'Superman Returns'))
Movies: {'Lady in the Water': {'Lisa Rose': 2.5, 'Gene Seymour': 3.0, 'Michael Phillips': 2.5, 'Mick LaSalle': 3.0, 'Jack Matthews': 3.0}, 'Snakes on a Plane': {'Lisa Rose': 3.5, 'Gene Seymour': 3.5, 'Michael Phillips': 3.0, 'Claudia Puig': 3.5, 'Mick LaSalle': 4.0, 'Jack Matthews': 4.0, 'Toby': 4.5}, 'Just My Luck': {'Lisa Rose': 3.0, 'Gene Seymour': 1.5, 'Claudia Puig': 3.0, 'Mick LaSalle': 2.0}, 'Superman Returns': {'Lisa Rose': 3.5, 'Gene Seymour': 5.0, 'Michael Phillips': 3.5, 'Claudia Puig': 4.0, 'Mick LaSalle': 3.0, 'Jack Matthews': 5.0, 'Toby': 4.0}, 'You, Me and Dupree': {'Lisa Rose': 2.5, 'Gene Seymour': 3.5, 'Claudia Puig': 2.5, 'Mick LaSalle': 2.0, 'Jack Matthews': 3.5, 'Toby': 1.0}, 'The Night Listener': {'Lisa Rose': 3.0, 'Gene Seymour': 3.0, 'Michael Phillips': 4.0, 'Claudia Puig': 4.5, 'Mick LaSalle': 3.0, 'Jack Matthews': 3.0}}
TopMatches: [(0.6579516949597695, 'You, Me and Dupree'), (0.4879500364742689, 'Lady in the Water'), (0.11180339887498941, 'Snakes on a Plane'), (-0.1798471947990544, 'The Night Listener'), (-0.42289003161103106, 'Just My Luck')]
In [7]:
# Item-based collaborative filtering
def calculateSimilarItems(prefs,n=10):
    # Create a dictionary of items showing which other items they
    # are most similar to.
    result={}
    # Invert the preference matrix to be item-centric
    itemPrefs = transformPrefs(prefs)
    c=0
    for item in itemPrefs:
        # Status updates for large datasets
        c+=1
        if c%100==0:
            print("%d / %d" % (c,len(itemPrefs)))
        # Find the most similar items to this one
        scores=topMatches(itemPrefs,item,n=n,similarity=sim_distance)
        result[item]=scores
    return result

itemsim = calculateSimilarItems(critics) # precomputation will be used
print("\nItem-based collaborative filtering (precomputation):" , itemsim)
Item-based collaborative filtering (precomputation): {'Lady in the Water': [(0.4, 'You, Me and Dupree'), (0.2857142857142857, 'The Night Listener'), (0.2222222222222222, 'Snakes on a Plane'), (0.2222222222222222, 'Just My Luck'), (0.09090909090909091, 'Superman Returns')], 'Snakes on a Plane': [(0.2222222222222222, 'Lady in the Water'), (0.18181818181818182, 'The Night Listener'), (0.16666666666666666, 'Superman Returns'), (0.10526315789473684, 'Just My Luck'), (0.05128205128205128, 'You, Me and Dupree')], 'Just My Luck': [(0.2222222222222222, 'Lady in the Water'), (0.18181818181818182, 'You, Me and Dupree'), (0.15384615384615385, 'The Night Listener'), (0.10526315789473684, 'Snakes on a Plane'), (0.06451612903225806, 'Superman Returns')], 'Superman Returns': [(0.16666666666666666, 'Snakes on a Plane'), (0.10256410256410256, 'The Night Listener'), (0.09090909090909091, 'Lady in the Water'), (0.06451612903225806, 'Just My Luck'), (0.05333333333333334, 'You, Me and Dupree')], 'You, Me and Dupree': [(0.4, 'Lady in the Water'), (0.18181818181818182, 'Just My Luck'), (0.14814814814814814, 'The Night Listener'), (0.05333333333333334, 'Superman Returns'), (0.05128205128205128, 'Snakes on a Plane')], 'The Night Listener': [(0.2857142857142857, 'Lady in the Water'), (0.18181818181818182, 'Snakes on a Plane'), (0.15384615384615385, 'Just My Luck'), (0.14814814814814814, 'You, Me and Dupree'), (0.10256410256410256, 'Superman Returns')]}
In [8]:
##############################################################################
# itemMatch is precomputed
def getRecommendedItems(prefs,itemMatch,user):
    userRatings=prefs[user]
    scores={}
    totalSim={}
    # Loop over items rated by this user
    for (item,rating) in userRatings.items( ):
        # Loop over items similar to this one
        for (similarity,item2) in itemMatch[item]:
            # Ignore if this user has already rated this item
            if item2 in userRatings: continue
            # Weighted sum of rating times similarity
            scores.setdefault(item2,0)
            scores[item2]+=similarity*rating
            # Sum of all the similarities
            totalSim.setdefault(item2,0)
            totalSim[item2]+=similarity
    # Divide each total score by total weighting to get an average
    rankings=[(score/totalSim[item],item) for item,score in scores.items( )]
    # Return the rankings from highest to lowest
    rankings.sort( )
    rankings.reverse( )
    return rankings

print("\nRecommendation via (precomputation) for Toby:" ,getRecommendedItems(critics,itemsim,'Toby'))
Recommendation via (precomputation) for Toby: [(3.182634730538922, 'The Night Listener'), (2.5983318700614575, 'Just My Luck'), (2.4730878186968837, 'Lady in the Water')]
In [11]:
##############################################################################
### u.item
# 1|Toy Story (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)|0|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0
# 2|GoldenEye (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?GoldenEye%20(1995)|0|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0

# Each line has a user ID, a movie ID, the rating given to the movie by the user, and a timestamp.
### u.data
# 196 242 3 881250949
# 186 302 3 891717742

def loadMovieLens(path='data'):
    # Get movie titles
    movies={}
    for line in open(path+'/u.item', encoding='latin-1'):
        (id,title)=line.split('|')[0:2] # 1|Toy Story
        movies[id]=title
    # Load data
    prefs={}
    for line in open(path+'/u.data', encoding='latin-1'):
        (user,movieid,rating,ts)=line.split('\t')
        prefs.setdefault(user,{})
        prefs[user][movies[movieid]]=float(rating)
    return prefs

prefs = loadMovieLens()
print("\n85th critic from Movielens Dataset", prefs['85'])

# t = {1: 4, 3: 0}
# t.setdefault(5,{}) ## dictionary within a dictionary
# t[5][1] = 1; t[5][2] = 2 ##  {1: 4, 3: 0, 5: {1: 1, 2: 2}}


## itemsim = calculateSimilarItems(prefs,n=50)
## print("\nItem-based recommendations for \n\tMovielens Dataset",getRecommendedItems(prefs,itemsim,'87')[0:30])
85th critic from Movielens Dataset {'To Kill a Mockingbird (1962)': 3.0, 'Streetcar Named Desire, A (1951)': 4.0, 'George of the Jungle (1997)': 2.0, 'Beauty and the Beast (1991)': 3.0, 'Legends of the Fall (1994)': 2.0, 'Koyaanisqatsi (1983)': 3.0, 'Star Trek: The Wrath of Khan (1982)': 3.0, 'Grifters, The (1990)': 4.0, 'Heathers (1989)': 3.0, 'Birdcage, The (1996)': 2.0, 'Time to Kill, A (1996)': 3.0, 'Godfather: Part II, The (1974)': 5.0, 'Mighty Aphrodite (1995)': 3.0, 'It Happened One Night (1934)': 4.0, 'Much Ado About Nothing (1993)': 4.0, 'Face/Off (1997)': 4.0, 'Dumbo (1941)': 3.0, 'Restoration (1995)': 2.0, 'Ran (1985)': 4.0, 'Good Will Hunting (1997)': 4.0, 'Brazil (1985)': 4.0, 'Gone with the Wind (1939)': 4.0, 'Silence of the Lambs, The (1991)': 4.0, 'Victor/Victoria (1982)': 3.0, 'Quiz Show (1994)': 4.0, 'Manhattan (1979)': 5.0, 'Fried Green Tomatoes (1991)': 4.0, 'Full Monty, The (1997)': 3.0, 'Natural Born Killers (1994)': 3.0, 'Dances with Wolves (1990)': 2.0, 'Remains of the Day, The (1993)': 4.0, 'American President, The (1995)': 3.0, 'Sunset Blvd. (1950)': 4.0, 'Black Beauty (1994)': 3.0, 'Pulp Fiction (1994)': 4.0, 'Pollyanna (1960)': 3.0, 'Jeffrey (1995)': 4.0, 'Strictly Ballroom (1992)': 5.0, 'Six Degrees of Separation (1993)': 4.0, 'Lion King, The (1994)': 4.0, 'Star Trek: Generations (1994)': 4.0, 'Unforgiven (1992)': 5.0, 'Last of the Mohicans, The (1992)': 3.0, 'People vs. Larry Flynt, The (1996)': 2.0, 'Tin Drum, The (Blechtrommel, Die) (1979)': 3.0, 'Evita (1996)': 3.0, 'Clear and Present Danger (1994)': 3.0, 'My Favorite Year (1982)': 4.0, 'Bullets Over Broadway (1994)': 4.0, 'Lone Star (1996)': 5.0, 'Seven Years in Tibet (1997)': 2.0, 'Pinocchio (1940)': 3.0, 'Cat on a Hot Tin Roof (1958)': 4.0, 'Little Buddha (1993)': 3.0, 'Bridge on the River Kwai, The (1957)': 5.0, 'Flirting With Disaster (1996)': 4.0, 'Adventures of Priscilla, Queen of the Desert, The (1994)': 4.0, 'North by Northwest (1959)': 4.0, 'Diva (1981)': 3.0, 'Sum of Us, The (1994)': 3.0, 'African Queen, The (1951)': 4.0, 'Mediterraneo (1991)': 3.0, 'High Noon (1952)': 4.0, 'Annie Hall (1977)': 5.0, 'Emma (1996)': 3.0, 'Sense and Sensibility (1995)': 3.0, 'Babe (1995)': 4.0, 'Backbeat (1993)': 4.0, 'Air Force One (1997)': 3.0, 'East of Eden (1955)': 4.0, 'Star Trek: The Motion Picture (1979)': 4.0, 'Kids in the Hall: Brain Candy (1996)': 2.0, 'Eat Drink Man Woman (1994)': 3.0, 'Stand by Me (1986)': 3.0, "Schindler's List (1993)": 4.0, 'Unbearable Lightness of Being, The (1988)': 2.0, 'M*A*S*H (1970)': 5.0, 'Vanya on 42nd Street (1994)': 5.0, 'Blade Runner (1982)': 4.0, 'Barcelona (1994)': 2.0, 'Jaws (1975)': 4.0, 'Cop Land (1997)': 3.0, 'Boogie Nights (1997)': 3.0, 'Bob Roberts (1992)': 4.0, 'Beautiful Thing (1996)': 4.0, 'Room with a View, A (1986)': 4.0, 'Bananas (1971)': 4.0, 'Home Alone (1990)': 3.0, 'Platoon (1986)': 3.0, 'Speed (1994)': 3.0, 'Birds, The (1963)': 4.0, 'Killing Fields, The (1984)': 4.0, 'Reality Bites (1994)': 3.0, 'Glengarry Glen Ross (1992)': 3.0, 'Pump Up the Volume (1990)': 3.0, 'Four Weddings and a Funeral (1994)': 4.0, 'Sex, Lies, and Videotape (1989)': 4.0, '8 1/2 (1963)': 3.0, "Sophie's Choice (1982)": 3.0, 'Walkabout (1971)': 3.0, 'Godfather, The (1972)': 5.0, 'Chasing Amy (1997)': 4.0, "Monty Python's Life of Brian (1979)": 4.0, 'Cool Hand Luke (1967)': 4.0, 'Fresh (1994)': 4.0, 'This Is Spinal Tap (1984)': 4.0, 'Stealing Beauty (1996)': 2.0, 'Man Who Would Be King, The (1975)': 3.0, 'Parent Trap, The (1961)': 3.0, 'My Left Foot (1989)': 4.0, 'Raging Bull (1980)': 4.0, 'Swiss Family Robinson (1960)': 3.0, 'Grease (1978)': 4.0, 'Vertigo (1958)': 4.0, 'English Patient, The (1996)': 4.0, 'Jerry Maguire (1996)': 3.0, 'Cinderella (1950)': 3.0, 'Alice in Wonderland (1951)': 4.0, 'When Harry Met Sally... (1989)': 3.0, 'Return of the Pink Panther, The (1974)': 3.0, 'Field of Dreams (1989)': 4.0, 'Shadow Conspiracy (1997)': 2.0, 'In the Name of the Father (1993)': 3.0, 'Lawrence of Arabia (1962)': 4.0, 'Safe (1995)': 4.0, 'Aladdin (1992)': 4.0, 'Fifth Element, The (1997)': 3.0, 'Young Frankenstein (1974)': 5.0, 'Clockwork Orange, A (1971)': 4.0, 'Henry V (1989)': 4.0, 'True Lies (1994)': 3.0, 'Terminator, The (1984)': 3.0, 'Chinatown (1974)': 4.0, 'Swingers (1996)': 3.0, 'My Life as a Dog (Mitt liv som hund) (1985)': 3.0, 'Everyone Says I Love You (1996)': 4.0, 'Mrs. Doubtfire (1993)': 4.0, 'Richard III (1995)': 4.0, 'Old Yeller (1957)': 3.0, 'Contact (1997)': 4.0, 'Rebel Without a Cause (1955)': 4.0, "Singin' in the Rain (1952)": 5.0, 'Apartment, The (1960)': 4.0, 'Fantasia (1940)': 4.0, 'Gandhi (1982)': 4.0, 'Tin Cup (1996)': 3.0, 'Priest (1994)': 5.0, 'Carrie (1976)': 3.0, 'Full Metal Jacket (1987)': 2.0, 'Star Trek: First Contact (1996)': 2.0, 'E.T. the Extra-Terrestrial (1982)': 4.0, 'Cinema Paradiso (1988)': 4.0, 'Fargo (1996)': 3.0, 'Apollo 13 (1995)': 4.0, 'Paths of Glory (1957)': 4.0, 'Bliss (1997)': 4.0, 'Basquiat (1996)': 2.0, 'Patton (1970)': 4.0, 'Sting, The (1973)': 4.0, 'Graduate, The (1967)': 5.0, 'Belle de jour (1967)': 3.0, 'Tin Men (1987)': 3.0, 'Wings of Desire (1987)': 3.0, 'Very Brady Sequel, A (1996)': 3.0, 'Bad Boys (1995)': 4.0, 'Fish Called Wanda, A (1988)': 3.0, 'Spanking the Monkey (1994)': 3.0, 'Citizen Kane (1941)': 5.0, 'Right Stuff, The (1983)': 3.0, 'Snow White and the Seven Dwarfs (1937)': 5.0, 'Postino, Il (1994)': 4.0, 'Arsenic and Old Lace (1944)': 4.0, 'GoodFellas (1990)': 4.0, 'Amadeus (1984)': 4.0, 'Absolute Power (1997)': 3.0, 'Rainmaker, The (1997)': 3.0, 'Gigi (1958)': 3.0, 'Star Trek III: The Search for Spock (1984)': 3.0, 'Sound of Music, The (1965)': 4.0, 'Before Sunrise (1995)': 4.0, 'Trust (1990)': 3.0, 'Around the World in 80 Days (1956)': 3.0, 'Umbrellas of Cherbourg, The (Parapluies de Cherbourg, Les) (1964)': 3.0, 'Fugitive, The (1993)': 3.0, 'Nixon (1995)': 3.0, 'Magnificent Seven, The (1954)': 4.0, 'Paris, Texas (1984)': 4.0, 'My Fair Lady (1964)': 5.0, 'Miracle on 34th Street (1994)': 4.0, "It's a Wonderful Life (1946)": 4.0, 'First Wives Club, The (1996)': 3.0, 'Butch Cassidy and the Sundance Kid (1969)': 4.0, 'Deconstructing Harry (1997)': 4.0, 'In & Out (1997)': 4.0, 'Women, The (1939)': 4.0, 'Like Water For Chocolate (Como agua para chocolate) (1992)': 4.0, 'Until the End of the World (Bis ans Ende der Welt) (1991)': 3.0, 'Local Hero (1983)': 4.0, 'Celluloid Closet, The (1995)': 4.0, 'Indiana Jones and the Last Crusade (1989)': 3.0, 'Jungle Book, The (1994)': 4.0, 'Forrest Gump (1994)': 4.0, 'Monty Python and the Holy Grail (1974)': 4.0, 'Crying Game, The (1992)': 4.0, 'Apocalypse Now (1979)': 4.0, 'Tomorrow Never Dies (1997)': 3.0, 'Conspiracy Theory (1997)': 3.0, 'All About Eve (1950)': 4.0, 'Piano, The (1993)': 4.0, 'Sleeper (1973)': 5.0, 'Taxi Driver (1976)': 4.0, 'Dave (1993)': 3.0, 'Star Trek IV: The Voyage Home (1986)': 3.0, 'Third Man, The (1949)': 4.0, 'Little Odessa (1994)': 2.0, 'Mary Poppins (1964)': 5.0, 'Independence Day (ID4) (1996)': 2.0, 'Bonnie and Clyde (1967)': 4.0, 'Faces (1968)': 3.0, 'Better Off Dead... (1985)': 2.0, 'Jurassic Park (1993)': 3.0, 'Wizard of Oz, The (1939)': 5.0, 'Mr. Smith Goes to Washington (1939)': 4.0, 'Ghosts of Mississippi (1996)': 3.0, 'Caught (1996)': 3.0, 'Manchurian Candidate, The (1962)': 4.0, 'Enchanted April (1991)': 4.0, "What's Eating Gilbert Grape (1993)": 3.0, 'Searching for Bobby Fischer (1993)': 4.0, 'Raiders of the Lost Ark (1981)': 4.0, 'Raise the Red Lantern (1991)': 4.0, 'Philadelphia Story, The (1940)': 4.0, 'Batman Returns (1992)': 2.0, 'That Thing You Do! (1996)': 3.0, 'Shine (1996)': 4.0, 'Young Guns (1988)': 3.0, 'Princess Bride, The (1987)': 3.0, 'White Squall (1996)': 1.0, 'Casablanca (1942)': 5.0, 'Philadelphia (1993)': 3.0, 'Hunchback of Notre Dame, The (1996)': 3.0, 'Wild Reeds (1994)': 3.0, 'Breaking the Waves (1996)': 2.0, 'Madness of King George, The (1994)': 3.0, '2001: A Space Odyssey (1968)': 5.0, 'Ben-Hur (1959)': 4.0, 'Deer Hunter, The (1978)': 3.0, 'Wild Bunch, The (1969)': 3.0, 'Star Wars (1977)': 5.0, 'Raising Arizona (1987)': 2.0, "One Flew Over the Cuckoo's Nest (1975)": 4.0, 'Titanic (1997)': 4.0, 'Basketball Diaries, The (1995)': 3.0, 'Living in Oblivion (1995)': 4.0, 'Shawshank Redemption, The (1994)': 5.0, 'Great Race, The (1965)': 3.0, 'River Wild, The (1994)': 3.0, 'Gattaca (1997)': 3.0, 'Treasure of the Sierra Madre, The (1948)': 4.0, 'Tie Me Up! Tie Me Down! (1990)': 4.0, 'As Good As It Gets (1997)': 3.0, 'Harold and Maude (1971)': 5.0, 'Boot, Das (1981)': 5.0, 'Back to the Future (1985)': 4.0, 'Farewell My Concubine (1993)': 3.0, 'Mrs. Parker and the Vicious Circle (1994)': 2.0, 'Mission: Impossible (1996)': 2.0, '20,000 Leagues Under the Sea (1954)': 3.0, 'Clerks (1994)': 3.0, 'Blues Brothers, The (1980)': 3.0, 'Dead Man Walking (1995)': 4.0, 'Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1963)': 5.0, 'Hamlet (1996)': 4.0, 'Empire Strikes Back, The (1980)': 4.0, 'Ruling Class, The (1972)': 3.0, 'Some Like It Hot (1959)': 4.0, 'Being There (1979)': 5.0, 'On Golden Pond (1981)': 2.0, 'To Die For (1995)': 4.0, 'Crash (1996)': 2.0, 'Man of No Importance, A (1994)': 3.0, 'Dead Poets Society (1989)': 4.0, 'Top Gun (1986)': 4.0, 'Great Escape, The (1963)': 3.0, 'Game, The (1997)': 1.0, 'Return of the Jedi (1983)': 4.0, 'Love & Human Remains (1993)': 4.0}
In [10]:
########################################################################
DB = {
    "uzay" : {"Star Wars": 4.5, "Superman": 3.5,"Batman": 4,},
    "selin" : {"Venedik": 4.5, "Paris": 3.5,"Batman": 4,},
    "fatih" : {"Superman": 4,"Batman": 4},
}
print("\nUzaycritics: ", DB['uzay'])
print("Uzay critics for the film Star Wars: ", DB['uzay']['Star Wars'])
print("\nSimilarity (using Euclidean distance)between uzay and fatih: ", sim_distance(DB,'uzay','fatih'))
print("\nSimilarity (using Euclidean distance)between selin and fatih: ", sim_distance(DB,'selin','fatih'))


print("\nSimilar (top-5) person like fatih: ", topMatches(DB,'fatih',similarity=sim_distance));
print("Gets recommendations for fatih:", getRecommendations(DB,'fatih',similarity=sim_distance))
Uzaycritics:  {'Star Wars': 4.5, 'Superman': 3.5, 'Batman': 4}
Uzay critics for the film Star Wars:  4.5

Similarity (using Euclidean distance)between uzay and fatih:  0.8

Similarity (using Euclidean distance)between selin and fatih:  1.0

Similar (top-5) person like fatih:  [(1.0, 'selin'), (0.8, 'uzay')]
Gets recommendations for fatih: [(4.5, 'Venedik'), (4.5, 'Star Wars'), (3.5, 'Paris')]
In [ ]: