apachecn / nlp-pytorch-zh

《Natural Language Processing with PyTorch》中文翻译
https://nlp2pytorch.apachecn.org
Other
717 stars 184 forks source link

The code in the chapter #24

Open nininininini opened 4 years ago

nininininini commented 4 years ago

Does there some code issue in the chapter1? such as Example 1-1. The following code in the example 1-1, xticklabels = vocab, I can't find the vocab was stated in the previous text. and if run this code, can't show any image. It likes the code loss import the matplotlib and should add the code plt.show().

from sklearn.feature_extraction.text import CountVectorizer import seaborn as sns corpus = ['Time flies flies like an arrow.', 'Fruit flies like a banana.'] one_hot_vectorizer = CountVectorizer(binary=True) one_hot = one_hot_vectorizer.fit_transform(corpus).toarray() sns.heatmap(one_hot, annot=True, cbar=False, xticklabels=vocab, yticklabels=['Sentence 2'])

jiangzhonglian commented 4 years ago

谢谢提醒,可以帮忙提交一下 pull request 一起来完善吗?

ferquintana84 commented 4 years ago

any response? same problem here

yu-cheng-kuo-28 commented 4 years ago

Hi there, I reckon this is the only way:

=> BOOK: NLP with PyTorch

from sklearn.feature_extraction.text import CountVectorizer import seaborn as sns

corpus = ['Time flies flies like an arrow.', 'Fruit flies like an banana.'] print(one_hotvectorizer.vocabulary) => {'time': 6, 'flies': 3, 'like': 5, 'an': 0, 'arrow': 1, 'fruit': 4, 'banana': 2}

print(sorted(one_hotvectorizer.vocabulary)) => ['an', 'arrow', 'banana', 'flies', 'fruit', 'like', 'time'] vocab = sorted(one_hotvectorizer.vocabulary)

one_hot_vectorizer = CountVectorizer(binary=True) one_hot = one_hot_vectorizer.fit_transform(corpus).toarray()

sns.heatmap(one_hot, annot=True, cbar=False, xticklabels= vocab, yticklabels= ['Sentence_1','Sentence_2']);

BTW, you can also do things like this to pick up the words you want

vocab_2 = sorted(one_hotvectorizer.vocabulary)[1:5] sns.heatmap(one_hot[:,1:5], annot=True, cbar=False, xticklabels= vocab_2, yticklabels= ['Sentence_1','Sentence_2']);