[英]Compute co-occurrence matrix by counting values in cells
I have a dataframe like this我有一个这样的数据框
df = pd.DataFrame({'a' : [1,1,0,0], 'b': [0,1,1,0], 'c': [0,0,1,1]})
I want to get我想得到
a b c
a 2 1 0
b 1 2 1
c 0 1 2
where a,b,c are column names, and I get the values counting '1' in all columns when the filter is '1' in another column.其中 a,b,c 是列名,当过滤器在另一列中为 '1' 时,我在所有列中得到计数为 '1' 的值。 For ample, when df.a == 1, we count a = 2, b =1, c = 0 etc例如,当 df.a == 1 时,我们计算 a = 2, b =1, c = 0 等
I made a loop to solve我做了一个循环来解决
matrix = []
for name, values in df.iteritems():
matrix.append(pd.DataFrame( df.groupby(name, as_index=False).apply(lambda x: x[x == 1].count())).values.tolist()[1])
pd.DataFrame(matrix)
But I think that there is a simpler solution, isn't it?但我认为有一个更简单的解决方案,不是吗?
You appear to want the matrix product, so leverage DataFrame.dot
:您似乎想要矩阵产品,因此请利用DataFrame.dot
:
df.T.dot(df)
a b c
a 2 1 0
b 1 2 1
c 0 1 2
Alternatively, if you want the same level of performance without the overhead of pandas, you could compute the product with np.dot
:或者,如果您想要相同级别的性能而没有np.dot
的开销,您可以使用np.dot
计算乘积:
v = df.values
pd.DataFrame(v.T.dot(v), index=df.columns, columns=df.columns)
Or, if you want to get cute,或者,如果你想变得可爱,
(lambda a, c: pd.DataFrame(a.T.dot(a), c, c))(df.values, df.columns)
a b c
a 2 1 0
b 1 2 1
c 0 1 2
np.einsum
Not as pretty as df.T.dot(df)
but how often do you see np.einsum
amirite?不如df.T.dot(df)
漂亮,但你np.einsum
看到np.einsum
amirite ?
pd.DataFrame(np.einsum('ij,ik->jk', df, df), df.columns, df.columns)
a b c
a 2 1 0
b 1 2 1
c 0 1 2
您可以使用@
运算符对 numpy 数组进行乘法运算。
df = pd.DataFrame(df.values.T @ df.values, df.columns, df.columns)
Numpy matmul
Numpy matmul
np.matmul(df.values.T,df.values)
Out[87]:
array([[2, 1, 0],
[1, 2, 1],
[0, 1, 2]], dtype=int64)
#pd.DataFrame(np.matmul(df.values.T,df.values), df.columns, df.columns)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.