【发布时间】:2018-11-14 19:42:09
【问题描述】:
我有一个 pandas 数据框,其中包含具有各自葡萄酒属性的葡萄酒列表。
然后我创建了一个新的列向量,其中包含来自这些属性的 numpy 向量。
def get_wine_profile(id):
wine = wines[wines['exclusiviId'] == id]
wine_vector = np.array(wine[wine_attrs].values.tolist()).flatten()
return wine_vector
wines['vector'] = wines.exclusiviId.apply(get_wine_profile)
因此向量列看起来像这样
vector
[1, 1, 1, 2, 2, 2, 2, 1, 1, 1]
[3, 1, 2, 1, 2, 2, 2, 0, 1, 3]
[1, 1, 2, 1, 3, 3, 3, 0, 1, 1]
.
.
现在我想在该列和另一个向量之间执行余弦相似度,该向量是用户输入的结果向量 这是我迄今为止尝试过的
from scipy.spatial.distance import cosine
cos_vec = wines.apply(lambda x: (1-cosine(wines["vector"],[1, 1, 1, 2, 2, 2, 2, 1, 1, 1]), axis=1)
Print(cos_vec)
这是抛出错误
ValueError: ('operands could not be broadcast together with shapes (63,) (10,) ', 'occurred at index 0')
我也尝试使用 sklearn,但它也有与 arrar 形状相同的问题
我想要的最终输出是在该列和用户输入之间具有匹配分数的列
【问题讨论】:
标签: python python-3.x numpy scikit-learn array-broadcasting