|
| 1 | +####Movie Lens 1m### |
| 2 | + |
| 3 | +import psycopg2 |
| 4 | +import sys |
| 5 | +import time |
| 6 | +import os |
| 7 | +def main(): |
| 8 | + host = raw_input("Enter the host address of postgresql: ") |
| 9 | + |
| 10 | + dbname = raw_input("Enter the database name: ") |
| 11 | + |
| 12 | + user = raw_input("Enter the username of postgresql: ") |
| 13 | + |
| 14 | + password = raw_input("Enter the password of postgresql: ") |
| 15 | + |
| 16 | + UserDataPath = raw_input("Enter the abs path for user data(.CSV file): ") |
| 17 | + |
| 18 | + ItemDataPath = raw_input("Enter the abs path for item data(.CSV file):: ") |
| 19 | + |
| 20 | + RatingDataPath = raw_input("Enter the abs path for ratings data(.csv file): ") |
| 21 | + |
| 22 | + |
| 23 | + #Define our connection string |
| 24 | + conn_string = "host='"+host+"' dbname='"+dbname+"' user='"+user+"' password='"+password+"'" |
| 25 | + |
| 26 | + # print the connection string we will use to connect |
| 27 | + print "Connecting to database\n ->%s" % (conn_string) |
| 28 | + |
| 29 | + # get a connection, if a connect cannot be made an exception will be raised here |
| 30 | + conn = psycopg2.connect(conn_string) |
| 31 | + cursor = conn.cursor() |
| 32 | + |
| 33 | + print "Data copying from .csv file to postgresql database" |
| 34 | + |
| 35 | + import os |
| 36 | + path = os.getcwd() |
| 37 | + |
| 38 | + executionStart = time.time() |
| 39 | + |
| 40 | + cursor.execute(" set client_encoding = LATIN1;"); |
| 41 | + conn.commit() |
| 42 | + |
| 43 | + cursor.execute("create table if not exists users( userid int, age varchar, gender varchar, job varchar, zipcode varchar);"); |
| 44 | + conn.commit() |
| 45 | + executionTime = time.time() - executionStart |
| 46 | + print "\n Execution time is :-" |
| 47 | + print executionTime |
| 48 | + |
| 49 | + executionStart = time.time() |
| 50 | + query = "COPY users(userid,gender,age,job,zipcode) from "+UserDataPath+"DELIMITERS ';';" |
| 51 | + cursor.execute(query); |
| 52 | + conn.commit() |
| 53 | + executionTime = time.time() - executionStart |
| 54 | + print "\n Execution time is :-" |
| 55 | + print executionTime |
| 56 | + |
| 57 | + cursor.execute("create table if not exists moive( itemid int, name varchar, genre varchar);"); |
| 58 | + conn.commit() |
| 59 | + |
| 60 | + query = "COPY moive(itemid,name,genre) from "+ItemDataPath+" DELIMITERS ';';" |
| 61 | + cursor.execute(query); |
| 62 | + conn.commit() |
| 63 | + |
| 64 | + |
| 65 | + cursor.execute("create table if not exists ratings ( userid int, itemid int, rating real ,garbage varchar);"); |
| 66 | + conn.commit() |
| 67 | + |
| 68 | + query = "COPY ratings(userid,itemid,rating,garbage) from "+RatingDataPath+" DELIMITERS ';';" |
| 69 | + cursor.execute(query); |
| 70 | + conn.commit() |
| 71 | + |
| 72 | + print "Connected!\n" |
| 73 | + |
| 74 | + print "Recommendation query being shooted with ItemCosCF technique" |
| 75 | + |
| 76 | + ############### |
| 77 | + |
| 78 | + print "\n \n Creating Recommender.." |
| 79 | + executionStart = time.time() |
| 80 | + cursor.execute("CREATE RECOMMENDER mlRecItemCos on ratings Users FROM userid Items FROM itemid Events FROM rating Using ItemCosCF;"); |
| 81 | + conn.commit() |
| 82 | + executionTime = time.time() - executionStart |
| 83 | + print " Execution time is :-" |
| 84 | + print executionTime |
| 85 | + |
| 86 | + ############### |
| 87 | + |
| 88 | + print "\n \n Selection of movie for single user.." |
| 89 | + executionStart = time.time() |
| 90 | + cursor.execute("select itemid from ratings RECOMMEND itemid to userid ON rating Using ItemCosCF where userid =21;"); |
| 91 | + conn.commit() |
| 92 | + executionTime = time.time() - executionStart |
| 93 | + print " Execution time is :-" |
| 94 | + print executionTime |
| 95 | + |
| 96 | + ############### |
| 97 | + |
| 98 | + print "\n \n Single Join Query.." |
| 99 | + executionStart = time.time() |
| 100 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating from ratings r, moive i Recommend r.itemid to r.userid On r.rating Using ItemCosCF where r.itemid = i.itemid AND i.genre ILIKE '%action%' and r.userid = 1;"); |
| 101 | + conn.commit() |
| 102 | + executionTime = time.time() - executionStart |
| 103 | + print "Execution time is :-" |
| 104 | + print executionTime |
| 105 | + |
| 106 | + ################ |
| 107 | + |
| 108 | + print "\n \n Second Join Query.." |
| 109 | + executionStart = time.time() |
| 110 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using ItemCosCF where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' ;"); |
| 111 | + conn.commit() |
| 112 | + executionTime = time.time() - executionStart |
| 113 | + print " Execution time is :-" |
| 114 | + print executionTime |
| 115 | + |
| 116 | + ################ |
| 117 | + |
| 118 | + |
| 119 | + print "\n \n Order by 10 .." |
| 120 | + executionStart = time.time() |
| 121 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using ItemCosCF where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' Order by rating DESC limit 10;"); |
| 122 | + conn.commit() |
| 123 | + executionTime = time.time() - executionStart |
| 124 | + print "Execution time is :-" |
| 125 | + print executionTime |
| 126 | + |
| 127 | + ################ |
| 128 | + |
| 129 | + print "\n \n Order by 50 .." |
| 130 | + executionStart = time.time() |
| 131 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using ItemCosCF where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' Order by rating DESC limit 50;"); |
| 132 | + conn.commit() |
| 133 | + executionTime = time.time() - executionStart |
| 134 | + print " Execution time is :-" |
| 135 | + print executionTime |
| 136 | + |
| 137 | + ################ |
| 138 | + |
| 139 | + print "\n \n Order by 100.." |
| 140 | + executionStart = time.time() |
| 141 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using ItemCosCF where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' Order by rating DESC limit 100;"); |
| 142 | + conn.commit() |
| 143 | + executionTime = time.time() - executionStart |
| 144 | + print "Execution time is :-" |
| 145 | + print executionTime |
| 146 | + |
| 147 | + ################ |
| 148 | + ################ |
| 149 | + |
| 150 | + print "\n Recommendation query being shooted with ItemPearCF technique" |
| 151 | + |
| 152 | + ############### |
| 153 | + |
| 154 | + print "\n \n Creating Recommender.." |
| 155 | + executionStart = time.time() |
| 156 | + cursor.execute("CREATE RECOMMENDER mlRecItemPear on ratings Users FROM userid Items FROM itemid Events FROM rating Using ItemPearCF;"); |
| 157 | + conn.commit() |
| 158 | + executionTime = time.time() - executionStart |
| 159 | + print "Execution time is :-" |
| 160 | + print executionTime |
| 161 | + |
| 162 | + ############### |
| 163 | + |
| 164 | + print "\n \n Selection of movie for single user.." |
| 165 | + executionStart = time.time() |
| 166 | + cursor.execute("select itemid from ratings RECOMMEND itemid to userid ON rating Using ItemPearCF where userid =21;"); |
| 167 | + conn.commit() |
| 168 | + executionTime = time.time() - executionStart |
| 169 | + print " Execution time is :-" |
| 170 | + print executionTime |
| 171 | + |
| 172 | + ############### |
| 173 | + |
| 174 | + print "\n \n Single Join Query.." |
| 175 | + executionStart = time.time() |
| 176 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating from ratings r, moive i Recommend r.itemid to r.userid On r.rating Using ItemPearCF where r.itemid = i.itemid AND i.genre ILIKE '%action%' and r.userid = 1;"); |
| 177 | + conn.commit() |
| 178 | + executionTime = time.time() - executionStart |
| 179 | + print " Execution time is :-" |
| 180 | + print executionTime |
| 181 | + |
| 182 | + ################ |
| 183 | + |
| 184 | + print "\n \n Second Join Query.." |
| 185 | + executionStart = time.time() |
| 186 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating using ItemPearCF where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' ;"); |
| 187 | + conn.commit() |
| 188 | + executionTime = time.time() - executionStart |
| 189 | + print " Execution time is :-" |
| 190 | + print executionTime |
| 191 | + |
| 192 | + ################ |
| 193 | + |
| 194 | + print "\n \n Order by 10 .." |
| 195 | + executionStart = time.time() |
| 196 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using ItemPearCF where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' Order by rating DESC limit 10;"); |
| 197 | + conn.commit() |
| 198 | + executionTime = time.time() - executionStart |
| 199 | + print " Execution time is :-" |
| 200 | + print executionTime |
| 201 | + |
| 202 | + ################ |
| 203 | + |
| 204 | + print "\n \n Order by 50 .." |
| 205 | + executionStart = time.time() |
| 206 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using ItemPearCF where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' Order by rating DESC limit 50;"); |
| 207 | + conn.commit() |
| 208 | + executionTime = time.time() - executionStart |
| 209 | + print " Execution time is :-" |
| 210 | + print executionTime |
| 211 | + |
| 212 | + ################ |
| 213 | + |
| 214 | + print "\n \n Order by 100.." |
| 215 | + executionStart = time.time() |
| 216 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using ItemPearCF where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' Order by rating DESC limit 100;"); |
| 217 | + conn.commit() |
| 218 | + executionTime = time.time() - executionStart |
| 219 | + print " Execution time is :-" |
| 220 | + print executionTime |
| 221 | + |
| 222 | + ################ |
| 223 | + ################ |
| 224 | + |
| 225 | + print "\n Recommendation query being shooted with SVD technique" |
| 226 | + |
| 227 | + ############### |
| 228 | + |
| 229 | + print "\n \n Creating Recommender.." |
| 230 | + executionStart = time.time() |
| 231 | + cursor.execute("CREATE RECOMMENDER mlRecSVD on ratings Users FROM userid Items FROM itemid Events FROM rating Using SVD; "); |
| 232 | + conn.commit() |
| 233 | + executionTime = time.time() - executionStart |
| 234 | + print " Execution time is :-" |
| 235 | + print executionTime |
| 236 | + |
| 237 | + ############### |
| 238 | + |
| 239 | + print " \n \n Selection of movie for single user.." |
| 240 | + executionStart = time.time() |
| 241 | + cursor.execute(" select itemid from ratings RECOMMEND itemid to userid ON rating Using SVD where userid =21;"); |
| 242 | + conn.commit() |
| 243 | + executionTime = time.time() - executionStart |
| 244 | + print " Execution time is :-" |
| 245 | + print executionTime |
| 246 | + |
| 247 | + ############### |
| 248 | + |
| 249 | + print " \n \n Single Join Query.." |
| 250 | + executionStart = time.time() |
| 251 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating from ratings r, moive I Recommend r.itemid to r.userid On r.rating Using SVD where r.itemid = i.itemid AND i.genre ILIKE '%action%' and r.userid = 1;"); |
| 252 | + conn.commit() |
| 253 | + executionTime = time.time() - executionStart |
| 254 | + print " Execution time is :-" |
| 255 | + print executionTime |
| 256 | + |
| 257 | + ################ |
| 258 | + |
| 259 | + print "\n \n Second Join Query.." |
| 260 | + executionStart = time.time() |
| 261 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using SVD where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' ;"); |
| 262 | + conn.commit() |
| 263 | + executionTime = time.time() - executionStart |
| 264 | + print " Execution time is :-" |
| 265 | + print executionTime |
| 266 | + |
| 267 | + ################ |
| 268 | + |
| 269 | + print "\n \n Order by 10 .." |
| 270 | + executionStart = time.time() |
| 271 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using SVD where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' Order by rating DESC limit 10;"); |
| 272 | + conn.commit() |
| 273 | + executionTime = time.time() - executionStart |
| 274 | + print " Execution time is :-" |
| 275 | + print executionTime |
| 276 | + |
| 277 | + ################ |
| 278 | + |
| 279 | + print "\n \n Order by 50 .." |
| 280 | + executionStart = time.time() |
| 281 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using SVD where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' Order by rating DESC limit 50;"); |
| 282 | + conn.commit() |
| 283 | + executionTime = time.time() - executionStart |
| 284 | + print " Execution time is :-" |
| 285 | + print executionTime |
| 286 | + |
| 287 | + ################ |
| 288 | + |
| 289 | + print "\n \n Order by 100.." |
| 290 | + executionStart = time.time() |
| 291 | + cursor.execute("select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using SVD where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' Order by rating DESC limit 100;"); |
| 292 | + conn.commit() |
| 293 | + executionTime = time.time() - executionStart |
| 294 | + print "Execution time is :-" |
| 295 | + print executionTime |
| 296 | + |
| 297 | + ################ |
| 298 | + |
| 299 | + cursor.execute("drop table ratings;"); |
| 300 | + conn.commit() |
| 301 | + |
| 302 | + cursor.execute("drop table users;"); |
| 303 | + conn.commit() |
| 304 | + |
| 305 | + cursor.execute("drop table moive;"); |
| 306 | + conn.commit() |
| 307 | + |
| 308 | + |
| 309 | + |
| 310 | + |
| 311 | +if __name__ == "__main__": |
| 312 | + main() |
0 commit comments