python 共现矩阵_Python词组共现矩阵

你可以试试下面的代码。在import collections, numpy

tokens=['He','is','not','lazy','intelligent','smart']

j=0

a=np.zeros((len(tokens),len(tokens)))

for pos,token in enumerate(tokens):

j+=pos+1

for token1 in tokens[pos+1:]:

count = 0

for sentence in [['He','is','not','lazy','He','is','intelligent','He','is','smart'] ]:

occurrences1 = [i for i,e in enumerate(sentence) if e == token1]

#print(token1,occurrences1)

occurrences2 = [i for i,e in enumerate(sentence) if e == token]

#print(token,occurrences2)

new1= np.repeat(occurrences1,len(occurrences2))

new2= np.asarray(occurrences2*len(occurrences1))

final_new= np.subtract(new1,new2)

final_abs_diff = np.absolute(final_new)

final_counts = collections.Counter(final_abs_diff)

count_1=final_counts[1]

count_2=final_counts[2]

count_0=final_counts[0]

count=count_1+count_2+count_0

a[pos][j]=count

#print(token,' ',pos,' ',token1,' ',j,' ',count)

j+=1

j=0

final_mat = a.T+a

print(final_mat)

输出为:

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值