问题描述
我有一个数据框如下:框的形状是 (1510, 1399).列代表产品,行代表用户为给定产品分配的值(0 或 1).如何计算 jaccard_similarity_score?
I have a dataframe as follows: the shape of the frame is (1510, 1399). The columns represents products, the rows represents the values (0 or 1) assigned by an user for a given product. How can I can compute a jaccard_similarity_score?
我创建了一个占位符数据框,列出了产品与产品
I created a placeholder dataframe listing product vs. product
data_ibs = pd.DataFrame(index=data_g.columns,columns=data_g.columns)
我不知道如何迭代 data_ibs 来计算相似性.
I am not sure how to iterate though data_ibs to compute similarities.
for i in range(0,len(data_ibs.columns)) :
# Loop through the columns for each column
for j in range(0,len(data_ibs.columns)) :
.........
推荐答案
简短且矢量化(快速)的答案:
Short and vectorized (fast) answer:
使用来自 scikit learn 的成对距离的 'hamming':
Use 'hamming' from the pairwise distances of scikit learn:
from sklearn.metrics.pairwise import pairwise_distances
jac_sim = 1 - pairwise_distances(df.T, metric = "hamming")
# optionally convert it to a DataFrame
jac_sim = pd.DataFrame(jac_sim, index=df.columns, columns=df.columns)
说明:
Explanation:
假设这是您的数据集:
import pandas as pd
import numpy as np
np.random.seed(0)
df = pd.DataFrame(np.random.binomial(1, 0.5, size=(100, 5)), columns=list('ABCDE'))
print(df.head())
A B C D E
0 1 1 1 1 0
1 1 0 1 1 0
2 1 1 1 1 0
3 0 0 1 1 1
4 1 1 0 1 0
使用sklearn的jaccard_score,A列和B列的相似度为:
Using sklearn's jaccard_score, similarity between column A and B is:
from sklearn.metrics import jaccard_score
print(jaccard_score(df['A'], df['B']))
0.43
这是在总行数 100 中具有相同值的行数.
This is the number of rows that have the same value over total number of rows, 100.
据我所知,jaccard_score 没有成对版本,但有成对版本的距离.
As far as I know, there is no pairwise version of the jaccard_score but there are pairwise versions of distances.
然而,SciPy 定义了Jaccard 距离如下:
However, SciPy defines Jaccard distance as follows:
给定两个向量 u 和 v,Jaccard 距离是那些不一致的元素 u[i] 和 v[i] 在其中至少一个非零的情况下的比例.
因此它排除了两列都为 0 值的行.jaccard_score 没有.另一方面,汉明距离符合相似性定义:
So it excludes the rows where both columns have 0 values. jaccard_score doesn't. Hamming distance, on the other hand, is inline with the similarity definition:
这些向量元素在两个 n 向量 u 和 v 之间的比例哪个不同意.
所以如果要计算jaccard_score,可以使用1-hamming:
So if you want to calculate jaccard_score, you can use 1 - hamming:
from sklearn.metrics.pairwise import pairwise_distances
print(1 - pairwise_distances(df.T, metric = "hamming"))
array([[ 1. , 0.43, 0.61, 0.55, 0.46],
[ 0.43, 1. , 0.52, 0.56, 0.49],
[ 0.61, 0.52, 1. , 0.48, 0.53],
[ 0.55, 0.56, 0.48, 1. , 0.49],
[ 0.46, 0.49, 0.53, 0.49, 1. ]])
以数据帧格式:
jac_sim = 1 - pairwise_distances(df.T, metric = "hamming")
jac_sim = pd.DataFrame(jac_sim, index=df.columns, columns=df.columns)
# jac_sim = np.triu(jac_sim) to set the lower diagonal to zero
# jac_sim = np.tril(jac_sim) to set the upper diagonal to zero
A B C D E
A 1.00 0.43 0.61 0.55 0.46
B 0.43 1.00 0.52 0.56 0.49
C 0.61 0.52 1.00 0.48 0.53
D 0.55 0.56 0.48 1.00 0.49
E 0.46 0.49 0.53 0.49 1.00
您可以通过迭代列组合来执行相同的操作,但速度会慢得多.
You can do the same by iterating over combinations of columns but it will be much slower.
import itertools
sim_df = pd.DataFrame(np.ones((5, 5)), index=df.columns, columns=df.columns)
for col_pair in itertools.combinations(df.columns, 2):
sim_df.loc[col_pair] = sim_df.loc[tuple(reversed(col_pair))] = jaccard_score(df[col_pair[0]], df[col_pair[1]])
print(sim_df)
A B C D E
A 1.00 0.43 0.61 0.55 0.46
B 0.43 1.00 0.52 0.56 0.49
C 0.61 0.52 1.00 0.48 0.53
D 0.55 0.56 0.48 1.00 0.49
E 0.46 0.49 0.53 0.49 1.00
这篇关于如何从 pandas 数据帧计算 jaccard 相似度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!