-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreaderdata.py
More file actions
199 lines (174 loc) · 5.68 KB
/
readerdata.py
File metadata and controls
199 lines (174 loc) · 5.68 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import csv
from datetime import datetime
import scipy.sparse as sparse
import numpy as np
import xlrd
import pandas as pd
def unique(old_list):
# Minimize the use_id and item_id in the dataset
count = 1
dic = {}
for i in range(len(old_list)):
if old_list[i] in dic:
old_list[i] = dic[old_list[i]]
else:
dic[old_list[i]] = count
old_list[i] = count
count += 1
return old_list
def readData(dataset = './ratings.csv'):
with open(dataset) as f:
reader=csv.reader(f)
header_row=next(reader)
row, column, value, timestamp= [], [], [],[]
for data in reader:
current_date = int(data[0])
row.append(current_date)
col = int(data[1])
column.append(col)
valu = int(float(data[2]))
value.append(valu)
time = (data[3])
timestamp.append(time)
# if(count>=5000):
# break
# count+=1
row = unique(row)
column = unique(column)
numberOfUser = max(row)+1
numberOfItem = max(column)+1
column = unique(column)
mtx = sparse.coo_matrix((value, (row, column)), shape=(numberOfUser, numberOfItem))
mtx = mtx.todense()
mtx = np.array(mtx)
# mdx = np.zeros([mtx.shape[0], mtx.shape[1]])
# mean_user = []
# for i in range(mtx.shape[0]):
# temp = mtx[i]
# if len(temp[temp != 0]) == 0:
# mean_user.append(0)
# else:
# mean_user.append(int(np.mean(temp[temp != 0])))
#
# for i in range(mtx.shape[0]):
# for j in range(mtx.shape[1]):
# if mtx[i][j] != 0:
# mdx[i][j] = mtx[i][j]
# else:
# mdx[i][j] = mean_user[i]
return numberOfUser, numberOfItem, mtx
def datareader(dataset = './mllm-ratings.csv'):
with open(dataset) as f:
reader=csv.reader(f)
header_row=next(reader)
row, column, value, timestamp= [], [], [],[]
for data in reader:
current_date = int(data[1])
row.append(current_date)
col = int(data[2])
column.append(col)
valu = int(float(data[3]))
value.append(valu)
time = (data[4])
timestamp.append(time)
# if(count>=25000):
# break
row = unique(row)
column = unique(column)
numberOfUser = max(row)+1
numberOfItem = max(column)+1
column = unique(column)
mtx = sparse.coo_matrix((value, (row, column)), shape=(numberOfUser, numberOfItem))
mtx = mtx.todense()
mtx = np.array(mtx)
return numberOfUser, numberOfItem, mtx
def truedata(dataset = './ratings_Amazon_Instant_Video.csv'):
user = {}
item = {}
numberOfUser = 0
numberOfItem = 0
row, column, value, timestamp = [], [], [], []
count = 0
with open(dataset) as f:
reader=csv.reader(f)
header_row = next(reader)
for data in reader:
current_date = (data[0])
if(current_date) not in user:
user.update({current_date:numberOfUser})
numberOfUser +=1
row.append(user[current_date])
col = (data[1])
if (col) not in item:
item.update({col: numberOfItem})
numberOfItem += 1
column.append(item[col])
#
valu = int(float(data[2]))
value.append(valu)
print(user[current_date], item[col], valu)
#
if(count >= 15000):
break
count+=1
# time = (data[3])
# timestamp.append(time)
row = unique(row)
column = unique(column)
numberOfUser +=1
numberOfItem +=1
column = unique(column)
mtx = sparse.coo_matrix((value, (row, column)), shape=(numberOfUser, numberOfItem))
mtx = mtx.todense()
mtx = np.array(mtx)
# mdx = np.zeros([mtx.shape[0], mtx.shape[1]])
# mean_user = []
#
# for i in range(mtx.shape[0]):
# temp = mtx[i]
# if len(temp[temp != 0]) == 0:
# mean_user.append(0)
# else:
# mean_user.append(np.mean(temp[temp != 0]))
#
# for i in range(mtx.shape[0]):
# for j in range(mtx.shape[1]):
# if mtx[i][j] != 0:
# mdx[i][j] = mtx[i][j]
# else:
# mdx[i][j] = mean_user[i]
# # mdx = mtx
return numberOfUser, numberOfItem, mtx
def readerxls(dataset = './jester-data-2.xls') :
data = pd.read_excel(dataset)
var1 =np.array(data)
biglist = []
for i in range(len(var1)):
smalllist=[]
for j in range(len(var1[0])):
if(j==0):
continue
elif(var1[i][j]==99):
smalllist.append(0)
else:
if(int(var1[i][j]) == 0):
if(var1[i][j]>0):
var1[i][j] = 1
if (var1[i][j] < 0):
var1[i][j] = -1
if(var1[i][j] == 10):
var1[i][j] = 9
smalllist.append((int(var1[i][j])))
biglist.append( smalllist)
# if(i>90000):
# break
mtx = np.array(biglist)
mtx = np.array(mtx)
numberOfUser = mtx.shape[0]
numberOfItem = mtx.shape[1]
# print(mtx)
# print(mdx)
# print(numberOfUser,numberOfItem)
return numberOfUser, numberOfItem, mtx
if __name__ == "__main__":
readamazon2('./transratings.csv')