recomendation_system.py 4.48 KB
Newer Older
DmitSPopov's avatar
DmitSPopov committed
1
2
3
4
5
import numpy as np
from scipy import sparse
from sklearn.preprocessing import normalize
from random import randint
from web_setting import db
DmitSPopov's avatar
DmitSPopov committed
6
from models.all_models import TagsUsers, TagsAdvertisement
DmitSPopov's avatar
DmitSPopov committed
7
8
9
from collections import Counter


10
def get_recomendation_by_tags(user_id):
DmitSPopov's avatar
DmitSPopov committed
11
12
13
14
15
16
17
    queryset = db.session.query(TagsUsers).filter(TagsUsers.user_id==user_id).all()
    all_tags = []
    for _ in queryset:
        all_tags.append(_.tags_id)

    all_tags.sort()

DmitSPopov's avatar
DmitSPopov committed
18
19
    user_data = dict(Counter(all_tags))

20
    from api.api_advertisment import all_advertisments
DmitSPopov's avatar
DmitSPopov committed
21

22
    all_data = dict()
DmitSPopov's avatar
DmitSPopov committed
23

24
25
26
27
28
    for _ in all_advertisments():
        tag_id = []
        for val in _['adv_id_tagsadv']:
            tag_id.append(val['connect_tags']['id'])
        all_data[_['id']] = tag_id
DmitSPopov's avatar
DmitSPopov committed
29

30
31
    result_id = calc_recomendation_by_tags(user_data, all_data)
    return result_id
DmitSPopov's avatar
DmitSPopov committed
32
33


DmitSPopov's avatar
DmitSPopov committed
34
input_distance_user = {1: [10, 200]}  # key - user_id, [x, y]
DmitSPopov's avatar
DmitSPopov committed
35
input_distance_data = {1: [20, 120], 2: [25, -100], 3: [10, 133], 4: [400, 300], 5: [144, 1322], 6: [122, -10],
DmitSPopov's avatar
DmitSPopov committed
36
                       7: [-120, 1432]}  # key - id_adv
DmitSPopov's avatar
DmitSPopov committed
37
38


39
def calc_recomendation_by_tags(user_data, all_data):
DmitSPopov's avatar
DmitSPopov committed
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
    row_column_index = []
    for i in range(len(user_data)):
        row_column_index.append(randint(0, 10))

    rows, rows_position = np.unique(row_column_index, return_inverse=True)
    cols, columns_position = np.unique(list(user_data.keys()), return_inverse=True)

    interaction_Sparse = sparse.csr_matrix((list(user_data.values()), (rows_position, columns_position)),
                                           shape=(len(all_data), len(all_data)))
    normalized_matrix = normalize(interaction_Sparse, norm="l2", axis=1)
    symmertic_matrix = normalized_matrix.T * normalized_matrix
    symmertic_matrix.todense()
    result = [all_data[i + 1] for i in symmertic_matrix[0].toarray().argsort()[0]]

    interaction_Sparse_transpose = interaction_Sparse.transpose(copy=True)
    normalized_matrix_transpose = normalize(interaction_Sparse_transpose, norm="l2", axis=1)

    symmertic_matrix_transpose = normalized_matrix * normalized_matrix_transpose * normalized_matrix

    result = [all_data[i + 1] for i in symmertic_matrix_transpose[0].toarray().argsort()[0]]

    result_id = []
    for i in range(len(result)):
        result_id.append(list(all_data.keys())[list(all_data.values()).index(result[i])])

    for x in result_id:
        if result_id.count(x) > 1:
            result_id.remove(x)
            for index in list(all_data.keys()):
                if index not in result_id:
                    result_id.insert(x, index)

    return result_id


DmitSPopov's avatar
DmitSPopov committed
75
def get_recomendation_by_location(user_data, all_data):
76
    tags_recomendation = get_recomendation_by_tags(data1)
DmitSPopov's avatar
DmitSPopov committed
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

    row_column_index = []
    for i in range(len(tags_recomendation)):
        row_column_index.append(randint(0, 10))

    destination = []
    for i in range(len(all_data)):
        destination.append(((list(all_data.values())[i][0] - list(user_data.values())[0][0]) ** 2 +
                            (list(all_data.values())[i][1] - list(user_data.values())[0][1]) ** 2) ** 0.5)

    priority = []
    for i in range(len(tags_recomendation)):
        priority.append(i / len(tags_recomendation))

    priority.sort(reverse=True)
    rows, rows_position = np.unique(row_column_index, return_inverse=True)
    cols, columns_position = np.unique(priority, return_inverse=True)

    interaction_Sparse = sparse.csr_matrix((tags_recomendation, (rows_position, columns_position)),
                                           shape=(len(destination), len(destination)))

    normalized_matrix = normalize(interaction_Sparse, norm="l2", axis=1)
    symmertic_matrix = normalized_matrix.T * normalized_matrix
    symmertic_matrix.todense()

    keys = list(all_data.keys())
    for key, i in zip(keys, range(len(all_data))):
        all_data[key] = destination[i]

    result = [all_data[i + 1] for i in symmertic_matrix[0].toarray().argsort()[0]]

    interaction_Sparse_transpose = interaction_Sparse.transpose(copy=True)
    normalized_matrix_transpose = normalize(interaction_Sparse_transpose, norm="l2", axis=1)

    symmertic_matrix_transpose = normalized_matrix * normalized_matrix_transpose * normalized_matrix

    result = [all_data[i + 1] for i in symmertic_matrix_transpose[0].toarray().argsort()[0]]

    result_id = []
    for i in range(len(result)):
        result_id.append(list(all_data.keys())[list(all_data.values()).index(result[i])])

    for x in result_id:
        if result_id.count(x) > 1:
            result_id.remove(x)

    return result_id