-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpython_recommender.py
More file actions
176 lines (143 loc) · 4.96 KB
/
python_recommender.py
File metadata and controls
176 lines (143 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import psycopg2
import pandas as pd
psql = psycopg2.connect(host = "localhost", database = "dvd_rental",
user = "postgres", password = "psqldbase")
cursor = psql.cursor()
pd.read_sql("SELECT * FROM language;", psql)
sql = "SELECT * FROM movies_rental;"
movie_data = pd.read_sql(sql, psql)
movie_data.iloc[:, 0:4].head()
def unbinarize(df, start, end):
ex = df.iloc[:, start:end].apply(lambda x: "".join(x.astype(str)), axis = 1)
return [int(i, 2) for i in ex]
compressed_movie = pd.DataFrame()
compressed_movie["customer"] = movie_data["customer"]
compressed_movie["bit1"] = unbinarize(movie_data, 1, 26)
compressed_movie["bit2"] = unbinarize(movie_data, 26, 51)
compressed_movie["bit3"] = unbinarize(movie_data, 51, 76)
compressed_movie["bit4"] = unbinarize(movie_data, 76, 101)
compressed_movie[0:5]
def hash_fun(df, list_of_columns):
return df.iloc[:, list_of_columns].apply(lambda x: "".join(x.astype(str)), axis = 1)
hash_fun(movie_data[0:4], [1, 2, 3, 4])
compressed_movie["bucket1"] = hash_fun(movie_data, [1, 15, 23, 67, 89])
compressed_movie["bucket2"] = hash_fun(movie_data, [7, 12, 29, 44, 96])
compressed_movie["bucket3"] = hash_fun(movie_data, [33, 11, 3, 52, 74])
compressed_movie.head
compressed_movie[0:5]
from sqlalchemy import create_engine
engine = create_engine('postgresql://postgres:psqldbase@localhost:5432/dvd_rental')
compressed_movie.to_sql("compressed_movies_rental", engine)
def create_index(column, cursor):
sql = "CREATE INDEX %s ON compressed_movies_rental (%s);" % (column, column)
cursor.execute(sql)
create_index("bucket1", cursor)
create_index("bucket2", cursor)
create_index("bucket3", cursor)
psql.commit()
bits_count = """
CREATE OR REPLACE FUNCTION bits_count(value bigint) RETURNS integer AS $$
DECLARE i integer;
c integer;
bits BIT(25);
BEGIN
c := 0;
bits := value::BIT(25);
FOR i IN 1..LENGTH(bits) LOOP
IF substring(bits, i, 1) = B'1' THEN
c := c + 1;
END IF;
END LOOP;
RETURN c;
END;
$$ LANGUAGE plpgsql;
-- another one to accept only integer (coming from python calls mainly)
CREATE OR REPLACE FUNCTION bits_count(value integer) RETURNS integer AS $$
DECLARE i integer;
c integer;
bits BIT(25);
BEGIN
c := 0;
bits := value::BIT(25);
FOR i IN 1..LENGTH(bits) LOOP
IF substring(bits, i, 1) = B'1' THEN
c := c + 1;
END IF;
END LOOP;
RETURN c;
END;
$$ LANGUAGE plpgsql;
"""
cursor.execute(bits_count)
ham_dist = """
CREATE OR REPLACE FUNCTION hamming_distance(
A0 bigint, A1 bigint, A2 bigint, A3 bigint,
B0 bigint, B1 bigint, B2 bigint, B3 bigint
)
RETURNS integer AS $$
BEGIN
RETURN
bits_count(A0 # B0) +
bits_count(A1 # B1) +
bits_count(A2 # B2) +
bits_count(A3 # B3);
END;
$$ LANGUAGE plpgsql;
-- another one to accept only integer (coming from python calls mainly)
CREATE OR REPLACE FUNCTION hamming_distance(
A0 integer, A1 integer, A2 integer, A3 integer,
B0 integer, B1 integer, B2 integer, B3 integer
)
RETURNS integer AS $$
BEGIN
RETURN
bits_count(A0 # B0) +
bits_count(A1 # B1) +
bits_count(A2 # B2) +
bits_count(A3 # B3);
END;
$$ LANGUAGE plpgsql;
"""
cursor.execute(ham_dist)
psql.commit()
#example = ['{0:025b}'.format(255)]
bin_numbers = [b"11101111", b"00000100", b"11011111", b"11111111" ,
b"11111111", b"10001001", b"11011111", b"11111111"]
example = [int(i, 2) for i in bin_numbers]
example
example_query = """SELECT hamming_distance%(example)s;"""
#ex = """SELECT HAMMINGDISTANCE%s;""" %(tuple(example))
#pd.read_sql(sql, psql)
#cursor.execute(ex, {
# "example" : tuple(example)
#})
sql = cursor.mogrify(example_query, {
"example" : tuple(example)
})
pd.read_sql(sql, psql)
#'{0:025b}'.format(255)
#format(255, "025b")
#psql.commit()
customer = "Andrea Henderson"
sql = "SELECT * FROM compressed_movies_rental WHERE customer = '%s'" % customer
customer_data = pd.read_sql(sql, psql)
customer_data
sql = """
SELECT customer, hamming_distance(bit1, bit2, bit3, bit4, %s,%s,%s,%s) AS distance
FROM compressed_movies_rental WHERE bucket1 = '%s' OR bucket2 ='%s'
OR bucket3 = '%s' ORDER BY distance LIMIT 6;
""" % (customer_data.bit1[0], customer_data.bit2[0],
customer_data.bit3[0], customer_data.bit4[0], customer_data.bucket1[0],
customer_data.bucket2[0], customer_data.bucket3[0])
shortlist = pd.read_sql(sql, psql)
query = "SELECT * FROM movies_rental WHERE customer IN %(customers)s"
sql = cursor.mogrify(query, {
"customers" : tuple(shortlist["customer"])
})
neighbors = pd.read_sql(sql, psql)
rec = neighbors.T
col_number = neighbors[neighbors["customer"] == customer].index.item()
rec2 = rec.loc[rec[col_number] == 0]
watched_movies = rec2.apply(lambda x: any(x == 1), axis = 1)
rec3 = rec2[watched_movies]
list(rec3.index)