-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
76 lines (58 loc) · 2.46 KB
/
main.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
#!/bin/python3
import re
from classes.jaccard_index import JaccardIndex
if __name__ == '__main__':
products = set()
purchase_history = dict()
with open('data.txt', 'r', encoding='UTF-8') as file:
data = file.read().splitlines(True)[1:]
for line in data:
format_line = re.split(r'\t+', line)
purchase_history[format_line[0]] = set()
products.add(format_line[1])
for line in data:
format_line = re.split(r'\t+', line)
purchase_history[format_line[0]].add(format_line[1])
# Assignment 1
max_jaccard_index_value = 0
for first_name in purchase_history:
for second_name in purchase_history:
if first_name != second_name:
jaccard_index_value = JaccardIndex(
purchase_history[first_name],
purchase_history[second_name]
).get_value()
if jaccard_index_value > max_jaccard_index_value:
max_jaccard_index_value = jaccard_index_value
max_jaccard_index_pattern = (first_name, second_name)
print('The {} and {} has the highest Jaccard Index value is {}.'.format(
max_jaccard_index_pattern[0],
max_jaccard_index_pattern[1],
max_jaccard_index_value)
)
# Assignment 2
andrew_recommendation_indexes = dict()
products_not_in_andrew_history = products - purchase_history['andrew']
for product in products_not_in_andrew_history:
product_exclude_history = set()
andrew_recommendation_indexes[product] = 0
for shopper in purchase_history:
if (shopper != 'andrew'
and product not in purchase_history[shopper]):
product_exclude_history.add(shopper)
for shopper in product_exclude_history:
andrew_recommendation_indexes[product] += JaccardIndex(
purchase_history['andrew'],
purchase_history[shopper]
).get_value()
andrew_recommendation_indexes[product] /= len(product_exclude_history)
sorted_andrew_recommendation_indexes = sorted(
andrew_recommendation_indexes.items(),
key=lambda dic: dic[1],
reverse=True
)
print('The products I want to recommend to andrew are: {}, {}, {}.'.format(
sorted_andrew_recommendation_indexes[0][0],
sorted_andrew_recommendation_indexes[1][0],
sorted_andrew_recommendation_indexes[2][0])
)