File size: 6,752 Bytes
69dd106 5c9a6f2 69dd106 b9dd71f 69dd106 b9dd71f 69dd106 b9dd71f 69dd106 b9dd71f 69dd106 b9dd71f 69dd106 5c9a6f2 69dd106 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import datasets
from enum import Enum
from dataclasses import dataclass
from typing import List
import pandas as pd
logger = datasets.logging.get_logger(__name__)
_CITATION = """\
@dataset{gyodi_kristof_2021_4446043,
author = {Gyódi, Kristóf and
Nawaro, Łukasz},
title = {{Determinants of Airbnb prices in European cities:
A spatial econometrics approach (Supplementary
Material)}},
month = jan,
year = 2021,
note = {{This research was supported by National Science
Centre, Poland: Project number 2017/27/N/HS4/00951}},
publisher = {Zenodo},
doi = {10.5281/zenodo.4446043},
url = {https://doi.org/10.5281/zenodo.4446043}
}"""
_DESCRIPTION = """\
This dataset contains accommodation offers from the AirBnb platform from 10 European cities.
It has been copied from https://zenodo.org/record/4446043#.ZEV8d-zMI-R to make it available as a Huggingface Dataset.
It was originally published as supplementary material for the article: Determinants of Airbnb prices in European cities: A spatial econometrics approach
(DOI: https://doi.org/10.1016/j.tourman.2021.104319)"""
_CITIES = [
"Amsterdam",
"Athens",
"Barcelona",
"Berlin",
"Budapest",
"Lisbon",
"London",
"Paris",
"Rome",
"Vienna"
]
_BASE_URL = "data/"
_URL_TEMPLATE = _BASE_URL + "{city}_{day_type}.csv"
class DayType(str, Enum):
WEEKDAYS = "weekdays"
WEEKENDS = "weekends"
@dataclass
class AirbnbFile:
"""A file from the Airbnb dataset."""
city: str
day_type: DayType
@property
def url(self) -> str:
return _URL_TEMPLATE.format(city=self.city.lower(), day_type=self.day_type.value)
class AirbnbConfig(datasets.BuilderConfig):
"""BuilderConfig for Airbnb."""
def __init__(self, files: List[AirbnbFile], **kwargs):
"""BuilderConfig for Airbnb.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(AirbnbConfig, self).__init__(**kwargs)
self.files = files
_WEEKDAY_FILES = [AirbnbFile(city=city, day_type=DayType.WEEKDAYS) for city in _CITIES]
_WEEKEND_FILES = [AirbnbFile(city=city, day_type=DayType.WEEKENDS) for city in _CITIES]
_DATASET_VERSION = "2.0.0"
class Airbnb(datasets.GeneratorBasedBuilder):
""""""
BUILDER_CONFIGS = [
AirbnbConfig(
name=DayType.WEEKDAYS.value,
files=_WEEKDAY_FILES,
version=datasets.Version(_DATASET_VERSION),
),
AirbnbConfig(
name=DayType.WEEKENDS.value,
files=_WEEKEND_FILES,
version=datasets.Version(_DATASET_VERSION),
),
AirbnbConfig(
name="all",
files=_WEEKDAY_FILES + _WEEKEND_FILES,
version=datasets.Version(_DATASET_VERSION),
),
]
def _info(self):
features = datasets.Features(
{
"_id": datasets.Value("string"),
"city": datasets.Value("string"),
"realSum": datasets.Value(dtype="float64"),
"room_type": datasets.Value(dtype="string"),
"room_shared": datasets.Value(dtype="bool"),
"room_private": datasets.Value(dtype="bool"),
"person_capacity": datasets.Value(dtype="float64"),
"host_is_superhost": datasets.Value(dtype="bool"),
"multi": datasets.Value(dtype="int64"),
"biz": datasets.Value(dtype="int64"),
"cleanliness_rating": datasets.Value(dtype="float64"),
"guest_satisfaction_overall": datasets.Value(dtype="float64"),
"bedrooms": datasets.Value(dtype="int64"),
"dist": datasets.Value(dtype="float64"),
"metro_dist": datasets.Value(dtype="float64"),
"attr_index": datasets.Value(dtype="float64"),
"attr_index_norm": datasets.Value(dtype="float64"),
"rest_index": datasets.Value(dtype="float64"),
"rest_index_norm": datasets.Value(dtype="float64"),
"lng": datasets.Value(dtype="float64"),
"lat": datasets.Value(dtype="float64")
})
if self.config.name == "all":
features["day_type"] = datasets.Value(dtype="string")
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
supervised_keys=None,
homepage="https://zenodo.org/record/4446043#.ZEV8d-zMI-R",
citation=_CITATION,
)
def _split_generators(self, dl_manager):
config_files: List[AirbnbFile] = self.config.files
urls = [file.url for file in config_files]
downloaded_files = dl_manager.download_and_extract(urls)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"paths": downloaded_files})
]
def _generate_examples(self, paths: List[str]):
_id = 0
config_files: List[AirbnbFile] = self.config.files
include_day_type = self.config.name == "all"
for file, path in zip(config_files, paths):
logger.info("generating examples from = %s", path)
df = pd.read_csv(path, index_col=0, header=0)
for row in df.itertuples():
city = file.city
data = {
"_id": _id,
"city": city,
"realSum": row.realSum,
"room_type": row.room_type,
"room_shared": row.room_shared,
"room_private": row.room_private,
"person_capacity": row.person_capacity,
"host_is_superhost": row.host_is_superhost,
"multi": row.multi,
"biz": row.biz,
"cleanliness_rating": row.cleanliness_rating,
"guest_satisfaction_overall": row.guest_satisfaction_overall,
"bedrooms": row.bedrooms,
"dist": row.dist,
"metro_dist": row.metro_dist,
"attr_index": row.attr_index,
"attr_index_norm": row.attr_index_norm,
"rest_index": row.rest_index,
"rest_index_norm": row.rest_index_norm,
"lng": row.lng,
"lat": row.lat
}
if include_day_type:
data["day_type"] = file.day_type.value
yield _id, data
_id += 1
|