pysuggestify package

Submodules

pysuggestify.PMF module

class pysuggestify.PMF.PMF(n_dims=50, lambda_U=0.3, lambda_V=0.3)[source]

Bases: object

Probabilistic Matrix Factorization class for building recommendation models.

evaluate()[source]

Evaluates the performance of the recommendation model using root mean squared error (RMSE).

Returns:

The root mean squared error (RMSE) value indicating the model’s performance.

Return type:

float

Examples

# Example: rmse_score = PMF_model.evaluate()

fit(n_epochs=10)[source]

Trains the recommendation model using the specified number of epochs.

Parameters:

n_epochs (int) – The number of epochs (iterations) for training the model. Defaults to 10.

Returns:

None

Examples

# Example: PMF_model.fit(n_epochs=20)

get_masked_preds()[source]

Returns the masked predictions where the original rating matrix has zero values.

Returns:

The masked predictions, where predictions are shown only for positions

where the original rating matrix has zero values. Positions with non-zero values in the original matrix are masked (set to zero) in the predictions.

Return type:

numpy.ndarray

Examples

# Example: Getting the masked predictions PMF_model.get_masked_preds()

get_rating_matrix()[source]

Method to eturns the rating matrix R representing user-item interactions with necessary informations.

Parameters:

None

Returns:

rating matrix representing user-item interactions n_users: number of unique users n_items: number of unique items _user_to_row: dictionary which translate userID to row number _item_to_column: dictionary which translate movieID to column number

Return type:

R

initialize_parameters()[source]

Initializes the parameters for the recommendation model.

log_a_posteriori()[source]

Returns log a posteriori probability

plot_history()[source]

Plots the history of the training process, including the log a-posteriori probability and root mean square error (RMSE) values over the epochs.

Returns:

None

Examples

# Example: PMF_model.plot_history()

predict()[source]

Generates the predicted rating matrix using the trained model parameters.

Returns:

The predicted rating matrix, where rows correspond to users and columns correspond to items. Each element of the matrix represents the predicted rating for a user-item pair.

Return type:

R_pred

Examples

# Example: PMF_model = PMF(n_dims = 30, lambda_U = 0.3, lambda_V = 0.3) PMF_model.prepare_data(ratings, ‘userId’, ‘movieId’, ‘rating’) PMF_model.fit(n_epochs=50) PMF_model.predict()

predict_one(user_id, item_id)[source]

Predicts the rating for a specific user-item pair using the trained model parameters.

Parameters:
  • user_id – The ID of the user for whom the rating is predicted.

  • item_id – The ID of the item for which the rating is predicted.

Returns:

The predicted rating for the specified user-item pair.

Return type:

float

Raises: TODO

KeyError: If the user ID or item ID is not found in the respective dictionaries.

Examples

# Example: predicted_rating = PMF_model.predict_one(user_id=42, item_id=101)

prepare_data(df: DataFrame, row_id_name: str, col_id_name: str, rating_name: str) None[source]

Prepares the data for building a PMF recommendation model.

Parameters:

df (pandas.DataFrame) – The input data containing user-item interactions. It should have the following columns: user ID, item ID and rating.

Returns:

None

Examples

# Example: Loading data from a CSV file PMF_model.prepare_data(ratings, ‘userId’, ‘movieId’, ‘rating’)

transpose_dict(dictionary='user')[source]

Transposes the given dictionary, swapping keys and values.

Parameters:

dictionary (str) – Specifies the dictionary to transpose. Should be either ‘user’ or ‘item’. If ‘user’ is provided, the user-to-row dictionary will be transposed, swapping user IDs with their corresponding row indices. If ‘item’ is provided, the item-to-column dictionary will be transposed, swapping item IDs with their corresponding column indices. Defaults to ‘user’.

Returns:

The transposed dictionary, where keys and values are swapped.

Return type:

dict

Examples

# Example 1: Transposing the user-to-row dictionary PMF_model = PMF(n_dims = 30, lambda_U = 0.3, lambda_V = 0.3) PMF_model.prepare_data(ratings, ‘userId’, ‘movieId’, ‘rating’) transposed_user_dict = PMF_model.transpose_dict(dictionary=’user’)

# Example 2: Transposing the item-to-column dictionary PMF_model = PMF(n_dims = 30, lambda_U = 0.3, lambda_V = 0.3) PMF_model.prepare_data(ratings, ‘userId’, ‘movieId’, ‘rating’) transposed_item_dict = PMF_model.transpose_dict(dictionary=’item’)

update_max_min_ratings()[source]

Update min and max ratings in predicted matrix.

update_parameters()[source]

Method for update parameters - latent space matrixes.

pysuggestify.version module

Module contents