我如何根据以下代码创建相似度矩阵?

How do i create a similarity matrix based on the below code?

我正在尝试使用此 link https://sourceforge.net/projects/gower-distance-4python/files/ 中的 gower 函数。我正在尝试将它应用于我的分类变量数据框。但是我可以看到,当我使用 gower_distances 函数时,我的对角线中有一些非零值(我需要它们都为 0)。

我一直在尝试调试代码。我想我知道这是在哪里发生的,它发生在 _gower_distance_row 函数中。有这行代码我不明白 sij_cat = np.where(xi_cat == xj_cat,np.zeros_like(xi_cat), np.ones_like(xi_cat))。但我会以更容易理解的形式呈现它。

假设我有:

xi=np.array(['cat','dog','monkey'])
xj=np.array([['cat','dog','monkey'],['horse','dog','hairy']])
sij_cat = np.where(xi == xj,np.zeros_like(xi),np.ones_like(xi))

我的结果是这样的:

array([['', '', ''],
       ['1', '', '1']], dtype='<U6') 

因为我正在比较 cat 和 cat 我想分配零,以及它不同的地方,例如cat vs horse and monkey vs hairy 它应该是 1。我不明白为什么在上面的结果中我得到''?我想要这里的零。我该如何解决这个问题?

np.logical_not(xi == xj).astype(int)

输出将是:

array([[0, 0, 0],
       [1, 0, 1]])

说明: np.logical_notTrue 更改为 False,将 False 更改为 True,将 astype(int) 更改为 01