Created
December 25, 2011 09:05
-
-
Save rohitdholakia/1518970 to your computer and use it in GitHub Desktop.
Script to find similar data for users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys,os | |
import MySQLdb | |
def connect(): | |
conn = MySQLdb.connect (host = "localhost",user = "root",db = "netflix") | |
cursor=conn.cursor() | |
return cursor | |
def getAverage(cursor,id): | |
#Take a movieId and find out the average rating for that movie | |
cursor.execute("select avg(rating) from allRatings where userId="+str(id)) | |
return cursor.fetchone() | |
def getRatings(cursor,id): | |
cursor.execute("select count(*) from allRatings where userId="+str(id)) | |
return cursor.fetchone() | |
cursor = connect() | |
cursor.execute("select distinct(userId) from allRatings") | |
listUsers=[] | |
for i in cursor.fetchall(): | |
listUsers.append(i[0]) | |
forMovies = open(sys.argv[1],'w') | |
for user in listUsers: | |
avg = getAverage(cursor,user)[0] | |
number = getRatings(cursor,user)[0] | |
forMovies.write(str(user)+","+str(avg)+","+str(number)+"\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment