如何将数组更改为Graphrame的GraphLab的ItemSimilarityRecommend

时间:2016-03-27 19:25:04

标签: python graphlab

我已经在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?任何关于我采取这样做的想法都非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以直接从numpy数组创建SFrame。它将具有单列数组类型。然后,您可以unpack进入四列SFrame。

>>> nearest_items = gl.SFrame(nn_SCD_min)
>>> nearest_items = nearest_items.unpack('X1', '')\
                                 .rename({'0': 'item_id', 
                                          '1': 'similar', 
                                          '2': 'score', 
                                          '3': 'rank'})

>>> nearest_items
Columns:
    item_id float
    similar float
    score   float
    rank    float

Rows: 6

Data:
+---------+---------+--------+------+
| item_id | similar | score  | rank |
+---------+---------+--------+------+
|   89.0  | 47146.0 | 1853.0 | 1.0  |
|   89.0  | 81147.0 | 1846.0 | 2.0  |
|   89.0  |  2207.0 | 1846.0 | 3.0  |
| 82463.0 |  1000.0 | 1393.0 | 8.0  |
| 82463.0 | 59893.0 | 1392.0 | 9.0  |
| 82463.0 |  1489.0 | 1350.0 | 10.0 |
+---------+---------+--------+------+