【发布时间】:2017-12-10 08:39:24
【问题描述】:
我正在尝试从我的 TF_norm 矩阵和 IDF 向量创建 TF-IDF。我知道它们的尺寸不同,所以我不知道如何将两者相乘。我是否需要使用 TF_norm 矩阵添加减少内容或转换 IDF 向量?从这里完全迷失了。
#c) Normalized term frequency
count=0
total=lexicon_dim
matrix_TF_norm=[[0 for c in range(lexicon_dim)] for r in range(4)]
for c in lexicon:
matrix_TF_norm[0][count]=c
matrix_TF_norm[1][count]=hamlet_tok_norm_stop_stem.count(c)/total
matrix_TF_norm[2][count]=macbeth_tok_norm_stop_stem.count(c)/total
matrix_TF_norm[3][count]=pinocchio_tok_norm_stop_stem.count(c)/total
count=count+1
print(matrix_TF_norm)
#d) TF-IDF
vector_idf=[] #initialize IDF vector
for i in range(lexicon_dim): #run through loop for each token in lexicon
df=0
if matrix_binary[1][i]==1: #[1] = doc1
df=df+1
if matrix_binary[2][i]==1:
df=df+1
if matrix_binary[3][i]==1:
df=df+1
#add them together
idf=math.log(3/df)
vector_idf.append(idf)
print(vector_idf)
import numpy as np
vector_idf=np.diag(vector_idf)
tf_idf=np.cross(vector_idf,matrix_TF_norm)
【问题讨论】:
标签: python python-3.x tf-idf