nlp-pytorch-zh icon indicating copy to clipboard operation
nlp-pytorch-zh copied to clipboard

The code in the chapter

Open nininininini opened this issue 6 years ago • 3 comments

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'])

nininininini avatar Nov 04 '19 08:11 nininininini

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

jiangzhonglian avatar Nov 05 '19 09:11 jiangzhonglian

any response? same problem here

ferquintana84 avatar Apr 11 '20 11:04 ferquintana84

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_hot_vectorizer.vocabulary_) => {'time': 6, 'flies': 3, 'like': 5, 'an': 0, 'arrow': 1, 'fruit': 4, 'banana': 2}

print(sorted(one_hot_vectorizer.vocabulary_)) => ['an', 'arrow', 'banana', 'flies', 'fruit', 'like', 'time'] vocab = sorted(one_hot_vectorizer.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_hot_vectorizer.vocabulary_)[1:5] sns.heatmap(one_hot[:,1:5], annot=True, cbar=False, xticklabels= vocab_2, yticklabels= ['Sentence_1','Sentence_2']);

yu-cheng-kuo-28 avatar Sep 11 '20 08:09 yu-cheng-kuo-28