我已经在 python 中编写了我的自定义成对相似度函数,它给出了一个特征矩阵 X(包含特征行),找到并返回输出作为给定相似度度量的每个项目的 k 最近邻:
def print_pairwise_sim_for_graphlab(X,item_ids,metric,p,knn):
N = len(X)
SI = DI.squareform(DI.pdist(X,metric,p))
q = -1
Y = np.zeros((N*knn,4))
for i in range(0, N):
for k in range(1, knn+1):
q = q + 1
Y[q,0] = item_ids[i]
Y[q,1] = item_ids[np.argsort(SI[i,:])[-k]]
Y[q,2] = np.sort(SI[i,:])[-k]
Y[q,3] = k
return (Y)
我这样称呼它:
nn_SCD_min = print_pairwise_sim_for_graphlab(LL_features_SCD_min_np,item_ids,'minkowski',p,knn)
在哪里
LL_features_SCD_min_np
array(
[[-200, -48, -127, ..., 1, 0, 1],
[-199, -38, -127, ..., 0, 0, 1],
[-202, -60, -127, ..., 1, 0, 1],
...,
[-202, -60, -127, ..., 1, 0, 1],
[-198, 56, -120, ..., 1, 0, 1],
[-202, -85, -127, ..., 1, 0, 1]])
输出如下所示
nn_SCD_min =
array([[ 8.90000000e+01, 4.71460000e+04, 1.85300000e+03,
1.00000000e+00],
[ 8.90000000e+01, 8.11470000e+04, 1.84600000e+03,
2.00000000e+00],
[ 8.90000000e+01, 2.20700000e+03, 1.84600000e+03,
3.00000000e+00],
...,
[ 8.24630000e+04, 1.00000000e+03, 1.39300000e+03,
8.00000000e+00],
[ 8.24630000e+04, 5.98930000e+04, 1.39200000e+03,
9.00000000e+00],
[ 8.24630000e+04, 1.48900000e+03, 1.35000000e+03,
1.00000000e+01]])
在 Graphlab 中,我想将输出用作graphlab.recommender.item_similarity_recommender.create
.
我使用它如下:
m2 = gl.item_similarity_recommender.create(ratings_5K, nearest_items=nn_SCD_min)
我收到以下错误:
87 _get_metric_tracker().track(metric_name, value=1, properties=track_props, send_sys_info=False)
88
---> 89 raise ToolkitError(str(message))
ToolkitError: Option 'nearest_items' not recognized
我认为错误的主要原因是我nn_SCD_min
需要作为 SFrame 导入(这里看起来像一个数组)。nn_SCD_min
有四列。我相信这些列应该有如下标题:
item_id, similar, score, rank
如何将数组 'nn_SCD_min' 更改为SFrame
具有上述四个标题的数组?任何关于我采购这样做的想法都非常感谢。