forked from X-XG/WebInfo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:X-XG/WebInfo
- Loading branch information
Showing
7 changed files
with
23,748 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import sys | ||
sys.path.append('..') | ||
from utils.getMaps import getUserMap | ||
from utils.JacoobiSim import JaccobiSim | ||
sys.path.append('UserBase') | ||
import numpy as np | ||
|
||
UserNum = 23599 | ||
data_path = '../../data/DoubanMusic.txt' | ||
output_path = '../../output/' | ||
|
||
def MatSimGen(mode = 1): | ||
UserMap = getUserMap() | ||
|
||
MatSim = np.zeros((UserNum, UserNum)) | ||
|
||
num = 0 | ||
for User1 in UserMap: | ||
num += 1 | ||
if num %10 == 0: | ||
print(num) | ||
for User2 in UserMap: | ||
MatSim[User1][User2] = JaccobiSim(UserMap[User1], UserMap[User2], mode) | ||
np.save('MatJaccobiSim'+str(mode) +'.npy', MatSim) | ||
|
||
if __name__ == '__main__': | ||
MatSimGen(mode = 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import sys | ||
sys.path.append('..') | ||
from utils.getMaps import getUserMap, getMusicMap | ||
sys.path.append('UserBase') | ||
import numpy as np | ||
|
||
MatSimPath = 'MatJaccobiSim1.npy' | ||
data_path = '../../data/DoubanMusic.txt' | ||
result_path = 'UserBaseMode1.txt' | ||
MusicNum = 21602 | ||
UserNum = 23599 | ||
|
||
def main(CarryOn = False): | ||
MatSim = np.load(MatSimPath) | ||
|
||
UserMap = getUserMap() | ||
|
||
MusicMap = getMusicMap() | ||
|
||
if CarryOn: | ||
f = open(result_path, 'r') | ||
count = len(f.readlines()) | ||
f.close() | ||
f = open(result_path, 'a') | ||
else: | ||
f = open(result_path, 'w') | ||
count = 0 | ||
|
||
for UserID in UserMap: | ||
if UserID < count: | ||
continue | ||
predict_list = [] | ||
for MusicID in range(MusicNum): | ||
if MusicID in UserMap[UserID]: | ||
continue | ||
predict = 0 | ||
for OtherUser in MusicMap[MusicID]: | ||
predict += MatSim[UserID][OtherUser] | ||
predict_list.append((predict,MusicID)) | ||
predict_list.sort(reverse=True) | ||
f.write(str(UserID)) | ||
f.write('\t') | ||
f.write(str(predict_list[0][1])) | ||
for i in range(1,100): | ||
f.write(',') | ||
f.write(str(predict_list[i][1])) | ||
f.write('\n') | ||
count += 1 | ||
if count % 1 == 0: | ||
print(count) | ||
|
||
if __name__ == '__main__': | ||
main(CarryOn=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from math import sqrt | ||
|
||
def JaccobiSim(list1:list, list2:list, mode = 1): | ||
intersection = len(set(list1).intersection(set(list2))) | ||
union = len(list1) + len(list2) - intersection | ||
if mode == 0: | ||
return intersection | ||
elif mode == 0.5: | ||
return intersection/sqrt(union) | ||
elif mode == 1: | ||
return intersection/union | ||
else: | ||
exit(-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
def getUserMap(data_path = '../../data/DoubanMusic.txt'): | ||
UserMap = {} | ||
|
||
f = open(data_path, 'r') | ||
lines = f.readlines() | ||
f.close() | ||
|
||
for line in lines: | ||
temp = line.split() | ||
UserID = int(temp[0]) | ||
for pair in temp[1:]: | ||
MusicID = int(pair.split(',')[0]) | ||
if UserID not in UserMap: | ||
UserMap[UserID] = [MusicID] | ||
else: | ||
UserMap[UserID].append(MusicID) | ||
return UserMap | ||
|
||
def getMusicMap(data_path = '../../data/DoubanMusic.txt'): | ||
MapMusicID = {} | ||
|
||
f = open(data_path, 'r') | ||
lines = f.readlines() | ||
f.close() | ||
|
||
for line in lines: | ||
temp = line.split() | ||
UserID = int(temp[0]) | ||
for pair in temp[1:]: | ||
MusicID = int(pair.split(',')[0]) | ||
if MusicID not in MapMusicID: | ||
MapMusicID[MusicID] = [UserID] | ||
else: | ||
MapMusicID[MusicID].append(UserID) | ||
return MapMusicID |
Oops, something went wrong.