Overview
Provides data handling classes for the GeoIMC (Geometry-aware Inductive Matrix Completion) model, including filtered data access, base dataset preprocessing, and a MovieLens-100K specific implementation.
Description
This module contains three classes that form the data infrastructure for the GeoIMC model. DataPtr is a lightweight pointer class that holds a sparse CSR target matrix and entity feature arrays, supporting filtered access via configurable row and column indices through data_indices and entity_indices. Dataset is the base class that provides feature normalization (using either length normalization or sklearn's normalize for CSR matrices), PCA dimensionality reduction via the reduce_dims utility, and train/test splitting on row entities to enable cold-start evaluation scenarios. ML_100K extends Dataset to handle the MovieLens-100K dataset specifically: it reads user features (age, gender, occupation, zip code as one-hot encoded columns) and item features (movie title, release date, genres), converts ratings into COO sparse matrices with optional normalization or binarization target transforms, and loads pre-split train/test files.
Usage
Use this module when working with the GeoIMC inductive matrix completion model. The ML_100K class provides end-to-end data loading for MovieLens-100K, while the Dataset base class can be extended for other datasets. Use DataPtr when you need filtered views of the target matrix and entity features without copying data.
Code Reference
Source Location
Signature
class DataPtr:
def __init__(self, data, entities)
def get_data(self)
def get_entity(self, of="row")
class Dataset:
def __init__(self, name, features_dim=0, normalize=False, target_transform="")
def normalize(self)
def generate_train_test_data(self, data, test_ratio=0.3)
def reduce_dims(self)
class ML_100K(Dataset):
def __init__(self, **kwargs)
def df2coo(self, df)
def load_data(self, path)
def _load_user_features(self, path)
def _load_item_features(self, path)
Import
from recommenders.models.geoimc.geoimc_data import DataPtr, Dataset, ML_100K
I/O Contract
Inputs
DataPtr.__init__
| Name |
Type |
Required |
Description
|
| data |
csr_matrix |
Yes |
The target data matrix in CSR sparse format
|
| entities |
Iterator |
Yes |
An iterator of 2 elements (ndarray) containing features for row and column entities
|
Dataset.__init__
| Name |
Type |
Required |
Description
|
| name |
str |
Yes |
Name of the dataset
|
| features_dim |
int |
No |
Dimension for PCA reduction; 0 means no reduction (default 0)
|
| normalize |
bool |
No |
Whether to normalize entity features (default False)
|
| target_transform |
str |
No |
Transform for target values: 'normalize', 'binarize', or for none (default )
|
Dataset.generate_train_test_data
| Name |
Type |
Required |
Description
|
| data |
csr_matrix |
Yes |
The entire target matrix
|
| test_ratio |
float |
No |
Ratio of test split (default 0.3)
|
ML_100K.load_data
| Name |
Type |
Required |
Description
|
| path |
str |
Yes |
Path to the directory containing the ML-100K dataset files
|
Outputs
DataPtr.get_data
| Name |
Type |
Description
|
| return |
csr_matrix |
Target matrix, optionally filtered by data_indices
|
DataPtr.get_entity
| Name |
Type |
Description
|
| return |
numpy.ndarray |
Entity feature matrix, optionally filtered by entity_indices
|
ML_100K.df2coo
| Name |
Type |
Description
|
| return |
coo_matrix |
Sparse COO matrix of shape (943, 1682) representing user-item ratings
|
Usage Examples
Basic Usage
from recommenders.models.geoimc.geoimc_data import ML_100K
# Initialize the ML-100K dataset handler with optional preprocessing
dataset = ML_100K(
features_dim=50, # Reduce features to 50 dimensions via PCA
normalize=True, # Normalize entity features
target_transform="binarize" # Binarize ratings
)
# Load data from the ML-100K directory
dataset.load_data("/path/to/ml-100k")
# Access training data
train_target = dataset.training_data.get_data()
train_user_features = dataset.training_data.get_entity(of="row")
train_item_features = dataset.training_data.get_entity(of="col")
# Access test data
test_target = dataset.test_data.get_data()
test_user_features = dataset.test_data.get_entity(of="row")
test_item_features = dataset.test_data.get_entity(of="col")
Related Pages