airbnb / airbnb.py
wscode's picture
add initial version of airbnb dataset
69dd106
raw
history blame
6.17 kB
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 = """
"""
_CITIES = [
"Amsterdam",
"Athens",
"Barcelona",
"Berlin",
"Budapest",
"Lisbon",
"London",
"Paris",
"Rome",
"Vienna"
]
_BASE_URL = "https://zenodo.org/record/4446043/files/"
_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]
class Airbnb(datasets.GeneratorBasedBuilder):
""""""
BUILDER_CONFIGS = [
AirbnbConfig(
name=DayType.WEEKDAYS.value,
files=_WEEKDAY_FILES,
),
AirbnbConfig(
name=DayType.WEEKENDS.value,
files=_WEEKEND_FILES,
),
AirbnbConfig(
name="all",
files=_WEEKDAY_FILES + _WEEKEND_FILES,
),
]
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