ValueError: qk и pk должны иметь одинаковую форму - scipy.spatial.distance.jensenshannon - PullRequest
0 голосов
/ 01 мая 2020

Я вызываю функцию jensen_shannon (запрос, матрица) ниже, чтобы найти наиболее похожие документы запроса документа в матрице документов

def jensen_shannon(query, matrix):
"""
This function implements a Jensen-Shannon similarity
between the input query (an LDA topic distribution for a document)
and the entire corpus of topic distributions.
It returns an array of length M where M is the number of documents in the corpus
"""
# lets keep with the p,q notation above
p = query[None,:].T # take transpose
q = matrix.T # transpose matrix
m = 0.5*(p + q)
return np.sqrt(0.5*(entropy(p,m) + entropy(q,m)))

Форма запроса: (100,)

Форма матрицы c: (10804, 100)

Отслеживание ошибок:

ValueError                                Traceback (most recent call last)
<ipython-input-103-86cb68dd862d> in <module>
      1 # this is surprisingly fast
----> 2 most_sim_ids = get_most_similar_documents(new_doc_distribution,doc_topic_dist)

<ipython-input-102-c0fb95224e87> in get_most_similar_documents(query, matrix, k)
      6     print(query.shape)
      7     print(matrix.shape)
----> 8     sims = jensen_shannon(query,matrix) # list of jensen shannon distances
      9     return sims.argsort()[:k] # the top k positional index of the smallest Jensen Shannon distances

<ipython-input-74-6ffb0ec54e9a> in jensen_shannon(query, matrix)
     10     q = matrix.T # transpose matrix
     11     m = 0.5*(p + q)
---> 12     return np.sqrt(0.5*(entropy(p,m) + entropy(q,m)))

~/venv/lib/python3.6/site-packages/scipy/stats/_distn_infrastructure.py in entropy(pk, qk, base, axis)
   2668         qk = asarray(qk)
   2669         if qk.shape != pk.shape:
-> 2670             raise ValueError("qk and pk must have same shape.")
   2671         qk = 1.0*qk / np.sum(qk, axis=axis, keepdims=True)
   2672         vec = rel_entr(pk, qk)

ValueError: qk and pk must have same shape.

Добавить параметр оси для scipy.spatial.distance.jensenshannon , но это не не принимаю параметры оси в функции.

Кто-нибудь знает, что мне не хватает? Любое руководство высоко ценится. Спасибо.

К вашему сведению: я пытаюсь использовать этот код kaggle https://www.kaggle.com/ktattan/lda-and-document-similarity/data

...