我在试着理解这里的代码。你知道吗
f = open('/Users/nk/Vocab.txt','r')
vocab_temp = f.read().split()
f.close()
col = len(vocab_temp)
print("Training column size:")
print(col)
row = run('cat '+'/Users/nk/X_train.txt'+" | wc -l").split()[0]
print("Training row size:")
print(row)
matrix_tmp = np.zeros((int(row),col), dtype=np.int64)
print("Train Matrix size:")
print(matrix_tmp.size)
label_tmp = np.zeros((int(row)), dtype=np.int64)
f = open('/Users/nk/X_train.txt','r')
count = 0
for line in f:
line_tmp = line.split()
#print(line_tmp)
for word in line_tmp[0:]:
if word not in vocab_temp:
continue
matrix_tmp[count][vocab_temp.index(word)] = 1
count = count + 1
f.close()
我知道col
基本上是词汇表中的单词,row
是训练集中的文本数据。
我还了解到,在loop
中,代码实际上是在检查词汇表中是否存在一个单词,是否存在于训练集中。有人能给我解释一下continue
后面的那一行是什么意思吗?你知道吗
matrix_tmp[count][vocab_temp.index(word)] = 1
如果查看代码,count
每行递增1。所以matrix_tmp[count]
是每行的单词向量。你知道吗现在,考虑到
vocab_temp.index(word)
,您可以在第二行中看到vocab_temp
保留了由f.read().split()
产生的向量。你知道吗事实上,它从
vocab_temp
中获取索引,实际上它获取了矩阵的位置(矩阵中单词word
所在的索引,并将其设置为1(单词word
出现在index
位置)。你知道吗相关问题 更多 >
编程相关推荐