tensorflow--VocabularyProcessor

tensorflow--VocabularyProcessor
from tensorflow.contrib import learn
import numpy as np
max_document_length = 4
x_text =[
    'This is an apple',
    'No,it is a banana',
    'it is an orange'
]
vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length)
vocab_processor.fit(x_text)
print(next(vocab_processor.transform(['it is an'])).tolist())
x_index = np.array(list(vocab_processor.fit_transform(x_text)))
print(x_index)

输出结果为:

[6, 2, 3, 0]
[[1 2 3 4]
 [5 6 2 7]
 [6 2 3 9]]

    索引计数直接从x_text的第一行开始,所以This is an apple No it a banana orange对应的索引分别为1 2 3 4 5 6 7 8 9,故x_index输出结果为:

[[1 2 3 4]
 [5 6 2 7]
 [6 2 3 9]]

如果将vocab_processor.fix(x_text)注释,

from tensorflow.contrib import learn
import numpy as np
max_document_length = 4
x_text =[
    'This is an apple',
    'No,it is a banana',
    'it is an orange'
]
vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length)
#vocab_processor.fit(x_text)
print(next(vocab_processor.transform(['it is an'])).tolist())
x_index = np.array(list(vocab_processor.fit_transform(x_text)))
print(x_index)

得到结果:

[1, 2, 3, 0]
[[4 2 3 5]
 [6 1 2 7]
 [1 2 3 9]]

    索引计数从vocab_processor.transform(['it is an']) 开始,所以在返回值得第一行输出[1,2,3,0],对于x_text中的全部词的返回结果中This对应的索引为4,is对应的索引为2,an对应的索引为3,apple对应的索引为5,即对于所有的词索引对应于it is an This apple No a banana orange的索引值为1 2 3 4 5 6 7 8 9,所以x_index输出结果为:

[[4 2 3 5]
 [6 1 2 7]
 [1 2 3 9]]

最终返回的结果都是词在单词表中的索引值



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值