简体   繁体   English

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

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

I'm trying to use the gower function from this link https://sourceforge.net/projects/gower-distance-4python/files/ . 我正在尝试通过此链接https://sourceforge.net/projects/gower-distance-4python/files/使用gower函数。 I'm trying to apply it to my dataframe of categorical variables. 我正在尝试将其应用于分类变量数据框。 However I can see that when i use the gower_distances function i have some non-zero values in my diagonals ( i need them to all be 0). 但是我可以看到,当我使用gower_distances函数时,对角线中有一些非零值(我需要将它们全部设为0)。

I've been trying to de-bug the code. 我一直在尝试调试代码。 I think i know where this is happening and it's occuring in the _gower_distance_row function. 我想我知道这是在哪里发生的,它正在_gower_distance_row函数中发生。 There is this line of code which i don;t understand sij_cat = np.where(xi_cat == xj_cat,np.zeros_like(xi_cat),np.ones_like(xi_cat)). 这是我不了解的代码行; sij_cat = np.where(xi_cat == xj_cat,np.zeros_like(xi_cat),np.ones_like(xi_cat))。 But i will present it in a easier format to understand. 但是我将以一种更易于理解的格式呈现它。

Say i have: 说我有:

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))

I get this as my result: 我得到这个作为我的结果:

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

since i am comparing cat with cat i want to assign zero, and where it is different eg cat vs horse and monkey vs hairy it should be 1. I don't get why in the above result i am getting ''? 因为我要比较猫和猫,所以我想指定零,并且在不同的地方(例如猫与马,猴子与毛茸茸的)应该为1。 i want zeroes here. 我想要零。 How do i fix this? 我该如何解决?

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

output will be: 输出将是:

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

explanation: np.logical_not changes True to False and False to True and astype(int) changes to 0 and 1 说明: np.logical_notTrue更改为False ,将False更改为True并且astype(int)更改为01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用以下代码构建cramer-v矩阵? - How do i build a cramer-v matrix using the below code? 如何创建下面的递归 function? - How do I create a recursive function below? 如何使用python pandas为以下代码片段以矩阵形式创建输出 - How to create a output in matrix form for the below code snippet using python pandas 如何解决 python 中的 KeyError? 检查下面的代码 - How do i resolve a KeyError in python? check the code below 如何创建基于资源的策略? - How do I create a resource based policy? 如何使用 python 创建幻方矩阵 - How do I create a magic square matrix using python 如何为矩阵中的行/列创建标题? - How do I create header for rows/columns in a matrix? 当我将listing_id输入到下面数据的函数中时,如何创建一个返回纬度的函数? - How do I create a function that returns the latitude when I input the listing_id into the function for the data below? 如何减少以下代码? 我不想放置“ for循环”,并希望接受输入作为整数列表 - How can I reduce the below code? I do not want to put the 'for loop' and want to accept input as a list of integers 如何将以下内容定义为函数? 因为我需要使用相同的代码4次 - How do I define the below into a function? As I need to use the same code 4 times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM