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}$
總結
以上是生活随笔為你收集整理的python 共现矩阵_Python词组共现矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拍卖源码java_Java并发的AQS原
- 下一篇: python多重循环导致内存不足_Pyt