ddop
is a Python library for data-driven operations management. The
goal of ddop
is to provide well-established data-driven operations
management tools within a programming environment that is accessible and
easy to use even for non-experts. At the current state ddop
contains
well known data-driven newsvendor models, a set of performance metrics
that can be used for model evaluation and selection, as well as datasets
that are useful to quickly illustrate the behavior of the various
algorithms implemented in ddop
or as benchmark for testing new models.
Through its consistent and easy-to-use interface one can run and compare
provided models with only a few lines of code.
ddop
is available via PyPI using:
The installation of this package requires the following dependencies:
- numpy==1.18.2
- scipy==1.4.1
- pandas==1.1.4
- statsmodels==0.11.1
- scikit-learn==0.23.0
- tensorflow==2.4.1
- pulp==2.0
- mpmath
Note: The package is actively developed, and conflicts with other
packages may occur during installation. To avoid any installation
conflicts, we recommend installing the package in an empty environment
with the above-mentioned dependencies.
ddop
provides a varity of newsvendor models. The following example
shows how to use one of these models for decision making. It assumes a
very basic knowledge of data-driven operations management practices.
As first step we initialize the model we want to use. In this example
RandomForestWeightedNewsvendor
.
from ddop2.newsvendor import RandomForestWeightedNewsvendor
rf_nv = RandomForestWeightedNewsvendor(cu=2, co=1)
2023-08-09 22:10:47.225307: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-08-09 22:10:48.239997: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
A model can take a set of parameters, each describing the model or the
optimization problem it tries to solve. Here we set the underage costs
cu to 2 and the overage costs co to 1.
As next step we load the Yaz Dataset and split it into train and test
set.
from ddop2.datasets import load_yaz
from sklearn.model_selection import train_test_split
X, y = load_yaz(one_hot_encoding=True, return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle=False, random_state=0)
After the model is initialized, the fit
method can be used to learn a
decision model from the training data X_train
, y_train
.
rf_nv.fit(X_train, y_train)
<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: "▸";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: "▾";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: "";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: scikit-learn/scikit-learn#21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style>
RandomForestWeightedNewsvendor(co=1, cu=2)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
RandomForestWeightedNewsvendor
RandomForestWeightedNewsvendor(co=1, cu=2)
We can then use the predict method to make a decision for new data
samples.
array([[ 5, 4, 14, ..., 23, 35, 22],
[ 6, 6, 11, ..., 26, 37, 23],
[ 8, 8, 16, ..., 35, 55, 40],
...,
[ 5, 6, 12, ..., 23, 41, 25],
[ 6, 6, 13, ..., 24, 41, 32],
[ 8, 9, 15, ..., 34, 57, 42]])
To get a representation of the model’s decision quality we can use the
score
function, which takes as input X_test
and y_test
. The
score
function makes a decision for each sample in X_test
and
calculates the negated average costs with respect to the true values
y_test
and the overage and underage costs.
rf_nv.score(X_test, y_test)