ขั้นตอนที่ 2: สํารวจข้อมูลของคุณ

การสร้างและฝึกโมเดลเป็นเพียงส่วนหนึ่งของเวิร์กโฟลว์เท่านั้น การทําความเข้าใจลักษณะของข้อมูลล่วงหน้าจะช่วยให้คุณสร้างโมเดลที่ดียิ่งขึ้น ซึ่งอาจหมายถึงการได้ตัวเลขที่แม่นยํามากขึ้น หรืออาจลดความจําเป็นในการฝึกอบรมหรือ ลดทรัพยากรที่ใช้คํานวณ

โหลดชุดข้อมูล

อย่างแรก มาโหลดชุดข้อมูลลงใน Python กัน

def load_imdb_sentiment_analysis_dataset(data_path, seed=123):
    """Loads the IMDb movie reviews sentiment analysis dataset.

    # Arguments
        data_path: string, path to the data directory.
        seed: int, seed for randomizer.

    # Returns
        A tuple of training and validation data.
        Number of training samples: 25000
        Number of test samples: 25000
        Number of categories: 2 (0 - negative, 1 - positive)

    # References
        Mass et al., http://www.aclweb.org/anthology/P11-1015

        Download and uncompress archive from:
        http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz
    """
    imdb_data_path = os.path.join(data_path, 'aclImdb')

    # Load the training data
    train_texts = []
    train_labels = []
    for category in ['pos', 'neg']:
        train_path = os.path.join(imdb_data_path, 'train', category)
        for fname in sorted(os.listdir(train_path)):
            if fname.endswith('.txt'):
                with open(os.path.join(train_path, fname)) as f:
                    train_texts.append(f.read())
                train_labels.append(0 if category == 'neg' else 1)

    # Load the validation data.
    test_texts = []
    test_labels = []
    for category in ['pos', 'neg']:
        test_path = os.path.join(imdb_data_path, 'test', category)
        for fname in sorted(os.listdir(test_path)):
            if fname.endswith('.txt'):
                with open(os.path.join(test_path, fname)) as f:
                    test_texts.append(f.read())
                test_labels.append(0 if category == 'neg' else 1)

    # Shuffle the training data and labels.
    random.seed(seed)
    random.shuffle(train_texts)
    random.seed(seed)
    random.shuffle(train_labels)

    return ((train_texts, np.array(train_labels)),
            (test_texts, np.array(test_labels)))

ตรวจสอบข้อมูล

หลังจากโหลดข้อมูลแล้ว เป็นแนวทางปฏิบัติที่ดีที่จะทําการตรวจสอบข้อมูล เช่น เลือกตัวอย่างและตรวจสอบด้วยตนเองว่าเป็นไปตามความคาดหวังของคุณไหม เช่น พิมพ์ตัวอย่างแบบสุ่ม 2-3 ตัวอย่างเพื่อดูว่าป้ายกํากับความเห็นสอดคล้องกับความรู้สึกของรีวิวหรือไม่ นี่คือการสุ่มรีวิวจากชุดข้อมูล IMDb "ความยาวเกือบ 10 นาที" จะยาวไปถึง 2 ชั่วโมง เมื่อไม่มีนัยสําคัญใดๆ เกิดขึ้นที่จุดครึ่งทางที่ฉันควรจะออกไป" ความรู้สึกที่คาดหวัง (เชิงลบ) จะตรงกับป้ายกํากับของตัวอย่าง

รวบรวมเมตริกที่สําคัญ

เมื่อยืนยันข้อมูลแล้ว ให้รวบรวมเมตริกสําคัญต่อไปนี้ซึ่งจะช่วยกําหนดลักษณะการจัดประเภทข้อความได้

  1. จํานวนตัวอย่าง: จํานวนตัวอย่างทั้งหมดที่คุณมีในข้อมูล

  2. จํานวนชั้นเรียน: จํานวนหัวข้อหรือหมวดหมู่ทั้งหมดในข้อมูล

  3. จํานวนตัวอย่างต่อชั้นเรียน: จํานวนตัวอย่างต่อชั้นเรียน (หัวข้อ/หมวดหมู่) ในชุดข้อมูลที่สมดุล ตัวอย่างทั้งหมดจะมีจํานวนตัวอย่างใกล้เคียงกัน ในชุดข้อมูลที่ไม่สมดุล จํานวนตัวอย่างในแต่ละคลาสจะแตกต่างกันไป

  4. จํานวนคําต่อตัวอย่าง: จํานวนคํามัธยฐานในตัวอย่าง 1 คํา

  5. การกระจายความถี่ของคํา: การกระจายแสดงความถี่ (จํานวนรายการ) ของแต่ละคําในชุดข้อมูล

  6. การกระจายของความยาวของตัวอย่าง: การกระจายแสดงจํานวนคําต่อตัวอย่างในชุดข้อมูล

เรามาดูกันว่าค่าของเมตริกเหล่านี้สําหรับชุดข้อมูลรีวิวของ IMDb เป็นอย่างไร (ดูรูปที่ 3 และ 4 สําหรับโครงแบบของความถี่ของคําและความยาวของตัวอย่าง)

ชื่อเมตริก ค่าเมตริก
จํานวนตัวอย่าง 25000
จํานวนชั้นเรียน 2
จํานวนตัวอย่างต่อคลาส 12500
จํานวนคําต่อตัวอย่าง 174

ตารางที่ 1: IMDb ตรวจสอบเมตริกชุดข้อมูล

explore_data.py มีฟังก์ชันเพื่อคํานวณและวิเคราะห์เมตริกเหล่านี้ ดังตัวอย่างต่อไปนี้

import numpy as np
import matplotlib.pyplot as plt

def get_num_words_per_sample(sample_texts):
    """Returns the median number of words per sample given corpus.

    # Arguments
        sample_texts: list, sample texts.

    # Returns
        int, median number of words per sample.
    """
    num_words = [len(s.split()) for s in sample_texts]
    return np.median(num_words)

def plot_sample_length_distribution(sample_texts):
    """Plots the sample length distribution.

    # Arguments
        samples_texts: list, sample texts.
    """
    plt.hist([len(s) for s in sample_texts], 50)
    plt.xlabel('Length of a sample')
    plt.ylabel('Number of samples')
    plt.title('Sample length distribution')
    plt.show()

การกระจายความถี่ของคําสําหรับ IMDb

รูปที่ 3: การกระจายความถี่ของคําสําหรับ IMDb

การกระจายของความยาวตัวอย่างสําหรับ IMDb

รูปที่ 4: การแจกแจงความยาวของตัวอย่างสําหรับ IMDb