mstz commited on
Commit
d15fcb4
1 Parent(s): 90e82da

Upload sonar.py

Browse files
Files changed (1) hide show
  1. sonar.py +72 -0
sonar.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+
3
+ import datasets
4
+
5
+ import pandas
6
+ import gzip
7
+
8
+
9
+ VERSION = datasets.Version("1.0.0")
10
+
11
+
12
+ DESCRIPTION = "Sonar dataset from the UCI ML repository."
13
+ _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/31/sonar"
14
+ _URLS = ("https://archive-beta.ics.uci.edu/dataset/31/sonar")
15
+ _CITATION = """"""
16
+
17
+ # Dataset info
18
+ urls_per_split = {
19
+ "train": "https://huggingface.co/datasets/mstz/sonar/raw/main/sonar.all-data"
20
+ }
21
+ features_types_per_config = {
22
+ "sonar": {str(i): datasets.Value("float32") for i in range(60)}
23
+ }
24
+ features_types_per_config["sonar"]["is_rock"] = datasets.ClassLabel(num_classes=2)
25
+ features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
26
+
27
+
28
+ class SonarConfig(datasets.BuilderConfig):
29
+ def __init__(self, **kwargs):
30
+ super(SonarConfig, self).__init__(version=VERSION, **kwargs)
31
+ self.features = features_per_config[kwargs["name"]]
32
+
33
+
34
+ class Sonar(datasets.GeneratorBasedBuilder):
35
+ # dataset versions
36
+ DEFAULT_CONFIG = "sonar"
37
+ BUILDER_CONFIGS = [
38
+ SonarConfig(name="sonar",
39
+ description="Sonar for binary classification.")
40
+ ]
41
+
42
+
43
+ def _info(self):
44
+ if self.config.name not in features_per_config:
45
+ raise ValueError(f"Unknown configuration: {self.config.name}")
46
+
47
+ info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
48
+ features=features_per_config[self.config.name])
49
+
50
+ return info
51
+
52
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
53
+ downloads = dl_manager.download_and_extract(urls_per_split)
54
+
55
+ return [
56
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]})
57
+ ]
58
+
59
+ def _generate_examples(self, filepath: str):
60
+ data = pandas.read_csv(filepath, header=None)
61
+ data.columns = [str(i) for i in range(60)] + ["is_rock"]
62
+ data = self.preprocess(data, config=self.config.name)
63
+
64
+ for row_id, row in data.iterrows():
65
+ data_row = dict(row)
66
+
67
+ yield row_id, data_row
68
+
69
+ def preprocess(self, data: pandas.DataFrame, config: str = DEFAULT_CONFIG) -> pandas.DataFrame:
70
+ data.loc[:, "is_rock"] = data["is_rock"].apply(lambda x: 1 if x == "R" else 0)
71
+
72
+ return data