jester_rating / jester_rating.py
kitkatdafu's picture
update
9952ea4
import pandas as pd
import datasets
from sklearn.model_selection import train_test_split
import numpy as np
_CITATION = "N/A"
_DESCRIPTION = "N/A"
_HOMEPAGE = "N/A"
_LICENSE = "apache-2.0"
class JesterEmbedding(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("0.0.1")
def _info(self):
features = datasets.Features({
"user_id": datasets.Value("int64"),
"item_id": datasets.Value("int64"),
"rating": datasets.Value("float64"),
})
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
urls = "./jester_rating.parquet"
data_dir = dl_manager.download_and_extract(urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": data_dir,
"split": "train",
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": data_dir,
"split": "dev",
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": data_dir,
"split": "test"
},
),
]
def _generate_examples(self, filepath, split):
df = pd.read_parquet(filepath)
rng = np.random.RandomState(42)
train_df, test_df, val_df = [], [], []
for user_id in df.user_id.unique():
if len(df[df.user_id == user_id]) < 3:
continue
_train_df, _test_df = train_test_split(df[df.user_id == user_id], test_size=0.2, random_state=rng)
_train_df, _val_df = train_test_split(_train_df, test_size=0.2, random_state=rng)
train_df.append(_train_df)
val_df.append(_val_df)
test_df.append(_test_df)
train_df = pd.concat(train_df)
test_df = pd.concat(test_df)
val_df = pd.concat(val_df)
if split == "train":
for _id, row in train_df.iterrows():
user_id, item_id, rating = row
user_id, item_id = int(user_id), int(item_id)
yield _id, {"user_id": user_id, "item_id": item_id, "rating": rating}
elif split == "test":
for _id, row in test_df.iterrows():
user_id, item_id, rating = row
user_id, item_id = int(user_id), int(item_id)
yield _id, {"user_id": user_id, "item_id": item_id, "rating": rating}
else:
for _id, row in val_df.iterrows():
user_id, item_id, rating = row
user_id, item_id = int(user_id), int(item_id)
yield _id, {"user_id": user_id, "item_id": item_id, "rating": rating}