stMind

about Tech, Computer vision and Machine learning

imdb_reviews datasetのレビュー長のヒストグラム

データセット自体を理解し、モデル改善に役立てていきます。

Positiveなレビュー、Negativeなレビュー、それぞれについてレビュー長(ワード数)をカウントし、ヒストグラムで可視化します。

    # 4. Count the length of sentences
    all_sentences = training_sentences + testing_sentences
    all_labels = training_labels + testing_labels
    positive_sentence_length = [len(sentence.split(' ')) for sentence, label in zip(all_sentences, all_labels) if label == 1]
    negative_sentence_length = [len(sentence.split(' ')) for sentence, label in zip(all_sentences, all_labels) if label == 0]
    max_length = max(max(positive_sentence_length), max(negative_sentence_length))

    # 5. Plot the distribution
    plt.hist(positive_sentence_length, bins=max_length, alpha=0.5, rwidth=2, label='positive')
    plt.hist(negative_sentence_length, bins=max_length, alpha=0.5, rwidth=2, label='negative')
    plt.xlabel('Review length')
    plt.ylabel('Frequency')
    plt.legend()
    plt.show()

f:id:satojkovic:20200629210500p:plain

100〜200ワード前後が最も多いですね。