Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีใช้ Keras ดาวน์โหลดและสำรวจชุดข้อมูล StackOverflow สำหรับ Tag Prediction ใน Python

บทนำ

Keras (ใน TensorFlow) ให้ Utility ดาวน์โหลด Dataset ยอดนิยม รวมถึง StackOverflow 16k สำหรับงาน Tag Prediction

การติดตั้ง

pip install tensorflow
pip install tensorflow-text

ตัวอย่างโค้ด

import pathlib
import tensorflow as tf
from tensorflow.keras import utils

data_url = 'https://storage.googleapis.com/download.tensorflow.org/data/stack_overflow_16k.tar.gz'
dataset = utils.get_file(
    'stack_overflow_16k.tar.gz',
    data_url,
    untar=True,
    cache_dir='stack_overflow',
    cache_subdir=''
)
dataset_dir = pathlib.Path(dataset).parent

# สำรวจโครงสร้าง
print(list(dataset_dir.iterdir()))
train_dir = dataset_dir / 'train'
print(list(train_dir.iterdir()))

# อ่านไฟล์ตัวอย่าง
sample_file = train_dir / 'python/1755.txt'
with open(sample_file) as f:
    print(f.read())

ผลลัพธ์

Downloading data from https://storage.googleapis.com/.../stack_overflow_16k.tar.gz
The stackoverflow questions are present in the 'train/' directory
A sample file is displayed
why does this blank program print true x=true.def stupid():. x=false.stupid().print x

คำอธิบาย

  • utils.get_file ดาวน์โหลด แตกไฟล์ .tar.gz อัตโนมัติ

  • โครงสร้าง: train/ มีโฟลเดอร์ย่อยเป็น Label (csharp, java, javascript, python)

  • แต่ละไฟล์ = 1 คำถาม