I'm writing an implementation of k-nearest neighbors and in the code is the following function:
def k_predict(k, X_train, user_num, joke_num):
# Distances defined by (user, distance) pairs
distances = []
for idx, rating_vec in enumerate(X_train):
if idx != user_num:
dist = np.linalg.norm(X_train[user_num], rating_vec)
distances.append((idx, dist))
retu distances
(Basically, users are the data points and joke responses are the features.)
When ruing this code, I'm getting the following error:
Traceback (most recent call last):
File "joke_recommender_k.py", line 40, in <module>
prediction = k_predict(K, X_train, user_num, joke_num)
File "joke_recommender_k.py", line 12, in k_predict
dist = np.linalg.norm(X_train[user_num], rating_vec)
File "/Users/ryanpeden/anaconda/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 2120, in norm
(ord == 2 and ndim == 1)):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I've gotten this error before when comparing an array to a boolean, but that's not what I'm doing here. I've traced it down, and the error is coming from the line
dist = np.linalg.norm(X_train[user_num], rating_vec)
Here, X_train[user_num] and rating_vec are just 100-dimensional numpy arrays, so I don't see what the problem is.
Can anyone help??
برچسب:
نویسنده: استخدام کار