-
Notifications
You must be signed in to change notification settings - Fork 0
/
svd4.py
155 lines (131 loc) · 5.22 KB
/
svd4.py
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
from sqlite3.dbapi2 import connect
import pandas as pd
import numpy as np
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.decomposition import TruncatedSVD
from sklearn import preprocessing
import sqlite3
# from svd import predict_rating,SVD
def predict_rating(user_mat, movie_mat, user_id, movie_id,data):
# Use the training data to create a series of users and movies that matches the ordering in training data
user_ids_series = np.array(data.index)
movie_ids_series = np.array(data.columns)
print(user_ids_series[:2])
print(movie_ids_series[:2])
# User row and Movie Column
user_row = np.where(user_ids_series == user_id)[0]
movie_col = np.where(movie_ids_series == movie_id)[0]
# Take dot product of that row and column in U and V to make prediction
pred = np.dot(user_mat, movie_mat)
print("USERS AND MOVIESs")
print(movie_ids_series)
print(user_ids_series)
# print(user_row)
# print(movie_col)
print("PRED")
print(pred.shape)
print(pred)
user_row = 568
print(movie_id)
print(float(pred[user_row,movie_col]))
print(type(float(pred[user_row,movie_col])))
return float(pred[user_row,movie_col])
def SVD(epochs,learning_rate=0.01,latent_features=6,db="movies2.db"):
con = sqlite3.connect(db)
data = pd.read_sql("select * from itemRatings limit 100000",con)
print(data.head)
"""
names = ['userId', 'movieId', 'rating', 'timestamp']
data = pd.read_csv('ml-100k/u.data', '\t', names=names,
engine='python')
# https://medium.com/@gazzaazhari/model-based-collaborative-filtering-systems-with-machine-learning-algorithm-d5994ae0f53b
columns = ['item_id', 'movie title', 'release date', 'video release date', 'IMDb URL', 'unknown', 'Action', 'Adventure',
'Animation', 'Childrens', 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy', 'Film-Noir', 'Horror',
'Musical', 'Mystery', 'Romance', 'Sci-Fi', 'Thriller', 'War', 'Western']
movies = pd.read_csv('ml-100k/u.item', sep='|', names=columns, encoding='latin-1')
movie_names = movies[['item_id', 'movie title']]
movie_names.head()
"""
data['user'] = data['user'].astype('str')
data['itemID'] = data['itemID'].astype('str')
users = data['user'].unique()
movies = data['itemID'].unique()
print(data.shape)
#training_df, validation_df,new_df = create_train_test(data,0.9)
dataDF=data.pivot_table(index='user', columns='itemID', values='rating',fill_value=0)
# print("TRAIN")
# print(dataDF.shape)
# print(dataDF.head())
# NEW END
data = dataDF.to_numpy()
p = np.random.uniform(0,1.1,size=(data.shape[0],latent_features))
q = np.random.uniform(0,1.1,size=(latent_features, data.shape[1]))
num_ratings = np.count_nonzero(~np.isnan(data))
print(f"numratings:{num_ratings}")
sse_accum = 0
# print("PRED")
# print(pred)
# print("DATA")
# print(data)
pred = p.dot(q)
print("DELTA")
print(pred-data)
for epoch in range(epochs):
sse_accum=0
num_ratings = 0
for i in range(data.shape[0]):
for j in range(data.shape[1]):
# sse_accum = 0
# if the rating exists
# print(data[i,j])
if data[i, j] > 0:
diff = data[i, j] - np.dot(p[i, :], q[:, j])
num_ratings += 1
sse_accum += diff**2 #keep tracking the sum of square error for the matrix
for k in range(latent_features):
p[i, k] += learning_rate * (2*diff*q[k, j])
q[k, j] += learning_rate * (2*diff*p[i, k])
learning_rate = learning_rate/1.02
pred = p.dot(q)
pred[np.where(data == 0)] = 0
print("DELTA")
print(pred-data)
print("%d \t\t %f" % (epoch+1, sse_accum / num_ratings))
return(p,q,data,dataDF)
def SVDUpgrade(d,epochs, user_matrix, item_matrix, learning_rate=0.0001,latent_features=6):
data = d
p = user_matrix
q = item_matrix
num_ratings = np.count_nonzero(~np.isnan(data))
sse_accum = 0
pred = p.dot(q)
print("PRED")
print(pred)
print("DATA")
print(data)
print("DELTA")
print(pred-data)
for epoch in range(epochs):
sse_accum=0
num_ratings = 0
for i in range(data.shape[0]):
for j in range(data.shape[1]):
# sse_accum = 0
# if the rating exists
# print(data[i,j])
if data[i, j] > 0:
diff = data[i, j] - np.dot(p[i, :], q[:, j])
num_ratings += 1
sse_accum += diff**2 #keep tracking the sum of square error for the matrix
for k in range(latent_features):
p[i, k] += learning_rate * (2*diff*q[k, j])
q[k, j] += learning_rate * (2*diff*p[i, k])
learning_rate = learning_rate/1.02
print("%d \t\t %f" % (epoch+1, sse_accum / num_ratings))
return(p,q)
#p,q,data,dataDF=SVD(7)
#p,q=SVDUpgrade(data, 3,p,q)
# print(predict_rating(p,q,1,3448,dataDF))
# print(predict_rating(p,q,1,2692,dataDF))
# print(predict_rating(p,q,1,2843,dataDF))