-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixify.py
94 lines (80 loc) · 2.91 KB
/
matrixify.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
import pandas as pd
import numpy as np
def CUR_decomposition(A):
"""
A = CUR
A: m x n matrix
C: m x r matrix
U: r x r matrix
R: r x n matrix
"""
# Get the shape of the matrix
m, n = A.shape
# Get the number of rows and columns
r = int(m/10)
# Get the row and column probabilities
row_prob = np.sum(A**2, axis=1)
row_prob = row_prob/np.sum(row_prob)
col_prob = np.sum(A**2, axis=0)
col_prob = col_prob/np.sum(col_prob)
# Get the row and column indices
row_indices = np.random.choice(m, r, replace=False, p=row_prob)
col_indices = np.random.choice(n, r, replace=False, p=col_prob)
# Get the row and column matrices
R = A[row_indices, :]
C = A[:, col_indices]
W = A[row_indices, :][:, col_indices]
print("This is R")
print(R)
print("This is C")
print(C)
X, Z, Y = np.linalg.svd(W)
# Take reciprocal of non zero values in Z
Z = 1/Z
# Make Z diagonal
Z = np.diag(Z)
# print(Z)
print(X.shape, Z.shape, Y.shape)
U = Y.T @ (Z**2) @ X.T
print(U.shape)
return C, U, R
# train_matrix = pd.read_csv("dataset/matrix.csv")
# train_matrix = train_matrix.drop('Unnamed: 0', axis=1)
# print(train_matrix.head())
# Convert to numpy array
# train_matrix = train_matrix.values
# A = np.array(train_matrix)
# C, U, R = CUR_decomposition(A)
# prediction_matrix = C @ U @ R
# # print(prediction_matrix)
# # Save the prediction matrix
# np.savetxt("dataset/prediction_matrix.csv", prediction_matrix, delimiter=",")
# print(prediction_matrix)
# df = pd.io.parsers.read_csv('dataset/ratings.dat', names=[
# 'userid', 'movieid', 'ratings', 'time'], encoding='latin-1', engine='python', delimiter='::')
# train_df = pd.read_csv("dataset/train_ratings.csv")
# unique_users = df['userid'].unique()
# unique_movies = df['movieid'].unique()
# print(len(unique_users), len(unique_movies))
# unique_users = train_df['userid'].unique()
# unique_movies = train_df['movieid'].unique()
# print(len(unique_users), len(unique_movies))
def create_matrix(filepath="dataset/train_ratings.csv", dest="dataset/matrix.csv"):
df = pd.io.parsers.read_csv('dataset/ratings.dat', names=[
'userid', 'movieid', 'ratings', 'time'], encoding='latin-1', engine='python', delimiter='::')
train_df = pd.read_csv(filepath)
unique_users = df['userid'].unique()
unique_movies = df['movieid'].unique()
# Create a matrix
matrix = pd.DataFrame(index=unique_users, columns=unique_movies)
# Fill the matrix with the ratings
for i in range(len(train_df)):
matrix.loc[train_df['userid'][i],
train_df['movieid'][i]] = train_df['ratings'][i]
# Fill the NaN values with 0
matrix.fillna(value=0, inplace=True)
# Save the matrix
matrix.to_csv(dest)
print(matrix)
# 6040, 3685
create_matrix("dataset/test_ratings.csv", "dataset/matrix_test.csv")