-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimilarity-recommender.py
93 lines (81 loc) · 3.03 KB
/
similarity-recommender.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
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
def path_to_vector(path, metadata, all_paths, all_metadata_keys):
vector = np.zeros(len(all_paths) + len(all_metadata_keys))
for p in path:
if p in all_paths:
vector[all_paths.index(p)] = 1
for key, value in metadata.items():
if key in all_metadata_keys:
vector[len(all_paths) + all_metadata_keys.index(key)] = value
return vector
def recommend_next_step(historical_data, new_path, new_metadata):
all_paths = list(
set(path for history in historical_data for path in history["path"])
)
all_metadata_keys = list(
set(key for history in historical_data for key in history["metadata"])
)
# print(all_paths)
# print([all_paths.index(p) for p in all_paths])
# print(all_metadata_keys)
historical_vectors = [
path_to_vector(h["path"], h["metadata"], all_paths, all_metadata_keys)
for h in historical_data
]
new_vector = path_to_vector(new_path, new_metadata, all_paths, all_metadata_keys)
# print(historical_vectors)
# print(new_vector)
similarities = cosine_similarity([new_vector], historical_vectors)[0]
most_similar_idx = np.argmax(similarities)
potential_next_steps = set(historical_data[most_similar_idx]["path"]) - set(
new_path
)
if potential_next_steps:
return list(potential_next_steps)[0]
return None
# Example usage
historical_data = [
{
"path": ["/path/to/1", "/path/to/2", "/path/to/5"],
"metadata": {"added_to_cart": 1, "checkout": 1},
},
{
"path": ["/path/to/2", "/path/to/1", "/path/to/5"],
"metadata": {"added_to_cart": 1, "checkout": 0},
},
{
"path": ["/path/to/3", "/path/to/1", "/path/to/5"],
"metadata": {"added_to_cart": 0, "checkout": 1},
},
{
"path": ["/path/to/3", "/path/to/1", "/path/to/4"],
"metadata": {"added_to_cart": 0, "checkout": 1},
},
{
"path": ["/path/to/3", "/path/to/1", "/path/to/12"],
"metadata": {"added_to_cart": 0, "checkout": 0},
},
{
"path": ["/path/to/4", "/path/to/11", "/path/to/16"],
"metadata": {"added_to_cart": 0, "checkout": 0},
},
{
"path": ["/path/to/6", "/path/to/8", "/path/to/16"],
"metadata": {"added_to_cart": 0, "checkout": 0},
},
{
"path": ["/path/to/4", "/path/to/8", "/path/to/16"],
"metadata": {"added_to_cart": 0, "checkout": 0},
},
{
"path": ["/path/to/11", "/path/to/6", "/path/to/16"],
"metadata": {"added_to_cart": 0, "checkout": 0},
},
]
new_path = ["/path/to/2", "/path/to/1"] # Should predict /path/to/5 as next
# new_path = ["/path/to/4", "/path/to/6"] # Should predict /path/to/11 as next
# new_path = ["/path/to/4", "/path/to/8"] # Should predict /path/to/16 as next
new_metadata = {"added_to_cart": 1, "checkout": 0}
recommendation = recommend_next_step(historical_data, new_path, new_metadata)
print(f"Recommended next path: {recommendation}")