steel_plates / steel_plates.py
mstz's picture
Upload steel_plates.py
fb080e7
raw
history blame
14.8 kB
"""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/198/steel+plates+faults"
_URLS = ("https://archive-beta.ics.uci.edu/dataset/198/steel+plates+faults")
_CITATION = """
@misc{misc_steel_plates_faults_198,
author = {Buscema,M, Terzi,S & Tastle,W},
title = {{Steel Plates Faults}},
year = {2010},
howpublished = {UCI Machine Learning Repository},
note = {{DOI}: \\url{10.24432/C5J88N}}
}
"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/steel_plates/raw/main/steel_plates.csv"
}
features_types_per_config = {
"steel_plates": {
"x_minimum": datasets.Value("int64"),
"x_maximum": datasets.Value("int64"),
"y_minimum": datasets.Value("int64"),
"y_maximum": datasets.Value("int64"),
"pixels_areas": datasets.Value("int64"),
"x_perimeter": datasets.Value("int64"),
"y_perimeter": datasets.Value("int64"),
"sum_of_luminosity": datasets.Value("int64"),
"minimum_of_luminosity": datasets.Value("int64"),
"maximum_of_luminosity": datasets.Value("int64"),
"length_of_conveyer": datasets.Value("int64"),
"typeofsteel_a300": datasets.Value("int64"),
"typeofsteel_a400": datasets.Value("int64"),
"steel_plate_thickness": datasets.Value("int64"),
"edges_index": datasets.Value("float64"),
"empty_index": datasets.Value("float64"),
"square_index": datasets.Value("float64"),
"outside_x_index": datasets.Value("float64"),
"edges_x_index": datasets.Value("float64"),
"edges_y_index": datasets.Value("float64"),
"outside_global_index": datasets.Value("float64"),
"logofareas": datasets.Value("float64"),
"log_x_index": datasets.Value("float64"),
"log_y_index": datasets.Value("float64"),
"orientation_index": datasets.Value("float64"),
"luminosity_index": datasets.Value("float64"),
"sigmoidofareas": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=7),
},
"steel_plates_0": {
"x_minimum": datasets.Value("int64"),
"x_maximum": datasets.Value("int64"),
"y_minimum": datasets.Value("int64"),
"y_maximum": datasets.Value("int64"),
"pixels_areas": datasets.Value("int64"),
"x_perimeter": datasets.Value("int64"),
"y_perimeter": datasets.Value("int64"),
"sum_of_luminosity": datasets.Value("int64"),
"minimum_of_luminosity": datasets.Value("int64"),
"maximum_of_luminosity": datasets.Value("int64"),
"length_of_conveyer": datasets.Value("int64"),
"typeofsteel_a300": datasets.Value("int64"),
"typeofsteel_a400": datasets.Value("int64"),
"steel_plate_thickness": datasets.Value("int64"),
"edges_index": datasets.Value("float64"),
"empty_index": datasets.Value("float64"),
"square_index": datasets.Value("float64"),
"outside_x_index": datasets.Value("float64"),
"edges_x_index": datasets.Value("float64"),
"edges_y_index": datasets.Value("float64"),
"outside_global_index": datasets.Value("float64"),
"logofareas": datasets.Value("float64"),
"log_x_index": datasets.Value("float64"),
"log_y_index": datasets.Value("float64"),
"orientation_index": datasets.Value("float64"),
"luminosity_index": datasets.Value("float64"),
"sigmoidofareas": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"steel_plates_1": {
"x_minimum": datasets.Value("int64"),
"x_maximum": datasets.Value("int64"),
"y_minimum": datasets.Value("int64"),
"y_maximum": datasets.Value("int64"),
"pixels_areas": datasets.Value("int64"),
"x_perimeter": datasets.Value("int64"),
"y_perimeter": datasets.Value("int64"),
"sum_of_luminosity": datasets.Value("int64"),
"minimum_of_luminosity": datasets.Value("int64"),
"maximum_of_luminosity": datasets.Value("int64"),
"length_of_conveyer": datasets.Value("int64"),
"typeofsteel_a300": datasets.Value("int64"),
"typeofsteel_a400": datasets.Value("int64"),
"steel_plate_thickness": datasets.Value("int64"),
"edges_index": datasets.Value("float64"),
"empty_index": datasets.Value("float64"),
"square_index": datasets.Value("float64"),
"outside_x_index": datasets.Value("float64"),
"edges_x_index": datasets.Value("float64"),
"edges_y_index": datasets.Value("float64"),
"outside_global_index": datasets.Value("float64"),
"logofareas": datasets.Value("float64"),
"log_x_index": datasets.Value("float64"),
"log_y_index": datasets.Value("float64"),
"orientation_index": datasets.Value("float64"),
"luminosity_index": datasets.Value("float64"),
"sigmoidofareas": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"steel_plates_2": {
"x_minimum": datasets.Value("int64"),
"x_maximum": datasets.Value("int64"),
"y_minimum": datasets.Value("int64"),
"y_maximum": datasets.Value("int64"),
"pixels_areas": datasets.Value("int64"),
"x_perimeter": datasets.Value("int64"),
"y_perimeter": datasets.Value("int64"),
"sum_of_luminosity": datasets.Value("int64"),
"minimum_of_luminosity": datasets.Value("int64"),
"maximum_of_luminosity": datasets.Value("int64"),
"length_of_conveyer": datasets.Value("int64"),
"typeofsteel_a300": datasets.Value("int64"),
"typeofsteel_a400": datasets.Value("int64"),
"steel_plate_thickness": datasets.Value("int64"),
"edges_index": datasets.Value("float64"),
"empty_index": datasets.Value("float64"),
"square_index": datasets.Value("float64"),
"outside_x_index": datasets.Value("float64"),
"edges_x_index": datasets.Value("float64"),
"edges_y_index": datasets.Value("float64"),
"outside_global_index": datasets.Value("float64"),
"logofareas": datasets.Value("float64"),
"log_x_index": datasets.Value("float64"),
"log_y_index": datasets.Value("float64"),
"orientation_index": datasets.Value("float64"),
"luminosity_index": datasets.Value("float64"),
"sigmoidofareas": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"steel_plates_3": {
"x_minimum": datasets.Value("int64"),
"x_maximum": datasets.Value("int64"),
"y_minimum": datasets.Value("int64"),
"y_maximum": datasets.Value("int64"),
"pixels_areas": datasets.Value("int64"),
"x_perimeter": datasets.Value("int64"),
"y_perimeter": datasets.Value("int64"),
"sum_of_luminosity": datasets.Value("int64"),
"minimum_of_luminosity": datasets.Value("int64"),
"maximum_of_luminosity": datasets.Value("int64"),
"length_of_conveyer": datasets.Value("int64"),
"typeofsteel_a300": datasets.Value("int64"),
"typeofsteel_a400": datasets.Value("int64"),
"steel_plate_thickness": datasets.Value("int64"),
"edges_index": datasets.Value("float64"),
"empty_index": datasets.Value("float64"),
"square_index": datasets.Value("float64"),
"outside_x_index": datasets.Value("float64"),
"edges_x_index": datasets.Value("float64"),
"edges_y_index": datasets.Value("float64"),
"outside_global_index": datasets.Value("float64"),
"logofareas": datasets.Value("float64"),
"log_x_index": datasets.Value("float64"),
"log_y_index": datasets.Value("float64"),
"orientation_index": datasets.Value("float64"),
"luminosity_index": datasets.Value("float64"),
"sigmoidofareas": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"steel_plates_4": {
"x_minimum": datasets.Value("int64"),
"x_maximum": datasets.Value("int64"),
"y_minimum": datasets.Value("int64"),
"y_maximum": datasets.Value("int64"),
"pixels_areas": datasets.Value("int64"),
"x_perimeter": datasets.Value("int64"),
"y_perimeter": datasets.Value("int64"),
"sum_of_luminosity": datasets.Value("int64"),
"minimum_of_luminosity": datasets.Value("int64"),
"maximum_of_luminosity": datasets.Value("int64"),
"length_of_conveyer": datasets.Value("int64"),
"typeofsteel_a300": datasets.Value("int64"),
"typeofsteel_a400": datasets.Value("int64"),
"steel_plate_thickness": datasets.Value("int64"),
"edges_index": datasets.Value("float64"),
"empty_index": datasets.Value("float64"),
"square_index": datasets.Value("float64"),
"outside_x_index": datasets.Value("float64"),
"edges_x_index": datasets.Value("float64"),
"edges_y_index": datasets.Value("float64"),
"outside_global_index": datasets.Value("float64"),
"logofareas": datasets.Value("float64"),
"log_x_index": datasets.Value("float64"),
"log_y_index": datasets.Value("float64"),
"orientation_index": datasets.Value("float64"),
"luminosity_index": datasets.Value("float64"),
"sigmoidofareas": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"steel_plates_5": {
"x_minimum": datasets.Value("int64"),
"x_maximum": datasets.Value("int64"),
"y_minimum": datasets.Value("int64"),
"y_maximum": datasets.Value("int64"),
"pixels_areas": datasets.Value("int64"),
"x_perimeter": datasets.Value("int64"),
"y_perimeter": datasets.Value("int64"),
"sum_of_luminosity": datasets.Value("int64"),
"minimum_of_luminosity": datasets.Value("int64"),
"maximum_of_luminosity": datasets.Value("int64"),
"length_of_conveyer": datasets.Value("int64"),
"typeofsteel_a300": datasets.Value("int64"),
"typeofsteel_a400": datasets.Value("int64"),
"steel_plate_thickness": datasets.Value("int64"),
"edges_index": datasets.Value("float64"),
"empty_index": datasets.Value("float64"),
"square_index": datasets.Value("float64"),
"outside_x_index": datasets.Value("float64"),
"edges_x_index": datasets.Value("float64"),
"edges_y_index": datasets.Value("float64"),
"outside_global_index": datasets.Value("float64"),
"logofareas": datasets.Value("float64"),
"log_x_index": datasets.Value("float64"),
"log_y_index": datasets.Value("float64"),
"orientation_index": datasets.Value("float64"),
"luminosity_index": datasets.Value("float64"),
"sigmoidofareas": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"steel_plates_6": {
"x_minimum": datasets.Value("int64"),
"x_maximum": datasets.Value("int64"),
"y_minimum": datasets.Value("int64"),
"y_maximum": datasets.Value("int64"),
"pixels_areas": datasets.Value("int64"),
"x_perimeter": datasets.Value("int64"),
"y_perimeter": datasets.Value("int64"),
"sum_of_luminosity": datasets.Value("int64"),
"minimum_of_luminosity": datasets.Value("int64"),
"maximum_of_luminosity": datasets.Value("int64"),
"length_of_conveyer": datasets.Value("int64"),
"typeofsteel_a300": datasets.Value("int64"),
"typeofsteel_a400": datasets.Value("int64"),
"steel_plate_thickness": datasets.Value("int64"),
"edges_index": datasets.Value("float64"),
"empty_index": datasets.Value("float64"),
"square_index": datasets.Value("float64"),
"outside_x_index": datasets.Value("float64"),
"edges_x_index": datasets.Value("float64"),
"edges_y_index": datasets.Value("float64"),
"outside_global_index": datasets.Value("float64"),
"logofareas": datasets.Value("float64"),
"log_x_index": datasets.Value("float64"),
"log_y_index": datasets.Value("float64"),
"orientation_index": datasets.Value("float64"),
"luminosity_index": datasets.Value("float64"),
"sigmoidofareas": datasets.Value("float64"),
"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):
# dataset versions
DEFAULT_CONFIG = "steel_plates"
BUILDER_CONFIGS = [
LandsatConfig(name="steel_plates", description="Landsat for multiclass classification."),
LandsatConfig(name="steel_plates_0", description="Landsat for binary classification."),
LandsatConfig(name="steel_plates_1", description="Landsat for binary classification."),
LandsatConfig(name="steel_plates_2", description="Landsat for binary classification."),
LandsatConfig(name="steel_plates_3", description="Landsat for binary classification."),
LandsatConfig(name="steel_plates_4", description="Landsat for binary classification."),
LandsatConfig(name="steel_plates_5", description="Landsat for binary classification."),
LandsatConfig(name="steel_plates_6", 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 == "steel_plates_0":
data["class"] = data["class"].apply(lambda x: 1 if x == 0 else 0)
elif self.config.name == "steel_plates_1":
data["class"] = data["class"].apply(lambda x: 1 if x == 1 else 0)
elif self.config.name == "steel_plates_2":
data["class"] = data["class"].apply(lambda x: 1 if x == 2 else 0)
elif self.config.name == "steel_plates_3":
data["class"] = data["class"].apply(lambda x: 1 if x == 3 else 0)
elif self.config.name == "steel_plates_4":
data["class"] = data["class"].apply(lambda x: 1 if x == 4 else 0)
elif self.config.name == "steel_plates_5":
data["class"] = data["class"].apply(lambda x: 1 if x == 5 else 0)
elif self.config.name == "steel_plates_6":
data["class"] = data["class"].apply(lambda x: 1 if x == 6 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}")