基于Pandas DataFrame矩阵的计算

时间:2014-02-03 07:37:27

标签: python pandas dataframe

我有一个Pandas DataFrame如下。它显示了用户在每个会话中如何访问页面p1到p4。

df = pd.DataFrame([[1,1,1,0,1],[2,1,1,0,1],[3,1,1,1,1],[4,0,1,0,1]])
df.columns = ['session','p1','p2','p3','p4']

以下是显示公共访问页面交集的矩阵。

In [20]: df.dot(df.T)
Out[20]: 
session  1  2  3  4
session            
1        3  3  3  2
2        3  3  3  2
3        3  3  4  2
4        2  2  2  2

我需要根据以下公式计算一个值。

s1 = No of pages accessed in common/(total no of pages in si*total no of pages in sj)^(1/2)

那是针对第1和第2会话

No of pages accessed in common = 3
total no of pages in s1*total no of pages in s2 = 3*3
s1 = 3/9^(1/2) = 1
会议2和4的

No of pages accessed in common = 2
total no of pages in s1*total no of pages in s2 = 3*2
s1 = 2/6^(1/2) = 0.8164

无法实现这一点。

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找numpy.outer

In [10]: df1 = df.set_index('session')
         common = df1.dot(df1.T)

In [11]: df1.sum(1)
Out[11]: 
session
1          3
2          3
3          4
4          2
dtype: int64

In [12]: np.outer(*[df1.sum(1)] * 2)  # same as np.outer(df1.sum(1), df1.sum(1))
Out[12]: 
array([[ 9,  9, 12,  6],
       [ 9,  9, 12,  6],
       [12, 12, 16,  8],
       [ 6,  6,  8,  4]])

In [13]: np.sqrt(np.outer(*[df1.sum(1)] * 2))
Out[13]: 
array([[ 3.        ,  3.        ,  3.46410162,  2.44948974],
       [ 3.        ,  3.        ,  3.46410162,  2.44948974],
       [ 3.46410162,  3.46410162,  4.        ,  2.82842712],
       [ 2.44948974,  2.44948974,  2.82842712,  2.        ]])

In [14]: common / np.sqrt(np.outer(*[df1.sum(1)] * 2))
Out[14]: 
session         1         2         3         4
session                                        
1        1.000000  1.000000  0.866025  0.816497
2        1.000000  1.000000  0.866025  0.816497
3        0.866025  0.866025  1.000000  0.707107
4        0.816497  0.816497  0.707107  1.000000