I have written my custom pairwise similarity function in python which given a matrix of features X (contains rows of features), find and returns the output as k nearest neighbor to each item given a similarity metric:
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)
I call it like this:
nn_SCD_min = print_pairwise_sim_for_graphlab(LL_features_SCD_min_np,item_ids,'minkowski',p,knn)
where
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]])
The output looks like this following
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]])
In Graphlab, I want to use the output as the input for graphlab.recommender.item_similarity_recommender.create
.
I use it as following:
m2 = gl.item_similarity_recommender.create(ratings_5K, nearest_items=nn_SCD_min)
and I get the following error:
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
I think the main reason for error is that my nn_SCD_min
needs to be imported as SFrame (it looks like an array here). nn_SCD_min
has FOUR columns. I believe the columns should have headers as following headers:
item_id, similar, score, rank
How can I change the array 'nn_SCD_min' to an SFrame
with the above four headers? Any idea about my procure to do this is greatly appreciated.