Content Based RecSys
ContentBasedRS(algorithm, train_set, items_directory, users_directory=None)
Bases: RecSys
Class for recommender systems which use the items' content in order to make predictions, some algorithms may also use users' content, so it's an optional parameter.
Every CBRS differ from each other based the algorithm used.
Examples:
In case you perform a splitting of the dataset which returns a single train and test set (e.g. HoldOut technique):
from clayrs import recsys as rs
from clayrs import content_analyzer as ca
original_rat = ca.Ratings(ca.CSVFile(ratings_path))
[train], [test] = rs.HoldOutPartitioning().split_all(original_rat)
alg = rs.CentroidVector() # any cb algorithm
cbrs = rs.ContentBasedRS(alg, train, items_path)
rank = cbrs.fit_rank(test, n_recs=10)
In case you perform a splitting of the dataset which returns a multiple train and test sets (KFold technique):
from clayrs import recsys as rs
from clayrs import content_analyzer as ca
original_rat = ca.Ratings(ca.CSVFile(ratings_path))
train_list, test_list = rs.KFoldPartitioning(n_splits=5).split_all(original_rat)
alg = rs.CentroidVector() # any cb algorithm
for train_set, test_set in zip(train_list, test_list):
cbrs = rs.ContentBasedRS(alg, train_set, items_path)
rank_to_append = cbrs.fit_rank(test_set)
result_list.append(rank_to_append)
result_list
will contain recommendation lists for each split
PARAMETER | DESCRIPTION |
---|---|
algorithm |
the content based algorithm that will be used in order to rank or make score prediction
TYPE:
|
train_set |
a Ratings object containing interactions between users and items
TYPE:
|
items_directory |
the path of the items serialized by the Content Analyzer
TYPE:
|
users_directory |
the path of the users serialized by the Content Analyzer
TYPE:
|
Source code in clayrs/recsys/recsys.py
110 111 112 113 114 115 116 117 118 119 120 |
|
algorithm: ContentBasedAlgorithm
property
The content based algorithm chosen
items_directory: str
property
Path of the serialized items by the Content Analyzer
train_set: Ratings
property
The train set of the Content Based RecSys
users_directory: str
property
Path of the serialized users by the Content Analyzer
fit(num_cpus=1)
Method which will fit the algorithm chosen for each user in the train set passed in the constructor
If the algorithm can't be fit for some users, a warning message is printed showing the number of users for which the alg couldn't be fit
PARAMETER | DESCRIPTION |
---|---|
num_cpus |
number of processors that must be reserved for the method. If set to
TYPE:
|
Source code in clayrs/recsys/recsys.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
fit_predict(test_set, user_list=None, methodology=TestRatingsMethodology(), save_fit=False, num_cpus=1)
Method used to both fit and calculate score prediction for all users in test set or all users in user_list
parameter.
The Recommender System will first be fit for each user in the test_set
parameter or for each
user inside the user_list
parameter: the user_list
parameter could contain users with their string id or
with their mapped integer
BE CAREFUL: not all algorithms are able to perform score prediction
Via the methodology
parameter you can perform different candidate item selection. By default, the
TestRatingsMethodology()
is used: so, for each user, items in its test set only will be considered for score
prediction
If the algorithm couldn't be fit for some users, they will be skipped and a warning message is printed showing the number of users for which the alg couldn't produce a ranking
With the save_fit
parameter you can decide if you want that you recommender system remains fit even after
the complete execution of this method, in case you want to compute ranking/score prediction with other
methodologies, or with a different n_recs
parameter. Be mindful since it can be memory-expensive,
thus by default this behaviour is disabled
PARAMETER | DESCRIPTION |
---|---|
test_set |
Ratings object which represents the ground truth of the split considered
TYPE:
|
user_list |
List of users for which you want to compute score prediction. If None, the ranking
will be computed for all users of the
TYPE:
|
methodology |
TYPE:
|
save_fit |
Boolean value which let you choose if the Recommender System should remain fit even after the complete execution of this method. Default is False
TYPE:
|
num_cpus |
number of processors that must be reserved for the method. If set to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Prediction
|
Prediction object containing score prediction lists for all users of the test set or for all users in
|
Source code in clayrs/recsys/recsys.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
|
fit_rank(test_set, n_recs=10, user_list=None, methodology=TestRatingsMethodology(), save_fit=False, num_cpus=1)
Method used to both fit and calculate ranking for all users in test set or all users in user_list
parameter.
The Recommender System will first be fit for each user in the test_set
parameter or for each
user inside the user_list
parameter: the user_list
parameter could contain users with their string id or
with their mapped integer
If the n_recs
is specified, then the rank will contain the top-n items for the users.
Otherwise, the rank will contain all unrated items of the particular users.
By default the top-10 ranking is computed for each user
Via the methodology
parameter you can perform different candidate item selection. By default, the
TestRatingsMethodology()
is used: so, for each user, items in its test set only will be ranked
If the algorithm couldn't be fit for some users, they will be skipped and a warning message is printed showing the number of users for which the alg couldn't produce a ranking
With the save_fit
parameter you can decide if you want that you recommender system remains fit even after
the complete execution of this method, in case you want to compute ranking with other methodologies, or
with a different n_recs
parameter. Be mindful since it can be memory-expensive, thus by default this behaviour
is disabled
PARAMETER | DESCRIPTION |
---|---|
test_set |
Ratings object which represents the ground truth of the split considered
TYPE:
|
n_recs |
Number of the top items that will be present in the ranking of each user.
If
TYPE:
|
user_list |
List of users for which you want to compute score prediction. If None, the ranking
will be computed for all users of the |
methodology |
TYPE:
|
save_fit |
Boolean value which let you choose if the Recommender System should remain fit even after the complete execution of this method. Default is False
TYPE:
|
num_cpus |
number of processors that must be reserved for the method. If set to
TYPE:
|
RAISES | DESCRIPTION |
---|---|
NotFittedAlg
|
Exception raised when this method is called without first calling the |
RETURNS | DESCRIPTION |
---|---|
Rank
|
Rank object containing recommendation lists for all users of the test set or for all users in |
Source code in clayrs/recsys/recsys.py
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 |
|
predict(test_set, user_list=None, methodology=TestRatingsMethodology(), num_cpus=1)
Method used to calculate score predictions for all users in test set or all users in user_list
parameter.
You must first call the fit()
method before you can compute score predictions.
The user_list
parameter could contain users with their string id or with their mapped integer
BE CAREFUL: not all algorithms are able to perform score prediction
Via the methodology
parameter you can perform different candidate item selection. By default, the
TestRatingsMethodology()
is used: so, for each user, items in its test set only will be considered for score
prediction
If the algorithm was not fit for some users, they will be skipped and a warning message is printed showing the number of users for which the alg couldn't produce a ranking
PARAMETER | DESCRIPTION |
---|---|
test_set |
Ratings object which represents the ground truth of the split considered
TYPE:
|
user_list |
List of users for which you want to compute score prediction. If None, the ranking
will be computed for all users of the
TYPE:
|
methodology |
TYPE:
|
num_cpus |
number of processors that must be reserved for the method. If set to
TYPE:
|
RAISES | DESCRIPTION |
---|---|
NotFittedAlg
|
Exception raised when this method is called without first calling the |
RETURNS | DESCRIPTION |
---|---|
Prediction
|
Prediction object containing score prediction lists for all users of the test set or for all users in
|
Source code in clayrs/recsys/recsys.py
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 |
|
rank(test_set, n_recs=10, user_list=None, methodology=TestRatingsMethodology(), num_cpus=1)
Method used to calculate ranking for all users in test set or all users in user_list
parameter.
You must first call the fit()
method before you can compute the ranking.
The user_list
parameter could contain users with their string id or with their mapped integer
If the n_recs
is specified, then the rank will contain the top-n items for the users.
Otherwise, the rank will contain all unrated items of the particular users.
By default the top-10 ranking is computed for each user
Via the methodology
parameter you can perform different candidate item selection. By default, the
TestRatingsMethodology()
is used: so, for each user, items in its test set only will be ranked
If the algorithm was not fit for some users, they will be skipped and a warning message is printed showing the number of users for which the alg couldn't produce a ranking
PARAMETER | DESCRIPTION |
---|---|
test_set |
Ratings object which represents the ground truth of the split considered
TYPE:
|
n_recs |
Number of the top items that will be present in the ranking of each user.
If |
user_list |
List of users for which you want to compute score prediction. If None, the ranking
will be computed for all users of the |
methodology |
TYPE:
|
num_cpus |
number of processors that must be reserved for the method. If set to
TYPE:
|
RAISES | DESCRIPTION |
---|---|
NotFittedAlg
|
Exception raised when this method is called without first calling the |
RETURNS | DESCRIPTION |
---|---|
Rank
|
Rank object containing recommendation lists for all users of the test set or for all users in |
Source code in clayrs/recsys/recsys.py
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 |
|