Skip to main content

fantasy-ga

Downloads Downloads

fantasy-ga is a Python module and a command line tool that uses the genetic algorithm to automate the generation of fantasy sports lineups. Currently supported platforms and leagues are as follows.

NBA NFL MLB NHL
DraftKings ✅ ✅ ✅ ✅

Installation

Dependencies: numpy

pip install fantasy-ga

Usage

Python

LineupGenerator class supports csv files exported from daily fantasy sports platforms for a given contest.

from fantasy_ga import LineupGenerator
from fantasy_ga.configs import Site, League, ModelConfig, ContestConfig

data_path = "examples/DraftKings/NBA/DKSalaries.csv"
export_path = "examples/DraftKings/NBA/export.csv"

cc = ContestConfig(Site.DK, League.NBA)
mc = ModelConfig(
    # initial population of random lineups
    n_pop = 1000
    # number of evolutions to itererate breeding and mutation for
    n_gen = 16
    # number of children lineups to choose from two best lineups
    n_breed = 30
    # number of random mutations for each evolution
    n_mutate = 30
    # number of compound evolutions with additional random lineups
    n_compound = 5
)

model = LineupGenerator(cc, mc)
model.read_csv(data_path)
model.fit()
# If top_n is not specified, it will save max(500, number of total lineups) lineups sorted by scores
model.export_csv(export_path, top_n=3)

lineups, scores = model.get_top_n_lineups(1)
print(
    f"""
    [Best Lineup]
    Players: {[model.id_to_name[id] for id in lineups[0]]} 
    Salary Total: {sum([model.id_to_salary[id] for id in lineups[0]])}
    Expected FPTS: {scores[0]}
    """
)

Using custom numpy.array for player data

Alternatively, you can provide a numpy.array where the first 3 columns correspond to player ID, salary, fantasy points (FPTS), followed by position information. For instance, the columns correspond to id,salary,fpts,PG,SG,SF,PF,C,G,F,UTIL for DraftKings Fantasy Basketball.

If you would like to use custom numpy array for data matrix instead of csv files, you can do so by using the set_matrix() method. For example

cc = ContestConfig(Site.DK, League.NBA)
mc = ModelConfig() # use default
m = np.array(
    [
        [0, 6600, 36.46503, 0, 0, 0, 1, 1, 0, 1, 1],
        [1, 4200, 26.760368, 0, 0, 1, 1, 0, 0, 1, 1],
        [2, 3000, 4.38538, 1, 1, 0, 0, 0, 1, 0, 1],
        [3, 5000, 27.175564, 0, 0, 0, 0, 1, 0, 0, 1],
        [4, 3400, 16.734577, 0, 1, 1, 0, 0, 1, 1, 1],
        [5, 5900, 3.4382372, 0, 1, 1, 0, 0, 1, 1, 1],
        [6, 3000, -0.18490964, 1, 1, 0, 0, 0, 1, 0, 1],
        [7, 3000, 11.075589, 0, 0, 1, 1, 0, 0, 1, 1],
        [8, 3000, 6.469783, 0, 0, 0, 0, 1, 0, 0, 1],
        [9, 3000, 8.459954, 0, 0, 0, 0, 1, 0, 0, 1],
        [10, 5700, 33.98281, 0, 0, 0, 1, 1, 0, 1, 1],
    ]
)
model = LineupGenerator(cc, mc)
model.set_matrix(m)
model.fit()

Running individual steps of the Genetic Algorithm

LineupGenerator class has two core methods which return optimised lineups. breed() method chooses the best two lineups according to the sum of fantasy points with valid positions and swap players randomly (creating children lineups). mutate() method randomly swaps players where applicable. fit() method wraps around those methods such that those operations are done for multiple generations with additional random lineups.

CLI

As a Python module

$ python -m fantasy_ga --data_path=examples/DraftKings/NBA/DKSalaries.csv --export_path=examples/DraftKings/NBA/lineups.csv --site=DraftKings --league=NBA --n_pop=100 --n_gen=5 --n_breed=100 --n_mutate=100 --n_compound=10 --top_n_lineups=3

or a CLI command

$ fantasy-ga --data_path=examples/DraftKings/NBA/DKSalaries.csv --export_path=examples/DraftKings/NBA/lineups.csv --site=DraftKings --league=NBA --n_pop=100 --n_gen=5 --n_breed=100 --n_mutate=100 --n_compound=10 --top_n_lineups=3

which generates

Saved top 3 lineups into "examples/DraftKings/NBA/lineups.csv".

[Best Lineup]
Players: ['Reggie Jackson', 'Max Strus', 'Anthony Edwards', "Royce O'Neale", 'Nikola Jokic', 'Dejounte Murray', 'John Collins', 'Jarrett Allen']
Salary Total: 50000
Expected FPTS: 268.13

Metadata

Release files for fantasy-ga 0.4.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fantasy-ga 0.4.3
File Size Uploaded
fantasy-ga-0.4.3.tar.gz 19.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fantasy-ga 0.4.3
File Interpreter ABI Platform
fantasy_ga-0.4.3-py3-none-any.whl Python 3 none any Details

Total release size: 35.5 kB

Release files / fantasy-ga-0.4.3.tar.gz

Download URL fantasy-ga-0.4.3.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f6809e015de6b46b1a398248b1467c34a8592ba950bf5419b3cfe4088a03085a
BLAKE2b-256 checksum
How to use checksums
a48573d7d317f8af4a6db69a4051f00f4f2c2cb55a11ca47e1db5c3eee04c463
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release files / fantasy_ga-0.4.3-py3-none-any.whl

Download URL fantasy_ga-0.4.3-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a132aea23ad1aaffb7287a9ab523b1135a699f660ffb111c9e25d9613d512803
BLAKE2b-256 checksum
How to use checksums
4f8c07f72a5ec71974da2ad78229c2b97d16e9cdadb7439290dbc8762e41562c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.4.3 This release

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page