| """Landsat Dataset""" |
|
|
| from typing import List |
| from functools import partial |
|
|
| import datasets |
|
|
| import pandas |
|
|
|
|
| VERSION = datasets.Version("1.0.0") |
|
|
| _ENCODING_DICS = {} |
|
|
| DESCRIPTION = "Landsat dataset." |
| _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/78/page+blocks+classification" |
| _URLS = ("https://archive-beta.ics.uci.edu/dataset/78/page+blocks+classification") |
| _CITATION = """ |
| @misc{misc_statlog_(landsat_satellite)_146, |
| author = {Srinivasan,Ashwin}, |
| title = {{Statlog (Landsat Satellite)}}, |
| year = {1993}, |
| howpublished = {UCI Machine Learning Repository}, |
| note = {{DOI}: \\url{10.24432/C55887}} |
| } |
| """ |
|
|
| |
| urls_per_split = { |
| "train": "https://huggingface.co/datasets/mstz/landsat/raw/main/landsat.csv" |
| } |
| features_types_per_config = { |
| "landsat": { |
| "f1": datasets.Value("int32"), |
| "f2": datasets.Value("int32"), |
| "f3": datasets.Value("int32"), |
| "f4": datasets.Value("int32"), |
| "class": datasets.ClassLabel(num_classes=6), |
| }, |
| "landsat_0": { |
| "f1": datasets.Value("int32"), |
| "f2": datasets.Value("int32"), |
| "f3": datasets.Value("int32"), |
| "f4": datasets.Value("int32"), |
| "class": datasets.ClassLabel(num_classes=2), |
| }, |
| "landsat_1": { |
| "f1": datasets.Value("int32"), |
| "f2": datasets.Value("int32"), |
| "f3": datasets.Value("int32"), |
| "f4": datasets.Value("int32"), |
| "class": datasets.ClassLabel(num_classes=2), |
| }, |
| "landsat_2": { |
| "f1": datasets.Value("int32"), |
| "f2": datasets.Value("int32"), |
| "f3": datasets.Value("int32"), |
| "f4": datasets.Value("int32"), |
| "class": datasets.ClassLabel(num_classes=2), |
| }, |
| "landsat_3": { |
| "f1": datasets.Value("int32"), |
| "f2": datasets.Value("int32"), |
| "f3": datasets.Value("int32"), |
| "f4": datasets.Value("int32"), |
| "class": datasets.ClassLabel(num_classes=2), |
| }, |
| "landsat_4": { |
| "f1": datasets.Value("int32"), |
| "f2": datasets.Value("int32"), |
| "f3": datasets.Value("int32"), |
| "f4": datasets.Value("int32"), |
| "class": datasets.ClassLabel(num_classes=2), |
| }, |
| "landsat_5": { |
| "f1": datasets.Value("int32"), |
| "f2": datasets.Value("int32"), |
| "f3": datasets.Value("int32"), |
| "f4": datasets.Value("int32"), |
| "class": datasets.ClassLabel(num_classes=2), |
| }, |
| } |
| features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} |
|
|
|
|
| class LandsatConfig(datasets.BuilderConfig): |
| def __init__(self, **kwargs): |
| super(LandsatConfig, self).__init__(version=VERSION, **kwargs) |
| self.features = features_per_config[kwargs["name"]] |
|
|
|
|
| class Landsat(datasets.GeneratorBasedBuilder): |
| |
| DEFAULT_CONFIG = "landsat" |
| BUILDER_CONFIGS = [ |
| LandsatConfig(name="landsat", description="Landsat for multiclass classification."), |
| LandsatConfig(name="landsat_0", description="Landsat for binary classification."), |
| LandsatConfig(name="landsat_1", description="Landsat for binary classification."), |
| LandsatConfig(name="landsat_2", description="Landsat for binary classification."), |
| LandsatConfig(name="landsat_3", description="Landsat for binary classification."), |
| LandsatConfig(name="landsat_4", description="Landsat for binary classification."), |
| LandsatConfig(name="landsat_5", description="Landsat 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: pandas.DataFrame) -> pandas.DataFrame: |
| if self.config.name == "landsat_0": |
| data["class"] = data["class"].apply(lambda x: 1 if x == 0 else 0) |
| elif self.config.name == "landsat_1": |
| data["class"] = data["class"].apply(lambda x: 1 if x == 1 else 0) |
| elif self.config.name == "landsat_2": |
| data["class"] = data["class"].apply(lambda x: 1 if x == 2 else 0) |
| elif self.config.name == "landsat_3": |
| data["class"] = data["class"].apply(lambda x: 1 if x == 3 else 0) |
| elif self.config.name == "landsat_4": |
| data["class"] = data["class"].apply(lambda x: 1 if x == 4 else 0) |
| elif self.config.name == "landsat_5": |
| data["class"] = data["class"].apply(lambda x: 1 if x == 5 else 0) |
|
|
| for feature in _ENCODING_DICS: |
| encoding_function = partial(self.encode, feature) |
| data.loc[:, feature] = data[feature].apply(encoding_function) |
| |
| return data[list(features_types_per_config[self.config.name].keys())] |
|
|
| def encode(self, feature, value): |
| if feature in _ENCODING_DICS: |
| return _ENCODING_DICS[feature][value] |
| raise ValueError(f"Unknown feature: {feature}") |
|
|