Welcome UFCPredictor documentation
Data processor
Data processing module for UFC fight data.
Provides classes to prepare and normalize data for model training and evaluation. Handles data transformation, normalization, and feature engineering.
DataProcessor
A data processor class designed to prepare and normalize UFC fight data for training and testing neural network models.
This class provides a way to handle and transform raw data into a format suitable for model training and evaluation.
The DataProcessor is designed to work with the dataset classes in ufcpredictor.datasets to provide a seamless data preparation workflow.
Source code in ufcpredictor/data_processor.py
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 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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
|
aggregated_fields: List[str]
property
The fields that are aggregated over the fighter's history.
This property returns all the statistic names, including the ones with "_opponent" appended to represent the opponent's statistics. It also returns the aggregated fields added by the data enhancers.
Returns:
Type | Description |
---|---|
List[str]
|
A list of strings, the names of the aggregated fields. |
normalized_fields: List[str]
property
The fields that are normalized over the fighter's history.
These fields are normalized in the sense that they are divided by their mean value in the history of the fighter. This is done to reduce the effect of outliers and to make the data more comparable between different fighters.
The fields normalized are:
- "age"
- "time_since_last_fight"
- "fighter_height_cm"
- "weight",
- All the aggregated fields (see :meth:aggregated_fields
),
and the same fields with "_per_minute" and "_per_fight" appended,
which represent the aggregated fields per minute and per fight,
respectively.
It also returns the normalized fields added by the data enhancers.
Returns:
Type | Description |
---|---|
List[str]
|
A list of strings, the names of the normalized fields. |
round_stat_names: List[str]
property
The names of the round statistics.
This property returns the names of the columns in the rounds data that are not in ["fight_id", "fighter_id", "round"]. It also returns the same names with "_opponent" appended, to represent the opponent's statistics.
Returns:
Type | Description |
---|---|
List[str]
|
A list of strings, the names of the round statistics. |
stat_names: List[str]
property
The names of the statistics.
This property returns the names of the columns in the rounds data that are not in ["fight_id", "fighter_id", "round"]. It also returns the same names with "_opponent" appended, to represent the opponent's statistics, and the names of the columns "KO", "Sub" and "win", which are the result of the fight, with "_opponent" appended to represent the opponent's result.
Returns:
Type | Description |
---|---|
List[str]
|
A list of strings, the names of the statistics. |
__init__(data_folder=None, ufc_scraper=None, bfo_scraper=None, data_aggregator=None, data_enhancers=[])
Constructor for DataProcessor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_folder
|
Optional[Path | str]
|
The folder containing the data. |
None
|
ufc_scraper
|
Optional[UFCScraper]
|
The scraper to use for ufc data. |
None
|
bfo_scraper
|
Optional[BestFightOddsScraper]
|
The scraper to use for best fight odds data. |
None
|
data_aggregator
|
Optional[DataAggregator]
|
The data aggregator to use for aggregating data. |
None
|
data_enhancers
|
List[DataEnhancer]
|
The list of data enhancers to apply to the data. |
[]
|
Raises:
Type | Description |
---|---|
ValueError
|
If data_folder is None and both ufc_scraper and bfo_scraper are None. |
Source code in ufcpredictor/data_processor.py
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 |
|
add_key_stats(data)
staticmethod
Add key statistics to the dataframe.
This function adds columns to the dataframe indicating whether a fighter has won a fight via KO, submission or decision, and whether the opponent has won a fight via KO, submission or decision. It also adds a column indicating the age of the fighter at the time of the fight.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The dataframe to be processed. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
The dataframe with the added columns. |
Source code in ufcpredictor/data_processor.py
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 |
|
add_per_minute_and_fight_stats()
Add two new columns to the aggregated data for each statistic.
The first column is the statistic per minute, computed by dividing the statistic by the total time in the octagon. The second column is the statistic per fight, computed by dividing the statistic by the number of fights.
The new columns are named
Returns:
Type | Description |
---|---|
None
|
None |
Source code in ufcpredictor/data_processor.py
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 |
|
aggregate_data()
Aggregate the data by combining the round statistics over the history of the fighters.
The aggregated data is stored in the attribute data_aggregated.
The specific implementation depends on the DataAggregator used.
Source code in ufcpredictor/data_processor.py
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
|
apply_filters(data)
staticmethod
Apply filters to the dataframe.
This function applies filters to the dataframe to remove fights: - Before 2008, 8, 1, since I don't have odds for these - With non-standard fight formats (time_format not in ["3 Rnd (5-5-5)", "5 Rnd (5-5-5-5-5)"]) - With female fighters (gender not in ["M"]) - With disqualified or doctor's stoppage results (result not in ["Decision", "KO/TKO", "Submission"]) - With draws or invalid winners (winner not in ("Draw", "NC") or winner.isna())
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The dataframe to be processed. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
The dataframe with the applied filters. |
Source code in ufcpredictor/data_processor.py
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 426 |
|
convert_odds_to_decimal(data)
staticmethod
Convert odds from American format to decimal format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The dataframe with the odds in American format. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
The dataframe with the odds in decimal format. |
Source code in ufcpredictor/data_processor.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
|
fill_weight(data)
staticmethod
Fill the weight column using the weight_class column and the weight_dict.
The weight_dict is a dictionary mapping the weight classes to their corresponding weights in lbs. The weights are then filled in the weight column according to the weight classes in the weight_class column.
This function also removes rows with null weight classes, or open weight or catch weight (agreed weight outside a weight class).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The dataframe to be processed. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
The dataframe with the weight column filled. |
Source code in ufcpredictor/data_processor.py
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 |
|
fix_date_and_time_fields(data)
staticmethod
Fix date and time fields in the dataframe.
This function takes care of converting control time, finish time and total time from minutes to seconds. It also converts the event date and fighter date of birth to datetime objects.
The dataframe is then sorted by fighter id and event date.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The dataframe to be processed. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
The dataframe with the fields fixed. |
Source code in ufcpredictor/data_processor.py
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 |
|
get_fighter_id(name)
Returns the id of the fighter with the given name. Search is performed using fuzzywuzzy. If multiple matches are found, the first one is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the fighter. |
required |
Returns:
Type | Description |
---|---|
str
|
The id of the fighter. |
Source code in ufcpredictor/data_processor.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
get_fighter_name(id_)
Returns the name of the fighter with the given id.
Args:cla id_: The id of the fighter.
Returns:
Type | Description |
---|---|
str
|
The name of the fighter. |
Source code in ufcpredictor/data_processor.py
115 116 117 118 119 120 121 122 123 124 125 |
|
group_round_data(data)
Group the round data by the fixed fields and sum the round statistics.
The fixed fields are the columns in the data that are not in the round statistics and not in ["round"]. The round statistics are the columns in the data that are in the round statistics and not in ["round"].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The data to be grouped. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
The grouped data, with the round statistics summed. |
Source code in ufcpredictor/data_processor.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
|
join_dataframes()
Joins all the relevant dataframes (fight, fighter, event, and odds).
It duplicates the current fight data to create two rows per match, one row for each fighter, and assigns fighter and opponent to each other. Then, it merges the fighter data, round data, and odds data to the previous table. Finally, it adds the date of the event to the dataframe.
Returns:
Type | Description |
---|---|
DataFrame
|
The joined dataframe. |
Source code in ufcpredictor/data_processor.py
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 |
|
load_data()
Loads and processes all the data.
First, it joins all the relevant dataframes (fight, fighter, event, and odds). Then, it fixes the date and time fields, converts the odds to decimal format, fills the weight for each fighter (if not available), adds key statistics (KO, Submission, and Win), and applies filters to the data. Finally, it groups the round data by fighter and fight, and assigns the result to the data attribute.
This method should be called before any other method.
Source code in ufcpredictor/data_processor.py
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 |
|
normalize_data()
Normalize the aggregated data by dividing each column by its mean.
This is done so that the data is more comparable between different fighters. The fields normalized are the ones in normalized_fields.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in ufcpredictor/data_processor.py
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
|
Datasets
This module contains dataset classes designed to handle UFC fight data for training and testing neural network models.
The dataset classes provide a structured way to store and retrieve data for fighter characteristics, fight outcomes, and odds. They are designed to work with the DataProcessor class to prepare and normalize the data.
BasicDataset
Bases: Dataset
A basic dataset class designed to handle UFC fight data for training and testing neural network models.
This class provides a simple way to store and retrieve data for fighter characteristics, fight outcomes, and odds. It is designed to be used with the SymmetricFightNet model and other UFC prediction models.
Source code in ufcpredictor/datasets.py
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 |
|
__getitem__(idx)
Returns a tuple of (X, Y, winner, odds_1, odds_2) for the given index.
The data is randomly flipped to simulate the possibility of a fight being between two fighters in either order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idx
|
int
|
The index of the data to return. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tuple of (X, Y, winner, odds_1, odds_2) where X and Y are the |
Tensor
|
input data for the two fighters, winner is a tensor of size 1 |
Tensor
|
indicating which fighter won, and odds_1 and odds_2 are the opening |
Tensor
|
odds for the two fighters. |
Source code in ufcpredictor/datasets.py
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 |
|
__init__(data_processor, fight_ids, X_set=None, Xf_set=None)
Constructor for ForecastDataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_processor
|
DataProcessor
|
The DataProcessor instance that contains the data. |
required |
fight_ids
|
List[str]
|
The list of fight ids to include in the dataset. |
required |
X_set
|
Optional[List[str]]
|
The list of columns to include in the dataset. If None, use all |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If some columns are not found in the normalized data. |
Source code in ufcpredictor/datasets.py
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 |
|
__len__()
Returns the size of the dataset.
Returns:
Type | Description |
---|---|
int
|
The size of the dataset. |
Source code in ufcpredictor/datasets.py
209 210 211 212 213 214 215 |
|
get_fight_data_from_ids(fight_ids=None)
Returns a tuple of (X, Y, winner, odds_1, odds_2, fighter_names, opponent_names) for the given fight ids.
If fight_ids is None, returns all the data in the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fight_ids
|
Optional[List[str]]
|
The list of fight ids to include in the dataset. If None, use all the data in the dataset. |
None
|
Returns:
Type | Description |
---|---|
FloatTensor
|
A tuple of (X, Y, winner, odds_1, odds_2, fighter_names, opponent_names) |
FloatTensor
|
where X and Y are the input data for the two fighters, winner is a tensor |
FloatTensor
|
of size 1 indicating which fighter won, and odds_1 and odds_2 are the |
FloatTensor
|
opening odds for the two fighters. fighter_names and opponent_names are |
FloatTensor
|
the names of the fighters and their opponents. |
Source code in ufcpredictor/datasets.py
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 |
|
load_data()
Loads the data into a format that can be used to train a model.
The data is first reduced to only include the columns specified in X_set. Then, the stats are shifted to get the stats prior to each fight. The data is then merged with itself to get one row per match with the data from the two fighters. The matchings of the fighter with itself are removed and only one row per match is kept. Finally, the data is loaded into torch tensors.
Source code in ufcpredictor/datasets.py
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 |
|
ForecastDataset
Bases: Dataset
A dataset class designed to handle forecasting data for UFC fight predictions.
This class provides a structured way to store and retrieve data for training and testing neural network models. It is designed to work with the DataProcessor class to prepare and normalize the data.
Source code in ufcpredictor/datasets.py
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 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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
|
__init__(data_processor, X_set=None, Xf_set=None)
Constructor for ForecastDataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_processor
|
DataProcessor
|
The DataProcessor instance that contains the data. |
required |
X_set
|
Optional[List[str]]
|
The list of columns to include in the dataset. If None, use all columns. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If some columns are not found in the normalized data. |
Source code in ufcpredictor/datasets.py
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 |
|
get_forecast_prediction(fighter_names, opponent_names, event_dates, fighter_odds, opponent_odds, model, fight_features=[], parse_ids=False, device='cpu')
Make a prediction for a given list of matches. Either providing the names of the fighters and their opponents, or providing the ids of the fighters and their opponents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fighters_names
|
The list of fighters names. |
required | |
opponent_names
|
List[str]
|
The list of opponent names. |
required |
event_dates
|
List[str | date]
|
The list of event dates. |
required |
fighter_odds
|
List[float]
|
The list of fighter odds. |
required |
opponent_odds
|
List[float]
|
The list of opponent odds. |
required |
model
|
Module
|
The model to make the prediction with. |
required |
parse_ids
|
bool
|
Whether to parse the ids of the fighters and opponents. Ids are parsed in fields "fighter_names" and "opponent_names"if True, and names are parsed if False. |
False
|
device
|
str
|
The device to use for the prediction. |
'cpu'
|
Returns:
Type | Description |
---|---|
NDArray
|
A tuple of two numpy arrays, each containing the predictions for one of the |
NDArray
|
fighters. |
Source code in ufcpredictor/datasets.py
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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
|
get_single_forecast_prediction(fighter_name, opponent_name, event_date, odds1, odds2, model, fight_features=[], parse_ids=False)
Make a prediction for a single match. Either providing the names of the fighters and their opponents, or providing the ids of the fighters and their opponents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fighter_name
|
str
|
The name of the fighter. |
required |
opponent_name
|
str
|
The name of the opponent. |
required |
event_date
|
str | date
|
The date of the fight. |
required |
odds1
|
int
|
The odds of the first fighter. |
required |
odds2
|
int
|
The odds of the second fighter. |
required |
model
|
Module
|
The model to make the prediction with. |
required |
parse_ids
|
bool
|
Whether to parse the ids of the fighters and opponents. Ids are parsed in fields "fighter_name" and "opponent_name"if True, and names are parsed if False. |
False
|
Returns: The predicted odds for the first and second fighters.
Source code in ufcpredictor/datasets.py
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 |
|
Loss Functions
This module contains loss functions designed to train neural network models to predict the outcome of UFC fights.
The loss functions take into account the predictions made by the model and the actual outcomes of the fights, and are used to optimize the model's performance.
BettingLoss
Bases: Module
Source code in ufcpredictor/loss_functions.py
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 |
|
__init__(max_bet=10)
Initializes the BettingLoss instance.
This function calls the constructor of the parent class and performs no other actions.
Source code in ufcpredictor/loss_functions.py
26 27 28 29 30 31 32 33 34 |
|
forward(predictions, targets, odds_1, odds_2)
Computes the betting loss for the given predictions and targets.
This function takes a tensor of predictions between 0 and 1, a tensor of targets (0 or 1), and two tensors of odds. It returns a tensor with the computed betting loss, which is the mean of the losses minus the earnings, this is the net profit.
The betting loss returned is the negative profit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions
|
Tensor
|
A tensor of predictions between 0 and 1. |
required |
targets
|
Tensor
|
A tensor of targets (0 or 1). |
required |
odds_1
|
Tensor
|
A tensor of odds for fighter 1. |
required |
odds_2
|
Tensor
|
A tensor of odds for fighter 2. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor with the computed betting loss. |
Source code in ufcpredictor/loss_functions.py
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 |
|
get_bet(prediction)
Computes the bet for the given prediction.
This function takes a prediction between 0 and 1 and returns the corresponding bet between 0 and 20. The bet is computed as the prediction times 2 times 10. This is just an approximation that seems to work well.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prediction
|
Tensor | float
|
A tensor or float between 0 and 1 representing a prediction. |
required |
Returns:
Type | Description |
---|---|
Tensor | float
|
A tensor or float between 0 and 20 representing the bet. |
Source code in ufcpredictor/loss_functions.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
Models
This module contains neural network models designed to predict the outcome of UFC fights.
The models take into account various characteristics of the fighters and the odds of the fights, and can be used to make predictions on the outcome of a fight and to calculate the benefit of a bet.
FighterNet
Bases: Module
A neural network model designed to predict the outcome of a fight based on a single fighter's characteristics.
The model takes into account the characteristics of the fighter and the odds of the fight. It can be used to make predictions on the outcome of a fight and to calculate the benefit of a bet.
Source code in ufcpredictor/models.py
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 |
|
__init__(input_size, dropout_prob=0.0, network_shape=[128, 256, 512, 256, 127])
Initialize the FighterNet model with the given input size and dropout probability.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_size
|
int
|
The size of the input to the model. |
required |
dropout_prob
|
float
|
The probability of dropout. |
0.0
|
network_shape
|
List[int]
|
Shape of the network layers (except input layer). |
[128, 256, 512, 256, 127]
|
Source code in ufcpredictor/models.py
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 |
|
forward(x)
Compute the output of the model given the input tensor x.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor to the model. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The output of the model. |
Source code in ufcpredictor/models.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
SimpleFightNet
Bases: Module
A neural network model designed to predict the outcome of a fight between two fighters.
The model takes into account the characteristics of both fighters and the odds of the fight. It combines the features of both fighters as an input to the model.
The model can be used to make predictions on the outcome of a fight and to calculate the benefit of a bet.
Source code in ufcpredictor/models.py
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 |
|
__init__(input_size, dropout_prob=0.0, network_shape=[1024, 512, 256, 128, 64, 1])
Initialize the SimpleFightNet model with the given input size and dropout probability.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dropout_prob
|
float
|
The probability of dropout. |
0.0
|
network_shape
|
List[int]
|
Shape of the network layers (except input layer). |
[1024, 512, 256, 128, 64, 1]
|
Source code in ufcpredictor/models.py
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 |
|
forward(X1, X2, X3, odds1, odds2)
Compute the output of the SimpleFightNet model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X1
|
Tensor
|
The input tensor for the first fighter. |
required |
X2
|
Tensor
|
The input tensor for the second fighter. |
required |
X3
|
Tensor
|
The input tensor for the fight features. |
required |
odds1
|
Tensor
|
The odds tensor for the first fighter. |
required |
odds2
|
Tensor
|
The odds tensor for the second fighter. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The output of the SimpleFightNet model. |
Source code in ufcpredictor/models.py
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 |
|
SymmetricFightNet
Bases: Module
A neural network model designed to predict the outcome of a fight between two fighters.
The model takes into account the characteristics of both fighters and the odds of the fight. It uses a symmetric architecture to ensure that the model is fair and unbiased towards either fighter.
The model can be used to make predictions on the outcome of a fight and to calculate the benefit of a bet.
Source code in ufcpredictor/models.py
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 |
|
__init__(input_size, input_size_f, dropout_prob=0.0, network_shape=[512, 128, 64, 1], fighter_network_shape=None)
Initialize the SymmetricFightNet model with the given input size and dropout probability.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_size
|
int
|
The size of the input to the model. |
required |
dropout_prob
|
float
|
The probability of dropout. |
0.0
|
network_shape
|
List[int]
|
Shape of the network layers (except input layer). |
[512, 128, 64, 1]
|
fighter_network_shape
|
Optional[List[int]]
|
Shape of the network layers for the fighter network (except input layer). |
None
|
Source code in ufcpredictor/models.py
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 |
|
forward(X1, X2, X3, odds1, odds2)
Compute the output of the SymmetricFightNet model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X1
|
Tensor
|
The input tensor for the first fighter. |
required |
X2
|
Tensor
|
The input tensor for the second fighter. |
required |
X3
|
Tensor
|
The input tensor for the fight features. |
required |
odds1
|
Tensor
|
The odds tensor for the first fighter. |
required |
odds2
|
Tensor
|
The odds tensor for the second fighter. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The output of the SymmetricFightNet model. |
Source code in ufcpredictor/models.py
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 |
|
Trainer
This module provides a Trainer class for training and testing PyTorch models using a specific workflow.
The Trainer class encapsulates the training and testing data, model, optimizer, loss function, and learning rate scheduler, providing a simple way to train and test a PyTorch model.
Trainer
Trainer class for training and testing a PyTorch model.
This class provides a simple way to train and test a PyTorch model using a specific training and testing workflow.
Attributes:
Name | Type | Description |
---|---|---|
train_loader |
DataLoader
|
A DataLoader for the training data. |
test_loader |
DataLoader
|
A DataLoader for the test data. |
model |
Module
|
The model to be trained. |
optimizer |
Optimizer
|
The optimizer to be used. |
loss_fn |
Module
|
The loss function to be used. |
scheduler |
Optional[ReduceLROnPlateau]
|
The learning rate scheduler to be used. |
device |
str | device
|
The device to be used for training. Defaults to "cpu". |
Source code in ufcpredictor/trainer.py
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 |
|
__init__(train_loader, model, optimizer, loss_fn, test_loader=None, scheduler=None, device='cpu', mlflow_tracking=False)
Initialize the Trainer object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_loader
|
DataLoader
|
A DataLoader for the training data. |
required |
test_loader
|
Optional[DataLoader]
|
A DataLoader for the test data. |
None
|
model
|
Module
|
The model to be trained. |
required |
optimizer
|
Optimizer
|
The optimizer to be used. |
required |
loss_fn
|
Module
|
The loss function to be used. |
required |
scheduler
|
Optional[ReduceLROnPlateau]
|
The learning rate scheduler to be used. |
None
|
device
|
str | device
|
The device to be used for training. Defaults to "cpu". |
'cpu'
|
Source code in ufcpredictor/trainer.py
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 |
|
test(test_loader=None, silent=False)
Evaluates the model on the test data and returns the validation loss, target F1 score, proportion of correct predictions, target predictions, and target labels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
test_loader
|
DataLoader | None
|
The DataLoader for the test data. Defaults to the DataLoader passed to the Trainer constructor. |
None
|
silent
|
bool
|
Whether to not print training progress. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
float
|
A tuple containing the validation loss, target F1 score, proportion of correct |
float
|
predictions, target predictions, and target labels. |
Source code in ufcpredictor/trainer.py
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 |
|
train(train_loader=None, test_loader=None, epochs=10, silent=False)
Train the model for a given number of epochs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_loader
|
DataLoader | None
|
The DataLoader for the training data. Defaults to the DataLoader passed to the Trainer constructor. |
None
|
test_loader
|
DataLoader | None
|
The DataLoader for the test data. Defaults to the DataLoader passed to the Trainer constructor. |
None
|
epochs
|
int
|
The number of epochs to train for. Defaults to 10. |
10
|
silent
|
bool
|
Whether to not print training progress. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in ufcpredictor/trainer.py
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 |
|
Plot tools
This module provides tools for plotting and visualizing predictions made by UFC predictor models.
PredictionPlots
Provides tools for visualizing and analyzing the predictions made by UFC predictor models.
This class contains methods for displaying the prediction details of a fight, including the prediction, shift, odds, and correctness. It also calculates and displays the total invested, earnings, number of bets, and number of fights. Additionally, it can show a plot of the benefit of the model over time.
Source code in ufcpredictor/plot_tools.py
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 |
|
plot_single_prediction(model, dataset, fighter_name, opponent_name, fight_features, event_date, odds1, odds2, ax=None, parse_id=False)
staticmethod
Plots the prediction for a single fight.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
The model to use to make predictions. |
required | |
dataset
|
The dataset to use to get the data. |
required | |
fighter_name
|
The name of the first fighter. |
required | |
opponent_name
|
The name of the second fighter. |
required | |
event_date
|
The date of the fight. |
required | |
odds1
|
The odds for the first fighter (decimal). |
required | |
odds2
|
The odds for the second fighter (decimal). |
required | |
ax
|
The axes to use to show the plot. If None, a new figure will be created. |
None
|
|
parse_id
|
If True, the id of the fighters is parsed instead of the name. |
False
|
Source code in ufcpredictor/plot_tools.py
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 |
|
show_fight_prediction_detail(model, data, print_info=False, show_plot=False, ax=None, device='cpu')
staticmethod
Shows the prediction detail of a fight and the benefit of the model.
It prints the prediction, shift, odd1, odd2, and correct for each fight. It also prints the total invested, earnings, number of bets and number of fights. Finally, it prints the benefit of the model as a percentage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
The model to use to make predictions. |
required | |
data
|
The data to use to make predictions. It should contain the fighter and opponent data, the label, the odds and the names of the fighters. |
required | |
print_info
|
If True, print the prediction, shift, odd1, odd2, and correct for each fight. If False, do not print anything. |
False
|
|
show_plot
|
If True, show a plot of the benefit of the model over time. |
False
|
|
ax
|
The axes to use to show the plot. If None, a new figure will be created. |
None
|
Source code in ufcpredictor/plot_tools.py
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 |
|
show_fight_prediction_detail_from_dataset(model, dataset, fight_ids=None, print_info=False, show_plot=False, ax=None, device='cpu')
staticmethod
Shows the prediction detail of a fight and the benefit of the model.
It uses the dataset to get the data for the specified fight ids. It then calls show_fight_prediction_detail with the model and the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
The model to use to make predictions. |
required | |
dataset
|
The dataset to use to get the data. |
required | |
fight_ids
|
The id of the fight to use. If None, it will use all the data in the dataset. |
None
|
|
print_info
|
If True, print the prediction, shift, odd1, odd2, and correct for each fight. If False, do not print anything. |
False
|
|
show_plot
|
If True, show a plot of the benefit of the model over time. |
False
|
|
ax
|
The axes to use to show the plot. If None, a new figure will be created. |
None
|
Source code in ufcpredictor/plot_tools.py
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 |
|
Utils
Utility functions for the UFC predictor project.
This module contains various utility functions used throughout the project, including functions for converting between different time and odds formats, as well as other miscellaneous helper functions.
convert_minutes_to_seconds(time_str)
Convert a time string from minutes:seconds format to seconds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_str
|
str
|
Time string in minutes:seconds format. |
required |
Returns:
Type | Description |
---|---|
Optional[int]
|
Time in seconds. If the input string is "--", returns 0. If the input is None or "NULL", or if the input is NaN, returns None. |
Source code in ufcpredictor/utils.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
convert_odds_to_decimal(odds)
Convert odds from American format to decimal format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
odds
|
List[int | float] | NDArray[float64 | int_]
|
Odds in American format. |
required |
Returns:
Type | Description |
---|---|
NDArray[float64]
|
Odds in decimal format. |
Source code in ufcpredictor/utils.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
convert_odds_to_moneyline(odds)
Convert odds from decimal format to moneyline format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
odds
|
NDArray[float64] | List[float]
|
Odds in decimal format. |
required |
Returns:
Type | Description |
---|---|
NDArray[int_]
|
Odds in moneyline format. |
Source code in ufcpredictor/utils.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|