Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions Chapter01/forecasting/forecasting_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ def plot_store_data(df: pd.DataFrame) -> None:
fig, ax = plt.subplots(figsize=(20,10))
df.plot(x='ds', y='y', ax=ax)
ax.set_xlabel('Date')
ax.set_ylabel('Volume')
ax.set_ylabel('Sales')
ax.legend(['Truth'])
current_ytick_values = plt.gca().get_yticks()
plt.gca().set_yticklabels(['{:,.0f}'.format(x) for x in current_ytick_values])
plt.savefig('store_data.png')



def train_predict(
df: pd.DataFrame,
train_fraction: float,
Expand Down Expand Up @@ -74,17 +76,17 @@ def plot_forecast(df_train: pd.DataFrame, df_test: pd.DataFrame, predicted: pd.D
x='ds',
y='yhat',
ax=ax,
label='Prediction',
label='Prediction + 95% CI',
linewidth=2,
markersize=5,
color='k'
color='red'
)
ax.fill_between(
x=predicted['ds'],
y1=predicted['yhat_upper'],
y2=predicted['yhat_lower'],
alpha=0.1,
color='k'
alpha=0.15,
color='red',
)
df_train.iloc[train_index-100:].plot(
x='ds',
Expand All @@ -99,6 +101,7 @@ def plot_forecast(df_train: pd.DataFrame, df_test: pd.DataFrame, predicted: pd.D
plt.gca().set_yticklabels(['{:,.0f}'.format(x) for x in current_ytick_values])
ax.set_xlabel('Date')
ax.set_ylabel('Sales')
plt.tight_layout()
plt.savefig('store_data_forecast.png')


Expand All @@ -107,29 +110,35 @@ def plot_forecast(df_train: pd.DataFrame, df_test: pd.DataFrame, predicted: pd.D
if __name__ == "__main__":
import os

# If data present, read it in
# If data present, read it in, otherwise, download it
file_path = 'rossman_store_data/train.csv'
if os.path.exists(file_path):
logging.info('Dataset found, reading into pandas dataframe.')
df = pd.read_csv(file_path)
else:
logging.info('Dataset not found, downloading ...')
download_kaggle_dataset()

download_kaggle_dataset()
logging.info('Reading dataset into pandas dataframe.')
df = pd.read_csv(file_path)

# Transform dataset in preparation for feeding to Prophet
df = prep_store_data(df)

# Define main parameters for modelling
seasonality = {
'yearly': True,
'weekly': True,
'daily': False
}


# Calculate the relevant dataframes
predicted, df_train, df_test, train_index = train_predict(
df = df,
train_fraction = 0.8,
seasonality=seasonality
)

# Plot the forecast
plot_forecast(df_train, df_test, predicted)


Expand Down
67 changes: 67 additions & 0 deletions Chapter01/mlewp-chapter01.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,42 @@ dependencies:
- pip:
- aiohttp==3.8.4
- aiosignal==1.3.1
- anyio==3.6.2
- appdirs==1.4.4
- appnope==0.1.3
- argon2-cffi==21.3.0
- argon2-cffi-bindings==21.2.0
- arrow==1.2.3
- asttokens==2.2.1
- async-timeout==4.0.2
- attrs==22.2.0
- backcall==0.2.0
- beautifulsoup4==4.12.0
- bleach==6.0.0
- build==0.10.0
- cachecontrol==0.12.11
- certifi==2022.12.7
- cffi==1.15.1
- charset-normalizer==3.1.0
- cleo==2.0.1
- cmdstanpy==1.1.0
- comm==0.1.3
- contourpy==1.0.7
- convertdate==2.4.0
- crashtest==0.4.1
- cycler==0.11.0
- cython==0.29.33
- debugpy==1.6.6
- decorator==5.1.1
- defusedxml==0.7.1
- distlib==0.3.6
- dulwich==0.21.3
- ephem==4.1.4
- executing==1.2.0
- fastjsonschema==2.16.3
- filelock==3.10.3
- fonttools==4.39.2
- fqdn==1.5.1
- frozenlist==1.3.3
- hijri-converter==2.2.4
- holidays==0.21.13
Expand All @@ -48,54 +64,105 @@ dependencies:
- idna==3.4
- importlib-metadata==6.1.0
- installer==0.7.0
- ipykernel==6.22.0
- ipython==8.11.0
- ipython-genutils==0.2.0
- isoduration==20.11.0
- jaraco-classes==3.2.3
- jedi==0.18.2
- jinja2==3.1.2
- joblib==1.2.0
- jsonpointer==2.3
- jsonschema==4.17.3
- jupyter-client==8.1.0
- jupyter-core==5.3.0
- jupyter-events==0.6.3
- jupyter-server==2.5.0
- jupyter-server-terminals==0.4.4
- jupyterlab-pygments==0.2.2
- kaggle==1.5.13
- keyring==23.13.1
- kiwisolver==1.4.4
- korean-lunar-calendar==0.3.1
- lockfile==0.12.2
- lunarcalendar==0.0.9
- markupsafe==2.1.2
- marshmallow==3.19.0
- matplotlib==3.7.1
- matplotlib-inline==0.1.6
- mistune==2.0.5
- more-itertools==9.1.0
- msgpack==1.0.5
- multidict==6.0.4
- nbclassic==0.5.3
- nbclient==0.7.2
- nbconvert==7.2.10
- nbformat==5.8.0
- nest-asyncio==1.5.6
- notebook==6.5.3
- notebook-shim==0.2.2
- numpy==1.24.2
- packaging==23.0
- pandas==1.5.3
- pandocfilters==1.5.0
- parso==0.8.3
- pexpect==4.8.0
- pickleshare==0.7.5
- pillow==9.4.0
- pkginfo==1.9.6
- platformdirs==2.6.2
- poetry==1.4.1
- poetry-core==1.5.2
- poetry-plugin-export==1.3.0
- prometheus-client==0.16.0
- prompt-toolkit==3.0.38
- prophet==1.1.2
- psutil==5.9.4
- ptyprocess==0.7.0
- pure-eval==0.2.2
- pycparser==2.21
- pygments==2.14.0
- pymeeus==0.5.12
- pyparsing==3.0.9
- pyproject-hooks==1.0.0
- pyrsistent==0.19.3
- python-dateutil==2.8.2
- python-json-logger==2.0.7
- python-slugify==8.0.1
- pytz==2022.7.1
- pyyaml==6.0
- pyzmq==25.0.2
- rapidfuzz==2.13.7
- requests==2.28.2
- requests-toolbelt==0.10.1
- rfc3339-validator==0.1.4
- rfc3986-validator==0.1.1
- scikit-learn==1.2.2
- scipy==1.10.1
- send2trash==1.8.0
- shellingham==1.5.0.post1
- six==1.16.0
- sniffio==1.3.0
- soupsieve==2.4
- stack-data==0.6.2
- terminado==0.17.1
- text-unidecode==1.3
- threadpoolctl==3.1.0
- tinycss2==1.2.1
- tomli==2.0.1
- tomlkit==0.11.6
- tornado==6.2
- tqdm==4.65.0
- traitlets==5.9.0
- trove-classifiers==2023.3.9
- uri-template==1.2.0
- urllib3==1.26.15
- virtualenv==20.21.0
- wcwidth==0.2.6
- webargs==8.2.0
- webcolors==1.12
- webencodings==0.5.1
- websocket-client==1.5.1
- xattr==0.10.1
- yarl==1.8.2
- zipp==3.15.0
Expand Down
1 change: 1 addition & 0 deletions Chapter07/forecast/create-kind-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kind create cluster --name mlewp-chapter07 --config kind-test2-config.yaml
7 changes: 7 additions & 0 deletions Chapter07/forecast/kind-test2-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# three node (two workers) cluster config
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
58 changes: 58 additions & 0 deletions Chapter07/forecast/kserve-basic/kserve_python_sdk_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'''
Based on https://www.kubeflow.org/docs/external-add-ons/kserve/first_isvc_kserve/

'''
from kubernetes import client
from kserve import KServeClient
from kserve import constants
from kserve import utils
from kserve import V1beta1InferenceService
from kserve import V1beta1InferenceServiceSpec
from kserve import V1beta1PredictorSpec
from kserve import V1beta1SKLearnSpec


# MLFLOW


# KUBEFLOW
namespace = utils.get_default_target_namespace()

name='sklearn-iris'
kserve_version='v1beta1'
api_version = constants.KSERVE_GROUP + '/' + kserve_version

isvc = V1beta1InferenceService(api_version=api_version,
kind=constants.KSERVE_KIND,
metadata=client.V1ObjectMeta(
name=name, namespace=namespace, annotations={'sidecar.istio.io/inject':'false'}),
spec=V1beta1InferenceServiceSpec(
predictor=V1beta1PredictorSpec(
sklearn=(V1beta1SKLearnSpec(
storage_uri="gs://kfserving-samples/models/sklearn/iris"))))
)

KServe = KServeClient()
KServe.create(isvc)

KServe.get(name, namespace=namespace, watch=True, timeout_seconds=120)

'''

'''
import requests

isvc_resp = KServe.get(name, namespace=namespace)
isvc_url = isvc_resp['status']['address']['url']

print(isvc_url)

inference_input = {
'instances': [
[6.8, 2.8, 4.8, 1.4],
[6.0, 3.4, 4.5, 1.6]
]
}

response = requests.post(isvc_url, json=inference_input)
print(response.text)
Loading