"""Titanic""" from typing import List import datasets import pandas VERSION = datasets.Version("1.0.0") DESCRIPTION = "Titanic dataset from the UCI ML repository." _HOMEPAGE = "https://www.kaggle.com/datasets/vinicius150987/titanic3" _URLS = ("https://www.kaggle.com/datasets/vinicius150987/titanic3") _CITATION = """""" # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/titanic/raw/main/titanic.csv" } features_types_per_config = { "survival": { "passenger_class": datasets.Value("int8"), "is_male": datasets.Value("bool"), "age": datasets.Value("int8"), "sibsp": datasets.Value("float64"), "parch": datasets.Value("float64"), "ticket": datasets.Value("string"), "fare": datasets.Value("float64"), "cabin": datasets.Value("string"), "embarked": datasets.Value("string"), "has_survived": datasets.ClassLabel(num_classes=2, names=("no", "yes")) }, } features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} class TitanicConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(TitanicConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Titanic(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "survival" BUILDER_CONFIGS = [ TitanicConfig(name="survival", description="Titanic for binary classification.") ] def _info(self): info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE, features=features_per_config[self.config.name]) return info def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]: downloads = dl_manager.download_and_extract(urls_per_split) return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}) ] def _generate_examples(self, filepath: str): data = pandas.read_csv(filepath) data = self.preprocess(data) for row_id, row in data.iterrows(): data_row = dict(row) yield row_id, data_row def preprocess(self, data): data = data.rename(columns={"sex": "is_male"}) data = data[list(features_types_per_config[self.config.name].keys())] data.loc[:, "is_male"] = data.is_male.apply(lambda x: x == "male") print(data.head()) return data