interaction_matrix
logger = logging.getLogger(__name__) module-attribute ¶
InteractionMatrix ¶
Bases: SelectionIDMixin, SelectionTimestampMixin
Matrix of interaction data between users and items.
It provides a number of properties and methods for easy manipulation of this interaction data.
.. attention::
- The InteractionMatrix does not assume binary user-item pairs.
If a user interacts with an item more than once, there will be two
entries for this user-item pair.
- We assume that the user and item IDs are integers starting from 0. IDs
that are indicated by "-1" are reserved to label the user or item to
be predicted. This assumption is crucial as it will be used during the
split scheme and evaluation of the RS since it will affect the 2D shape
of the CSR matrix
Source code in src/recnexteval/matrix/interaction_matrix.py
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
ITEM_IX = 'iid' class-attribute instance-attribute ¶
USER_IX = 'uid' class-attribute instance-attribute ¶
TIMESTAMP_IX = 'ts' class-attribute instance-attribute ¶
INTERACTION_IX = 'interactionid' class-attribute instance-attribute ¶
MASKED_LABEL = -1 class-attribute instance-attribute ¶
user_item_shape instance-attribute ¶
The shape of the interaction matrix, i.e. |user| x |item|.
values property ¶
All user-item interactions as a sparse matrix of size (|users|, |items|).
The shape of the matrix is determined by the user_item_shape attribute. Each row represents a user and each column represents an item. The index of the rows and columns correspond to the user and item IDs respectively. An entry in the matrix is 1 if there is an interaction.
indices property ¶
Returns a tuple of lists of user IDs and item IDs corresponding to interactions.
:return: tuple of lists of user IDs and item IDs that correspond to at least one interaction. :rtype: tuple[list[int], list[int]]
item_interaction_sequence_matrix property ¶
Converts the interaction data into an item interaction sequence matrix.
Dataframe values are converted into such that the row sequence is maintained and the item that interacted with will have the column at item_id marked with 1.
user_id_sequence_array property ¶
Array of user IDs in the order of interactions.
:return: Numpy array of user IDs. :rtype: np.ndarray
user_ids property ¶
The set of all user ID in matrix
item_ids property ¶
The set of all item ID in matrix
num_interactions property ¶
The total number of interactions.
:return: Total interaction count. :rtype: int
has_timestamps property ¶
Boolean indicating whether instance has timestamp information.
:return: True if timestamps information is available, False otherwise. :rtype: bool
min_timestamp property ¶
The earliest timestamp in the interaction
:return: The earliest timestamp. :rtype: int
max_timestamp property ¶
The latest timestamp in the interaction
:return: The latest timestamp. :rtype: int
global_num_user property ¶
global_num_item property ¶
known_num_user property ¶
The highest known number of users
Note that we add 1 to the max known user ID to get the number of users, since user IDs are zero-indexed.
known_num_item property ¶
The highest known user ID in the interaction matrix.
max_user_id property ¶
The highest known user ID in the interaction matrix.
:return: The highest user ID. :rtype: int
max_item_id property ¶
The highest known item ID in the interaction matrix.
In the case of an empty matrix, the highest item ID is -1. This is consistent with the the definition that -1 denotes the item that is unknown. It would be incorrect to use any other value, since 0 is a valid item ID.
:return: The highest item ID. :rtype: int
timestamps property ¶
Timestamps of interactions as a pandas Series, indexed by user ID and item ID.
:raises TimestampAttributeMissingError: If timestamp column is missing. :return: Interactions with composite index on (user ID, item ID) :rtype: pd.Series
latest_interaction_timestamps_matrix property ¶
A csr matrix containing the last interaction timestamp for each user, item pair.
We only account for the last interacted timestamp making the dataset non-deduplicated.
copy() ¶
Create a deep copy of this InteractionMatrix.
Source code in src/recnexteval/matrix/interaction_matrix.py
77 78 79 | |
copy_df(reset_index=False) ¶
Create a deep copy of the dataframe.
Source code in src/recnexteval/matrix/interaction_matrix.py
81 82 83 84 85 | |
concat(im) ¶
Concatenate this InteractionMatrix with another.
Note
This is a inplace operation. and will modify the current object.
Source code in src/recnexteval/matrix/interaction_matrix.py
87 88 89 90 91 92 93 94 95 96 97 98 | |
union(im) ¶
Combine events from this InteractionMatrix with another.
Source code in src/recnexteval/matrix/interaction_matrix.py
101 102 103 | |
difference(im) ¶
Difference between this InteractionMatrix and another.
Source code in src/recnexteval/matrix/interaction_matrix.py
105 106 107 | |
nonzero() ¶
Source code in src/recnexteval/matrix/interaction_matrix.py
139 140 | |
timestamps_gt(timestamp, inplace=False) ¶
timestamps_gt(timestamp: float) -> T
timestamps_gt(
timestamp: float, inplace: Literal[True]
) -> None
Select interactions after a given timestamp.
:param timestamp: The timestamp with which the interactions timestamp is compared. :type timestamp: float :param inplace: Apply the selection in place if True, defaults to False :type inplace: bool, optional :return: None if inplace, otherwise returns a new InteractionMatrix object :rtype: Union[InteractionMatrix, None]
Source code in src/recnexteval/matrix/filters.py
67 68 69 70 71 72 73 74 75 76 77 78 | |
timestamps_gte(timestamp, inplace=False) ¶
timestamps_gte(timestamp: float) -> T
timestamps_gte(
timestamp: float, inplace: Literal[True]
) -> None
Select interactions after and including a given timestamp.
:param timestamp: The timestamp with which the interactions timestamp is compared. :type timestamp: float :param inplace: Apply the selection in place if True, defaults to False :type inplace: bool, optional :return: None if inplace, otherwise returns a new InteractionMatrix object :rtype: Union[InteractionMatrix, None]
Source code in src/recnexteval/matrix/filters.py
84 85 86 87 88 89 90 91 92 93 94 95 | |
timestamps_lt(timestamp, inplace=False) ¶
timestamps_lt(timestamp: float) -> T
timestamps_lt(
timestamp: float, inplace: Literal[True]
) -> None
Select interactions up to a given timestamp.
:param timestamp: The timestamp with which the interactions timestamp is compared. :type timestamp: float :param inplace: Apply the selection in place if True, defaults to False :type inplace: bool, optional :return: None if inplace, otherwise returns a new InteractionMatrix object :rtype: Union[InteractionMatrix, None]
Source code in src/recnexteval/matrix/filters.py
101 102 103 104 105 106 107 108 109 110 111 112 | |
timestamps_lte(timestamp, inplace=False) ¶
timestamps_lte(timestamp: float) -> T
timestamps_lte(
timestamp: float, inplace: Literal[True]
) -> None
Select interactions up to and including a given timestamp.
:param timestamp: The timestamp with which the interactions timestamp is compared. :type timestamp: float :param inplace: Apply the selection in place if True, defaults to False :type inplace: bool, optional :return: None if inplace, otherwise returns a new InteractionMatrix object :rtype: Union[InteractionMatrix, None]
Source code in src/recnexteval/matrix/filters.py
118 119 120 121 122 123 124 125 126 127 128 129 | |
get_users_n_last_interaction(n_seq_data=1, t_upper=None, user_in=None, inplace=False) ¶
Source code in src/recnexteval/matrix/filters.py
143 144 145 146 147 148 | |
get_items_n_last_interaction(n_seq_data=1, t_upper=None, item_in=None, inplace=False) ¶
Source code in src/recnexteval/matrix/filters.py
150 151 152 153 | |
get_users_n_first_interaction(n_seq_data=1, t_lower=None, inplace=False) ¶
Source code in src/recnexteval/matrix/filters.py
155 156 157 158 | |
get_items_n_first_interaction(n_seq_data=1, t_lower=None, inplace=False) ¶
Source code in src/recnexteval/matrix/filters.py
160 161 162 163 | |
users_in(U, inplace=False) ¶
users_in(U: set[int]) -> T
users_in(U: set[int], inplace: Literal[False]) -> T
users_in(U: set[int], inplace: Literal[True]) -> None
Source code in src/recnexteval/matrix/filters.py
23 24 25 26 | |
users_not_in(U, inplace=False) ¶
users_not_in(U: set[int]) -> T
users_not_in(U: set[int], inplace: Literal[False]) -> T
users_not_in(U: set[int], inplace: Literal[True]) -> None
Source code in src/recnexteval/matrix/filters.py
34 35 36 37 | |
items_in(id_set, inplace=False) ¶
items_in(id_set: set[int]) -> T
items_in(id_set: set[int], inplace: Literal[False]) -> T
items_in(id_set: set[int], inplace: Literal[True]) -> None
Source code in src/recnexteval/matrix/filters.py
45 46 47 48 | |
items_not_in(id_set, inplace=False) ¶
items_not_in(id_set: set[int]) -> T
items_not_in(
id_set: set[int], inplace: Literal[False]
) -> T
items_not_in(
id_set: set[int], inplace: Literal[True]
) -> None
Source code in src/recnexteval/matrix/filters.py
56 57 58 59 | |