首页 > 解决方案 > 如何在python中计算向量和矩阵之间的相似距离之前对数据进行归一化?

问题描述

我有一个用户的口味表:user_preference.head()

genres  Action Adventure Animation Children Comedy Crime Documentary Drama Fantasy Film-Noir Horror IMAX Musical Mys...
userId                                                                      
18         0       0         0        0       1      0        0        0      0        0        0     0     0       0   
65         9       4         0        4      12      8        1       15      6        4        0     0     0       2   
96         0      16        16        0      16      0        0        0     16        0        0     0    16       0   
121        8       0         0        0      69      9        0       21      0        0        0     0     0       0   
129       11      14         0        3      85      4        4       46      3        0        2     3     0      19   

我有电影的流派内容表:

genres  Action Adventure Animation Children Comedy Crime Documentary Drama Fantasy Film-Noir Horror IMAX Musical Mys...
movieId                                                                     
10         0       0         0        0       1      0        0        0      0        0        0     0     0       0   
11         0       0         0        0       1      1        0        1      0        1        0     0     0       0   
12         0       1         0        0       1      0        0        0      0        0        0     0     0       0   
13         0       0         0        0       0      0        0        1      0        0        0     0     0       0   
14         0       1         0        0       0      1        1        0      0        0        0     0     0       1   

我正在尝试获取每个用户的偏好向量并获取它与电影内容的相似性度量,以便通过采用点积来推荐最优惠的电影:

distances = np.dot(movie_content_df, user_preference[1].reshape(-1, 1))

为了计算距离,我首先通过以下方式对用户的偏好表进行归一化:

from sklearn.preprocessing import MinMaxScaler

user_preference_tmp = user_preference.copy()
norm = MinMaxScaler().fit(user_preference_tmp)
user_preference_norm = norm.transform(user_preference_tmp)

print(user_preference[1])
print(user_preference_norm[1])
________________________________________________________________________
([ 0,  9,  4,  0,  4, 12,  8,  1, 15,  6,  4,  0,  0,  0,  2,  7,  7,  10,  0,  0])
[0. 0.00239107 0.00131709 0. 0.00355872 0.00212352 0.00300639 0.00044287 0.001469 0.00358637 0.01520913 0. 0. 0. 0.00174978 0.00237691 0.0026616 0.00241604 0. 0. ]

我不明白为什么规范化后user_preference_norm[1]代表不同的偏好?例如,第三个和第五个值都是,4但在归一化后我得到0.001317090.00355872,或者最大值15转换为0.001469,在点积之后给出的相似度值较小。

在计算点积之前对数据进行规范化是否正确?如果是我做对了吗?

标签: pythonpandasnumpydata-science

解决方案


这给了我正确的输出,代表正确的用户偏好:

from sklearn.preprocessing import Normalizer
user_preference_tmp = user_preference.copy()

norm = Normalizer().fit(user_preference_tmp)
user_preference_norm = norm.transform(user_preference_tmp)

print(list(user_preference[1]))
print(user_preference_norm[1])
_______________________________________________________________________________
[0, 9, 4, 0, 4, 12, 8, 1, 15, 6, 4, 0, 0, 0, 2, 7, 7, 10, 0, 0]
[0.         0.31799936 0.14133305 0.         0.14133305 0.42399915
 0.2826661  0.03533326 0.52999894 0.21199958 0.14133305 0.
 0.         0.         0.07066653 0.24733284 0.24733284 0.35333263
 0.         0.        ]


推荐阅读