我运行python manage.py runserver
时一切正常。但是,当我单击“建议”时,会看到此错误。为什么?
def get_suggestions(request):
num_reviews = Review.objects.count()
all_user_names = list(map(lambda x: x.id, User.objects.only("id")))
all_movie_ids = set(map(lambda x: x.movie.id, Review.objects.only("movie")))
num_users = len(list(all_user_names))
movieRatings_m = sp.sparse.dok_matrix((num_users, max(all_movie_ids)+1), dtype=np.float32)
for i in range(num_users):
user_reviews = Review.objects.filter(user_id=all_user_names[i])
for user_review in user_reviews:
movieRatings_m[i,user_review.movie.id] = user_review.rating
movieRatings = movieRatings_m.transpose()
coo = movieRatings.tocoo(copy=False)
df = pd.DataFrame({'movies': coo.row, 'users': coo.col, 'rating': coo.data}
)[['movies', 'users', 'rating']].sort_values(['movies', 'users']
).reset_index(drop=True)
mo = df.pivot_table(index=['movies'], columns=['users'], values='rating')
mo.replace({np.nan: 0}, regex=True, inplace=True)
model_knn = NearestNeighbors(algorithm='brute', metric='cosine', n_neighbors=7)
model_knn.fit(mo.values)
distances, indices = model_knn.kneighbors(mo.iloc[100:].values, return_distance=True)
context = list(map(lambda x: Movie.objects.get(id=indices.flatten()[x]), range(0, len(distances.flatten()))))
return render(request, 'get_suggestions.html', {'context': context})
我看到此错误。由于此代码
"model_knn.fit(mo.values)"
"raise ValueError(msg_err.format(type_err, X.dtype))
ValueError: Input contains NaN, infinity or a value too large for dtype('float32')."
我该如何解决?