├── dev-requirements.txt ├── images ├── feature-store-training.png ├── feature_store_diagram.png ├── feature-store-training-v2.png ├── feature-store-vector-line.png ├── feature-store-vector-screen.png ├── feature_store_demo_diagram.png ├── feature_store_how_it_works.png ├── features-catalog-transaction.png └── feature-store-graph.svg ├── requirements.txt ├── .flake8 ├── pyproject.toml ├── .test ├── setup.py ├── src ├── __init__.py ├── get_vector.py ├── serving.py ├── date_adjust.py ├── train_sklearn.py └── train_workflow.py ├── tests └── test_data_prep.py ├── README.md ├── .github └── workflows │ └── ci.yaml ├── Makefile ├── project_setup.py ├── LICENSE ├── 05-real-time-serving-pipeline.ipynb ├── 04-train-test-pipeline.ipynb └── 02-interactive-data-preparation.ipynb /dev-requirements.txt: -------------------------------------------------------------------------------- 1 | pytest 2 | black 3 | isort 4 | flake8 5 | -------------------------------------------------------------------------------- /images/feature-store-training.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlrun/demo-fraud/HEAD/images/feature-store-training.png -------------------------------------------------------------------------------- /images/feature_store_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlrun/demo-fraud/HEAD/images/feature_store_diagram.png -------------------------------------------------------------------------------- /images/feature-store-training-v2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlrun/demo-fraud/HEAD/images/feature-store-training-v2.png -------------------------------------------------------------------------------- /images/feature-store-vector-line.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlrun/demo-fraud/HEAD/images/feature-store-vector-line.png -------------------------------------------------------------------------------- /images/feature-store-vector-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlrun/demo-fraud/HEAD/images/feature-store-vector-screen.png -------------------------------------------------------------------------------- /images/feature_store_demo_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlrun/demo-fraud/HEAD/images/feature_store_demo_diagram.png -------------------------------------------------------------------------------- /images/feature_store_how_it_works.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlrun/demo-fraud/HEAD/images/feature_store_how_it_works.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | scikit-learn 2 | seaborn 3 | pandas==2.1.4 4 | mlrun 5 | redis 6 | s3fs 7 | matplotlib 8 | plotly 9 | graphviz -------------------------------------------------------------------------------- /images/features-catalog-transaction.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlrun/demo-fraud/HEAD/images/features-catalog-transaction.png -------------------------------------------------------------------------------- /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | max-line-length = 85 3 | extend-ignore = E203, W503 4 | 5 | # exclude these dirs 6 | exclude = .git,venv,playground 7 | 8 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.isort] 2 | profile = "black" 3 | multi_line_output = 3 4 | #line_length = 85 5 | 6 | [tool.black] 7 | line-length = 85 8 | -------------------------------------------------------------------------------- /.test: -------------------------------------------------------------------------------- 1 | 01-exploratory-data-analysis.ipynb 2 | 02-interactive-data-preparation.ipynb 3 | 03-ingest-with-feature-store.ipynb 4 | 04-train-test-pipeline.ipynb 5 | 05-real-time-serving-pipeline.ipynb -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import find_packages, setup 2 | 3 | project_name = "demo_fraud" 4 | with open("README.md", "r", encoding="utf-8") as fh: 5 | long_description = fh.read() 6 | 7 | setup( 8 | name=project_name, 9 | packages=[project_name], 10 | package_dir={project_name: "src"}, 11 | version="0.1.0", 12 | description="my desc", 13 | author="Yaron", 14 | author_email="author@example.com", 15 | license="MIT", 16 | long_description=long_description, 17 | long_description_content_type="text/markdown", 18 | python_requires=">=3.7", 19 | ) 20 | -------------------------------------------------------------------------------- /src/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Iguazio 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | -------------------------------------------------------------------------------- /src/get_vector.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Iguazio 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import mlrun 16 | import mlrun.feature_store as fstore 17 | 18 | from mlrun.datastore.targets import ParquetTarget 19 | 20 | 21 | def get_offline_features(feature_vector, features, label_feature): 22 | 23 | fv = fstore.FeatureVector(feature_vector, 24 | features, 25 | label_feature=label_feature, 26 | description='Predicting a fraudulent transaction') 27 | 28 | data = fv.get_offline_features(target=ParquetTarget()) 29 | 30 | return data -------------------------------------------------------------------------------- /tests/test_data_prep.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import pandas as pd 4 | 5 | 6 | class MyTest(unittest.TestCase): 7 | def test_(self): 8 | data = self.get_data() 9 | assert data is not None 10 | 11 | def get_data(self): 12 | return pd.DataFrame( 13 | { 14 | "key": [ 15 | "2009-06-15 17:26:21.0000001", 16 | "2010-01-05 16:52:16.0000002", 17 | "2011-08-18 00:35:00.00000049", 18 | ], 19 | "fare_amount": [4.5, 16.9, 5.7], 20 | "pickup_datetime": [ 21 | "2009-06-15 17:26:21 UTC", 22 | "2010-01-05 16:52:16 UTC", 23 | "2011-08-18 00:35:00 UTC", 24 | ], 25 | "pickup_longitude": [-73.844311, -74.016048, -73.982738], 26 | "pickup_latitude": [40.721319, 40.711303, 40.76127], 27 | "dropoff_longitude": [-73.84161, -73.979268, -73.991242], 28 | "dropoff_latitude": [40.712278, 40.782004, 40.750562], 29 | "passenger_count": [1, 1, 2], 30 | } 31 | ) 32 | -------------------------------------------------------------------------------- /src/serving.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Iguazio 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | 17 | import numpy as np 18 | from cloudpickle import load 19 | from mlrun.serving.v2_serving import V2ModelServer 20 | 21 | 22 | class ClassifierModel(V2ModelServer): 23 | """Model serving classifer example""" 24 | 25 | def load(self): 26 | """load and initialize the model and/or other elements""" 27 | model_file, extra_data = self.get_model(".pkl") 28 | self.model = load(open(model_file, "rb")) 29 | 30 | def predict(self, body: dict) -> list: 31 | """Generate model predictions from sample""" 32 | print(f"Input -> {body['inputs']}") 33 | feats = np.asarray(body["inputs"]) 34 | result: np.ndarray = self.model.predict(feats) 35 | return result.tolist() 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Feature-Store End-to-End Demo 2 | 3 | 4 | This demo shows the usage of MLRun and the feature store. 5 | 6 | > - This demo works with the online feature store, which is currently not part of the Open Source default deployment. 7 | 8 | The demo showcases: 9 | 10 | - [**Data Exploration**](01-exploratory-data-analysis.ipynb) 11 | - [**Offline Data prepare and train**](02-interactive-data-preparation.ipynb) 12 | - [**Data ingestion & preparation**](03-ingest-with-feature-store.ipynb) 13 | - [**Building An Automated ML Pipeline**](04-train-test-pipeline.ipynb) 14 | - [**Model serving**](05-real-time-serving-pipeline.ipynb) 15 | 16 | Fraud prevention specifically is a challenge as it requires processing raw transaction and events in real-time and being able to 17 | quickly respond and block transactions before they occur. Consider, for example, a case where you would like to evaluate the 18 | average transaction amount. When training the model, it is common to take a DataFrame and just calculate the average. However, 19 | when dealing with real-time/online scenarios, this average has to be calculated incrementally. 20 | 21 | In this demo we will learn how to **Ingest** different data sources to our **Feature Store**. Specifically, we will consider 2 types of data: 22 | 23 | - **Transactions**: Monetary activity between 2 parties to transfer funds. 24 | - **Events**: Activity that done by the party, such as login or password change. 25 | 26 | ![](./images/feature_store_demo_diagram.png) 27 | 28 | We will walk through creation of ingestion pipeline for each data source with all the needed preprocessing and validation. We will run the pipeline locally within the notebook and then launch a real-time function to **ingest live data** or schedule a cron to run the task when needed. 29 | 30 | Following the ingestion, we will create a feature vector, select the most relevant features and create a final model. We will then deploy the model and showcase the feature vector and model serving. 31 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - development 7 | push: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | lint: 13 | name: Lint code (Python ${{ matrix.python-version }}) 14 | runs-on: ubuntu-latest 15 | strategy: 16 | matrix: 17 | python-version: [3.9] 18 | steps: 19 | - uses: actions/checkout@v3 20 | - name: Set up python ${{ matrix.python-version }} 21 | uses: actions/setup-python@v4 22 | with: 23 | python-version: ${{ matrix.python-version }} 24 | - uses: actions/cache@v2 25 | with: 26 | path: ~/.cache/pip 27 | key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('**/dev-requirements.txt') }} 28 | restore-keys: | 29 | ${{ runner.os }}-pip-${{ matrix.python-version }}- 30 | ${{ runner.os }}-pip- 31 | - name: Install dependencies 32 | run: | 33 | python -m pip install --upgrade pip~=22.3.0 34 | pip install -r dev-requirements.txt 35 | - name: Lint 36 | run: make lint 37 | 38 | 39 | tests: 40 | name: Run tests (Python ${{ matrix.python-version }}) 41 | runs-on: ubuntu-latest 42 | strategy: 43 | matrix: 44 | python-version: [3.9] 45 | steps: 46 | - uses: actions/checkout@v3 47 | - name: Set up python ${{ matrix.python-version }} 48 | uses: actions/setup-python@v4 49 | with: 50 | python-version: ${{ matrix.python-version }} 51 | - uses: actions/cache@v2 52 | with: 53 | path: ~/.cache/pip 54 | key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('requirements.txt') }} 55 | restore-keys: | 56 | ${{ runner.os }}-pip-${{ matrix.python-version }}- 57 | ${{ runner.os }}-pip- 58 | - name: Install automation scripts dependencies and add mlrun to dev packages 59 | run: pip install -r requirements.txt -r dev-requirements.txt 60 | - name: Test package 61 | run: make test 62 | -------------------------------------------------------------------------------- /src/date_adjust.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Iguazio 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | 17 | # Helper functions to adjust the timestamps of our data 18 | # while keeping the order of the selected events and 19 | # the relative distance from one event to the other 20 | import pandas as pd 21 | 22 | 23 | def date_adjustment( 24 | sample: pd.Timestamp, 25 | data_max: pd.Timestamp, 26 | new_max: pd.Timestamp, 27 | old_data_period: pd.Timedelta, 28 | new_data_period: pd.Timedelta, 29 | ) -> pd.Timestamp: 30 | """ 31 | Adjust a specific sample's date according to the original and new time periods 32 | 33 | :param sample: The sample's timestamp 34 | :param data_max: The original data's max timestamp 35 | :param new_max: The new data's max timestamp 36 | :param old_data_period: The original data's time period 37 | :param new_data_period: The new data's time period 38 | 39 | :returns: The adjusted timestamp 40 | """ 41 | sample_dates_scale = (data_max - sample) / old_data_period 42 | sample_delta = new_data_period * sample_dates_scale 43 | new_sample_ts = new_max - sample_delta 44 | return new_sample_ts 45 | 46 | 47 | def adjust_data_timespan( 48 | dataframe: pd.DataFrame, 49 | timestamp_col: str = "timestamp", 50 | new_period: str = "2d", 51 | new_max_date_str: str = "now", 52 | ): 53 | """ 54 | Adjust the dataframe timestamps to the new time period 55 | 56 | :param dataframe: The dataframe to adjust 57 | :param timestamp_col: The timestamp column name 58 | :param new_period: The new time period 59 | :param new_max_date_str: The new max date 60 | 61 | :returns: The adjusted dataframe 62 | """ 63 | # Calculate old time period 64 | data_min = dataframe.timestamp.min() 65 | data_max = dataframe.timestamp.max() 66 | old_data_period = data_max - data_min 67 | 68 | # Set new time period 69 | new_time_period = pd.Timedelta(new_period) 70 | new_max = pd.Timestamp(new_max_date_str) 71 | new_min = new_max - new_time_period 72 | new_data_period = new_max - new_min 73 | 74 | # Apply the timestamp change 75 | df = dataframe.copy() 76 | df[timestamp_col] = df[timestamp_col].apply( 77 | lambda x: date_adjustment( 78 | x, data_max, new_max, old_data_period, new_data_period 79 | ) 80 | ) 81 | df.sort_values(by="timestamp", axis=0, inplace=True) 82 | return df 83 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | PYTHON_INTERPRETER = python3 3 | SHARED_DIR ?= ~/mlrun-data 4 | HOST_MNT_DIR ?= $(SHARED_DIR) 5 | MLRUN_TAG ?= 1.2.1 6 | HOST_IP ?=$$(ip route get 1.2.3.4 | awk '{print $$7}') 7 | CONDA_ENV ?= mlrun 8 | SHELL=/bin/bash 9 | CONDA_PY_VER ?= 3.7 10 | CONDA_ACTIVATE = source $$(conda info --base)/etc/profile.d/conda.sh ; conda activate ; conda activate 11 | 12 | ################################################################################# 13 | # COMMANDS # 14 | ################################################################################# 15 | 16 | .PHONY: help 17 | help: ## Display available commands 18 | @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' 19 | 20 | .PHONY: all 21 | all: 22 | $(error please pick a target) 23 | 24 | .PHONY: install-requirements 25 | install-requirements: ## Install all requirements needed for development 26 | $(PYTHON_INTERPRETER) -m pip install -r requirements.txt -r dev-requirements.txt 27 | 28 | 29 | .PHONY: package-wheel 30 | package-wheel: clean ## Build python package wheel 31 | python setup.py bdist_wheel 32 | 33 | .PHONY: clean 34 | clean: ## Clean python package build artifacts 35 | rm -rf build 36 | rm -rf dist 37 | find . -type f -name "*.py[co]" -delete 38 | find . -type d -name "__pycache__" -delete 39 | 40 | .PHONY: fmt 41 | fmt: ## Format the code (using black and isort) 42 | @echo "Running black fmt..." 43 | $(PYTHON_INTERPRETER) -m black src tests 44 | $(PYTHON_INTERPRETER) -m isort src tests 45 | 46 | .PHONY: lint 47 | lint: fmt-check flake8 ## Run lint on the code 48 | 49 | .PHONY: fmt-check 50 | fmt-check: ## Format and check the code (using black and isort) 51 | @echo "Running black+isort fmt check..." 52 | $(PYTHON_INTERPRETER) -m black --check --diff src tests 53 | $(PYTHON_INTERPRETER) -m isort --check --diff src tests 54 | 55 | .PHONY: flake8 56 | flake8: ## Run flake8 lint 57 | @echo "Running flake8 lint..." 58 | $(PYTHON_INTERPRETER) -m flake8 src tests 59 | 60 | .PHONY: test 61 | test: clean ## Run tests 62 | $(PYTHON_INTERPRETER) -m pytest -v --capture=no --disable-warnings -rf tests 63 | 64 | .PHONY: mlrun-docker 65 | mlrun-docker: ## Start MLRun & Nuclio containers (using Docker compose) 66 | mkdir $(SHARED_DIR) -p 67 | @echo "HOST_IP=$(HOST_IP)" > .env 68 | SHARED_DIR=$(SHARED_DIR) HOST_MNT_DIR=$(HOST_MNT_DIR) TAG=$(MLRUN_TAG) docker-compose -f compose.yaml up -d 69 | @echo "use docker-compose stop / logs commands to stop or view logs" 70 | 71 | .PHONY: mlrun-api 72 | mlrun-api: ## Run MLRun DB locally (as process) 73 | @echo "Installing MLRun API dependencies ..." 74 | MLRUN_IGNORE_ENV_FILE=1 mlrun db -b 75 | 76 | .PHONY: conda-env 77 | conda-env: ## Create a conda environment 78 | @echo "Creating new conda environment $(CONDA_ENV)..." 79 | conda create -n $(CONDA_ENV) -y python=$(CONDA_PY_VER) ipykernel graphviz pip 80 | test -s ./mlrun.env && conda env config vars set -n $(CONDA_ENV) MLRUN_ENV_FILE=$$(realpath ./mlrun.env) 81 | @echo "Installing requirements.txt..." 82 | $(CONDA_ACTIVATE) $(CONDA_ENV); pip install -r requirements.txt -r dev-requirements.txt 83 | @echo -e "\nTo run mlrun API as a local process type:\n conda activate $(CONDA_ENV) && make mlrun-api" 84 | -------------------------------------------------------------------------------- /src/train_sklearn.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Iguazio 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | 17 | import pandas as pd 18 | from sklearn.model_selection import train_test_split 19 | from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score 20 | from sklearn.model_selection import RandomizedSearchCV 21 | from sklearn.ensemble import RandomForestClassifier 22 | 23 | 24 | def prepare_data_to_train( 25 | transactions_data_p: pd.DataFrame, 26 | user_events_data_p: pd.DataFrame, 27 | labels_set: pd.DataFrame, 28 | ) -> pd.DataFrame: 29 | """ 30 | This function prepare data to train and test 31 | 32 | :param transactions_data_p: transactions data 33 | :param user_events_data_p: user events data 34 | :param labels_set: labels data 35 | :return: train and test data 36 | """ 37 | transactions_data_p.drop(columns=["age", "target", "device"], inplace=True) 38 | transactions_data_p.sort_values(by="timestamp", inplace=True) 39 | user_events_data_p.sort_values(by="timestamp", inplace=True) 40 | 41 | merged_df = pd.merge_asof( 42 | transactions_data_p, 43 | user_events_data_p, 44 | on="timestamp", 45 | by="source", 46 | ) 47 | 48 | data_for_train = ( 49 | pd.merge_asof(merged_df, labels_set, on="timestamp", by="source") 50 | .drop(columns=["source", "timestamp"]) 51 | .dropna() 52 | ) 53 | 54 | lable = data_for_train["label"] 55 | data_for_train.drop(columns=["label"], inplace=True) 56 | 57 | return train_test_split(data_for_train, lable, test_size=0.2, random_state=42) 58 | 59 | 60 | def train_and_val( 61 | X_train: pd.DataFrame, 62 | X_test: pd.DataFrame, 63 | y_train: pd.DataFrame, 64 | y_test: pd.DataFrame, 65 | ) -> RandomForestClassifier: 66 | """ 67 | This function train and validate the model 68 | 69 | :param X_train: train data 70 | :param X_test: test data 71 | :param y_train: train labels 72 | :param y_test: test labels 73 | :return: model 74 | """ 75 | grid_search = { 76 | "bootstrap": [True, False], 77 | "max_depth": [ 78 | 10, 79 | 30, 80 | 50, 81 | 100, 82 | ], 83 | "max_features": ["log2", "sqrt"], 84 | "min_samples_leaf": [1, 2, 4], 85 | "min_samples_split": [2, 5, 10], 86 | "n_estimators": [50, 100, 500], 87 | } 88 | 89 | rf = RandomForestClassifier() 90 | rfc = RandomizedSearchCV( 91 | estimator=rf, 92 | param_distributions=grid_search, 93 | n_iter=100, 94 | cv=3, 95 | verbose=2, 96 | random_state=42, 97 | n_jobs=-1, 98 | ) 99 | rfc.fit(X_train, y_train) 100 | 101 | # Make predictions on the test set 102 | y_pred = rfc.best_estimator_.predict(X_test) 103 | 104 | # Calculate evaluation metrics 105 | accuracy = accuracy_score(y_test, y_pred) 106 | precision = precision_score(y_test, y_pred) 107 | recall = recall_score(y_test, y_pred) 108 | f1 = f1_score(y_test, y_pred) 109 | 110 | # Print the evaluation metrics 111 | print("Accuracy:", accuracy) 112 | print("Precision:", precision) 113 | print("Recall:", recall) 114 | print("F1 Score:", f1) 115 | return rfc.best_estimator_ 116 | -------------------------------------------------------------------------------- /project_setup.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Iguazio 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | import mlrun 17 | import os 18 | from mlrun.datastore.datastore_profile import DatastoreProfileRedis, DatastoreProfileKafkaStream, register_temporary_client_datastore_profile 19 | 20 | def setup(project: mlrun.projects.MlrunProject) -> mlrun.projects.MlrunProject: 21 | """ 22 | Creating the project for this demo. This function is expected to be called automatically when 23 | calling the function `mlrun.get_or_create_project`. 24 | 25 | :returns: a fully prepared project for this demo. 26 | """ 27 | # Set the project git source: 28 | source = project.get_param(key="source") 29 | if not source: 30 | source = "git://github.com/mlrun/demo-fraud.git" 31 | print(f"Project Source: {source}") 32 | project.set_source(source=source, pull_at_runtime=True) 33 | 34 | if project.get_param("pre_load_data"): 35 | print("pre_load_data") 36 | 37 | # Refresh MLRun hub to the most up-to-date version: 38 | mlrun.get_run_db().get_hub_catalog(source_name="default", force_refresh=True) 39 | 40 | # Set the functions: 41 | 42 | project.set_function( 43 | func="src/get_vector.py", 44 | name="get-vector", 45 | handler="get_offline_features", 46 | kind="job", 47 | ).save() 48 | project.set_function(f"db://{project.name}/get-vector", name="get-vector") 49 | 50 | _set_function( 51 | project=project, 52 | func="hub://feature_selection", 53 | name="feature-selection", 54 | kind="job", 55 | ) 56 | 57 | _set_function( 58 | project=project, 59 | func="hub://auto_trainer", 60 | name="train", 61 | kind="job", 62 | ) 63 | _set_function( 64 | project=project, 65 | func="hub://auto_trainer", 66 | name="evaluate", 67 | kind="job", 68 | ) 69 | 70 | _set_function( 71 | project=project, 72 | func="hub://v2_model_server", 73 | name="serving", 74 | kind="serving", 75 | ) 76 | 77 | # Set the training workflow: 78 | project.set_workflow("main", "src/train_workflow.py", embed=True) 79 | 80 | # Set data source for feature store 81 | _set_datasource(project) 82 | 83 | # Save and return the project: 84 | project.save() 85 | return project 86 | 87 | 88 | def _set_function( 89 | project: mlrun.projects.MlrunProject, 90 | func: str, 91 | name: str, 92 | kind: str, 93 | node_name: str = None, 94 | image: str = None, 95 | ): 96 | # Set the given function: 97 | with_repo = not func.startswith("hub://") 98 | mlrun_function = project.set_function( 99 | func=func, 100 | name=name, 101 | kind=kind, 102 | with_repo=with_repo, 103 | image=image, 104 | ) 105 | if node_name: 106 | mlrun_function.with_node_selection(node_name=node_name) 107 | # Save: 108 | mlrun_function.save() 109 | 110 | project.set_function(f"db://{project.name}/{name}", name=name) 111 | 112 | 113 | def _set_datasource(project: mlrun.projects.MlrunProject): 114 | # If running on community edition - use redis and kafka. 115 | tsdb_profile = mlrun.datastore.DatastoreProfileV3io(name="fraud-tsdb", v3io_access_key=mlrun.mlconf.get_v3io_access_key()) 116 | stream_profile = mlrun.datastore.DatastoreProfileV3io(name="fraud-stream", v3io_access_key=mlrun.mlconf.get_v3io_access_key()) 117 | 118 | if mlrun.mlconf.is_ce_mode(): 119 | redis_uri = os.environ.get('REDIS_URI', None) 120 | redis_user = os.environ.get('REDIS_USER', '') 121 | redis_password = os.environ.get('REDIS_PASSWORD', '') 122 | kafka_host = os.environ.get('KAFKA_SERVICE_HOST', f"kafka-stream.{os.environ.get('MLRUN_NAMESPACE', 'mlrun')}.svc.cluster.local") 123 | kafka_port = os.environ.get('KAFKA_SERVICE_PORT', '9092') 124 | assert redis_uri is not None, "ERROR - When running on community edition, redis endpoint is required to run fraud-demo." 125 | assert kafka_host is not None, "ERROR - When running on community edition, kafka endpoint is required to run fraud-demo." 126 | 127 | project.set_secrets({'REDIS_URI': redis_uri, 128 | 'REDIS_USER': redis_user, 129 | 'REDIS_PASSWORD': redis_password, 130 | 'KAFKA_SERVICE_HOST':kafka_host, 131 | 'KAFKA_SERVICE_PORT': kafka_port}) 132 | 133 | # Redis datastore-profile 134 | tsdb_profile = DatastoreProfileRedis( 135 | name="fraud-tsdb", 136 | endpoint_url=redis_uri, 137 | username=redis_user, 138 | password=redis_password, 139 | ) 140 | # Kafka datastore-profile 141 | stream_profile = DatastoreProfileKafkaStream( 142 | name='fraud-stream', 143 | brokers=f"{kafka_host}:{kafka_port}", 144 | topics=[], 145 | ) 146 | project.params['online_target'] = "ds://fraud-tsdb" 147 | for fs in ['transactions', 'events', 'labels']: 148 | project.params[fs] = os.path.join(mlrun.mlconf.artifact_path, fs + '.pq') 149 | 150 | # dealing with kafka 151 | kafka_uri = f"{kafka_host}:{kafka_port}" 152 | project.params['transaction_stream'] = f'kafka://{kafka_uri}?topic=transactions' 153 | project.params['events_stream'] = f'kafka://{kafka_uri}?topic=events' 154 | 155 | register_temporary_client_datastore_profile(tsdb_profile) 156 | register_temporary_client_datastore_profile(stream_profile) 157 | 158 | else: 159 | project.params['transaction_stream'] = f'v3io:///projects/{project.name}/streams/transaction' 160 | project.params['events_stream'] = f'v3io:///projects/{project.name}/streams/events' 161 | 162 | project.register_datastore_profile(tsdb_profile) 163 | project.register_datastore_profile(stream_profile) 164 | 165 | 166 | for key, value in project.params.items(): 167 | project.params[key] = value.replace('{{run.project}}', project.name) 168 | 169 | -------------------------------------------------------------------------------- /src/train_workflow.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Iguazio 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | import mlrun 16 | from kfp import dsl 17 | import os 18 | 19 | from mlrun.model import HyperParamOptions 20 | from mlrun.datastore.datastore_profile import DatastoreProfileKafkaStream, DatastoreProfileTDEngine 21 | 22 | 23 | # Create a Kubeflow Pipelines pipeline 24 | @dsl.pipeline( 25 | name="Fraud Detection Pipeline", 26 | description="Detecting fraud from a transactions dataset", 27 | ) 28 | def pipeline(vector_name="transactions-fraud", features=[], label_column="is_error"): 29 | """ 30 | This pipeline will train a model to detect fraud from a transactions dataset. 31 | :param vector_name: The name of the feature vector to use 32 | :param features: A list of features to use 33 | :param label_column: The name of the label column 34 | 35 | :returns: None 36 | """ 37 | 38 | # Get the project 39 | project = mlrun.get_current_project() 40 | 41 | # Get FeatureVector 42 | get_vector_func = project.get_function("get-vector") 43 | get_vector_run = project.run_function( 44 | get_vector_func, 45 | name="get-vector", 46 | params={ 47 | "feature_vector": vector_name, 48 | "features": features, 49 | "label_feature": label_column, 50 | "target": {"name": "parquet", "kind": "parquet"}, 51 | "update_stats": True, 52 | }, 53 | outputs = [ 54 | "feature_vector" 55 | ] 56 | ) 57 | 58 | # Feature selection 59 | feature_selection_func = project.get_function("feature-selection") 60 | feature_selection_run = project.run_function( 61 | feature_selection_func, 62 | name="feature-selection", 63 | params={ 64 | "output_vector_name": "short", 65 | "label_column": project.get_param("label_column", "label"), 66 | "k": 18, 67 | "min_votes": 2, 68 | "ignore_type_errors": True, 69 | }, 70 | inputs={ 71 | "df_artifact": project.get_artifact_uri(vector_name, "feature-vector") 72 | }, 73 | outputs=[ 74 | "feature_scores", 75 | "selected_features_count", 76 | "top_features_vector", 77 | "selected_features", 78 | ], 79 | ).after(get_vector_run) 80 | 81 | # train with hyper-paremeters 82 | train_func = project.get_function("train") 83 | train_run = project.run_function( 84 | train_func, 85 | name="train", 86 | handler="train", 87 | params={ 88 | "sample": -1, 89 | "label_column": project.get_param("label_column", "label"), 90 | "test_size": 0.10, 91 | }, 92 | hyperparams={ 93 | "model_name": [ 94 | "transaction_fraud_rf", 95 | "transaction_fraud_xgboost", 96 | "transaction_fraud_adaboost", 97 | ], 98 | "model_class": [ 99 | "sklearn.ensemble.RandomForestClassifier", 100 | "sklearn.linear_model.LogisticRegression", 101 | "sklearn.ensemble.AdaBoostClassifier", 102 | ], 103 | }, 104 | hyper_param_options=HyperParamOptions( 105 | strategy="list", selector="max.accuracy" 106 | ), 107 | inputs={"dataset": feature_selection_run.outputs["top_features_vector"]}, 108 | outputs=["model", "test_set"], 109 | ).after(feature_selection_run) 110 | 111 | # test and visualize your model 112 | test_func = project.get_function("evaluate") 113 | test_run = mlrun.run_function( 114 | test_func, 115 | name="evaluate", 116 | handler="evaluate", 117 | params={ 118 | "label_columns": project.get_param("label_column", "label"), 119 | "model": train_run.outputs["model"], 120 | "drop_columns": project.get_param("label_column", "label"), 121 | }, 122 | inputs={"dataset": train_run.outputs["test_set"]}, 123 | ).after(train_run) 124 | 125 | # Create a serverless function from the hub, add a feature enrichment router 126 | # This will enrich and impute the request with data from the feature vector 127 | serving_func = project.get_function("serving") 128 | serving_func.set_topology( 129 | "router", 130 | mlrun.serving.routers.EnrichmentModelRouter( 131 | feature_vector_uri="short", impute_policy={"*": "$mean"} 132 | ), 133 | exist_ok=True, 134 | ) 135 | 136 | # Enable model monitoring 137 | serving_func.set_tracking() 138 | 139 | if mlrun.mlconf.is_ce_mode(): 140 | # Use default service 141 | tsdb_profile = DatastoreProfileTDEngine(name="fraud-monitoring-tsdb", 142 | user='root', 143 | password='taosdata', 144 | host=f"tdengine-tsdb.{os.environ.get('MLRUN_NAMESPACE', 'mlrun')}.svc.cluster.local", 145 | port='6041') 146 | project.register_datastore_profile(tsdb_profile) 147 | 148 | kafka_host = os.environ.get('KAFKA_SERVICE_HOST', f"kafka-stream.{os.environ.get('MLRUN_NAMESPACE', 'mlrun')}.svc.cluster.local") 149 | kafka_port = os.environ.get('KAFKA_SERVICE_PORT', '9092') 150 | 151 | stream_profile = DatastoreProfileKafkaStream( 152 | name='fraud-monitoring-stream', 153 | brokers=f"{kafka_host}:{kafka_port}", 154 | topics=[], 155 | ) 156 | project.register_datastore_profile(stream_profile) 157 | 158 | project.set_model_monitoring_credentials( 159 | tsdb_profile_name=tsdb_profile.name, 160 | stream_profile_name=stream_profile.name, 161 | replace_creds=True 162 | ) 163 | 164 | else: 165 | project.set_model_monitoring_credentials( 166 | tsdb_profile_name='fraud-tsdb', 167 | stream_profile_name='fraud-stream', 168 | replace_creds=True 169 | ) 170 | 171 | serving_func.save() 172 | # deploy the model server, pass a list of trained models to serve 173 | deploy = project.deploy_function( 174 | serving_func, 175 | models=[{"key": "fraud", "model_path": train_run.outputs["model"]}], 176 | ).after(train_run) 177 | -------------------------------------------------------------------------------- /images/feature-store-graph.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | mlrun-flow 11 | 12 | 13 | 14 | _start 15 | 16 | start 17 | 18 | 19 | 20 | MyMap 21 | 22 | MyMap 23 | 24 | 25 | 26 | _start->MyMap 27 | 28 | 29 | 30 | 31 | 32 | storey.Extend 33 | 34 | storey.Extend 35 | 36 | 37 | 38 | MyMap->storey.Extend 39 | 40 | 41 | 42 | 43 | 44 | filter 45 | 46 | filter 47 | 48 | 49 | 50 | storey.Extend->filter 51 | 52 | 53 | 54 | 55 | 56 | FeaturesetValidator 57 | 58 | FeaturesetValidator 59 | 60 | 61 | 62 | filter->FeaturesetValidator 63 | 64 | 65 | 66 | 67 | 68 | Aggregates 69 | 70 | Aggregates 71 | 72 | 73 | 74 | FeaturesetValidator->Aggregates 75 | 76 | 77 | 78 | 79 | 80 | parquet 81 | 82 | 83 | parquet 84 | 85 | 86 | 87 | Aggregates->parquet 88 | 89 | 90 | 91 | 92 | 93 | nosql 94 | 95 | 96 | nosql 97 | 98 | 99 | 100 | Aggregates->nosql 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /05-real-time-serving-pipeline.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Real-time Application Pipeline\n", 8 | "\n", 9 | "In this example, we define an application pipeline that accepts a user request, enriches the request with real-time features from the feature store, and feeds the features into a three-legged ensemble that uses the newly trained models.\n", 10 | "\n", 11 | "You would typically need to implement and deploy multiple microservices and com‐ plex logic to build such an application pipeline. But, with MLRun, you can define it in a few lines of code and deploy it automatically into elastic serverless functions. In addition, the MLRun serving framework will automatically support real-time feature imputing, model monitoring, and so on without requiring extra coding." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": null, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "The history saving thread hit an unexpected error (DatabaseError('database disk image is malformed')).History will not be written to the database.\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "import mlrun\n", 29 | "\n", 30 | "project = mlrun.get_or_create_project(\n", 31 | " name=\"fraud-demo\",\n", 32 | " context=\"./\",\n", 33 | " user_project=True,\n", 34 | " )" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "## Defining a custom serving class\n", 42 | "\n", 43 | "MLRun has many built-in model-serving classes for different frameworks (Sklearn, Xgboost, PyTorch, TensorFlow, ONNX, Hugging Face models, and so on). You can also build your custom model serving class as demonstrated in Example 7-24. The serving class must support the load() method for loading the model and the predict() method for making a prediction. You can read MLRun documentation to see all the hooks and advanced usage." 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "# mlrun: start-code" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 3, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "import numpy as np\n", 62 | "from cloudpickle import load\n", 63 | "from mlrun.serving.v2_serving import V2ModelServer\n", 64 | "\n", 65 | "class ClassifierModel(V2ModelServer):\n", 66 | " \n", 67 | " def load(self):\n", 68 | " \"\"\"load and initialize the model and/or other elements\"\"\"\n", 69 | " model_file, extra_data = self.get_model('.pkl')\n", 70 | " self.model = load(open(model_file, 'rb'))\n", 71 | " \n", 72 | " def predict(self, body: dict) -> list:\n", 73 | " \"\"\"Generate model predictions from sample\"\"\"\n", 74 | " print(f\"Input -> {body['inputs']}\")\n", 75 | " feats = np.asarray(body['inputs'])\n", 76 | " result: np.ndarray = self.model.predict(feats)\n", 77 | " return result.tolist()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 4, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "# mlrun: end-code" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "## Building an Application Pipeline with Enrichment and Ensemble\n", 94 | "\n", 95 | "MLRun serving can produce managed real-time serverless pipelines from various tasks, including MLRun models or standard model files. The pipelines use the Nuclio real-time serverless engine, which can be deployed anywhere. Nuclio is a high-performance open-source serverless framework focused on data, I/O, and compute-intensive workloads.\n", 96 | "\n", 97 | "The EnrichmentVotingEnsemble router class auto-enriches the request with data from the feature store. The router input accepts a list of inference requests (each request can be a dict or list of incoming features/keys). It enriches the request with data from the specified feature vector (feature_vector_uri), forwards the vector to one or more models in an ensemble, and returns an aggregated prediction value (for example, the average result across the three models).\n", 98 | "The features can often have null values (None, NaN, Inf). The Enrichment_ routers can substitute the null value with fixed or statistical value per fea ture. This is done through the `impute_policy` parameter, which accepts the impute policy per feature (where * is used to specify the default). The value can be a fixed number for constants or $mean, $max, $min, $std, $count for statistical values to substitute the value with the equivalent feature stats (taken from the feature store).\n", 99 | "The code in Example 7-24 defines a new serving function with the ClassifierModel class code (in serving.py) and a router topology (using the EnrichmentVotingEnsem ble router class) with three child models." 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 5, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "[('transaction_fraud_rf',\n", 111 | " {'updated': '2024-10-08 13:21:47.319818+00:00',\n", 112 | " 'project': 'fraud-demo-felipe',\n", 113 | " 'key': 'model',\n", 114 | " 'tree': 'e27f0d6d-8f14-4642-abee-43b9867631ef',\n", 115 | " 'tag': 'latest',\n", 116 | " 'labels': {'workflow-id': 'e27f0d6d-8f14-4642-abee-43b9867631ef',\n", 117 | " 'framework': 'sklearn'},\n", 118 | " 'iter': 1,\n", 119 | " 'hash': 'd31c0e672e4ac97438e866612c9fb02c1a3a1732'}),\n", 120 | " ('transaction_fraud_xgboost',\n", 121 | " {'updated': '2024-10-08 13:21:18.851606+00:00',\n", 122 | " 'project': 'fraud-demo-felipe',\n", 123 | " 'key': 'model',\n", 124 | " 'tree': 'e27f0d6d-8f14-4642-abee-43b9867631ef',\n", 125 | " 'tag': 'latest',\n", 126 | " 'labels': {'workflow-id': 'e27f0d6d-8f14-4642-abee-43b9867631ef',\n", 127 | " 'framework': 'sklearn'},\n", 128 | " 'iter': 2,\n", 129 | " 'hash': 'd40c64ec2d081899089ec7e34288c87a175a848f'}),\n", 130 | " ('transaction_fraud_adaboost',\n", 131 | " {'updated': '2024-10-08 13:21:22.048244+00:00',\n", 132 | " 'project': 'fraud-demo-felipe',\n", 133 | " 'key': 'model',\n", 134 | " 'tree': 'e27f0d6d-8f14-4642-abee-43b9867631ef',\n", 135 | " 'tag': 'latest',\n", 136 | " 'labels': {'workflow-id': 'e27f0d6d-8f14-4642-abee-43b9867631ef',\n", 137 | " 'framework': 'sklearn'},\n", 138 | " 'iter': 3,\n", 139 | " 'hash': 'db02f4acce087779cbc634285ea1647206a5fc84'})]" 140 | ] 141 | }, 142 | "execution_count": 5, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [ 148 | "[(m.spec.db_key, m.metadata.to_dict()) for m in project.list_models('', tag='latest')]" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 6, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "image/svg+xml": [ 159 | "\n", 160 | "\n", 162 | "\n", 164 | "\n", 165 | "\n", 167 | "\n", 168 | "mlrun-flow\n", 169 | "\n", 170 | "\n", 171 | "\n", 172 | "_start\n", 173 | "\n", 174 | "start\n", 175 | "\n", 176 | "\n", 177 | "\n", 178 | "\n", 179 | "\n", 180 | "\n", 181 | "\n", 182 | "\n", 183 | "_start->\n", 184 | "\n", 185 | "\n", 186 | "\n", 187 | "\n", 188 | "\n", 189 | "transaction_fraud_rf\n", 190 | "\n", 191 | "transaction_fraud_rf\n", 192 | "\n", 193 | "\n", 194 | "\n", 195 | "->transaction_fraud_rf\n", 196 | "\n", 197 | "\n", 198 | "\n", 199 | "\n", 200 | "\n", 201 | "transaction_fraud_xgboost\n", 202 | "\n", 203 | "transaction_fraud_xgboost\n", 204 | "\n", 205 | "\n", 206 | "\n", 207 | "->transaction_fraud_xgboost\n", 208 | "\n", 209 | "\n", 210 | "\n", 211 | "\n", 212 | "\n", 213 | "transaction_fraud_adaboost\n", 214 | "\n", 215 | "transaction_fraud_adaboost\n", 216 | "\n", 217 | "\n", 218 | "\n", 219 | "->transaction_fraud_adaboost\n", 220 | "\n", 221 | "\n", 222 | "\n", 223 | "\n", 224 | "\n" 225 | ], 226 | "text/plain": [ 227 | "" 228 | ] 229 | }, 230 | "execution_count": 6, 231 | "metadata": {}, 232 | "output_type": "execute_result" 233 | } 234 | ], 235 | "source": [ 236 | "# Create the serving function from your code above\n", 237 | "serving_fn = project.set_function('src/serving.py', name='test-function',\n", 238 | " image=\"mlrun/mlrun\", kind=\"serving\")\n", 239 | "serving_fn.set_topology(\n", 240 | " \"router\",\n", 241 | " mlrun.serving.routers.EnrichmentVotingEnsemble(\n", 242 | " feature_vector_uri=\"short\",\n", 243 | " impute_policy={\"*\": \"$mean\"}),\n", 244 | ")\n", 245 | "# add the 3 trained models to the Ensemble\n", 246 | "for model in project.list_models('', tag='latest'):\n", 247 | " name = model.spec.db_key\n", 248 | " serving_fn.add_model(name, class_name=\"ClassifierModel\", model_path=model.uri)\n", 249 | "# Plot the ensemble configuration\n", 250 | "serving_fn.spec.graph.plot()" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "## Test the Application Pipeline Locally\n", 258 | "\n", 259 | "Before deploying the serving function, you can test it in the current notebook and check the model output." 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 7, 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "name": "stdout", 269 | "output_type": "stream", 270 | "text": [ 271 | "> 2024-10-08 13:41:43,004 [info] model transaction_fraud_rf was loaded\n", 272 | "> 2024-10-08 13:41:43,033 [info] model transaction_fraud_xgboost was loaded\n", 273 | "> 2024-10-08 13:41:43,064 [info] model transaction_fraud_adaboost was loaded\n" 274 | ] 275 | }, 276 | { 277 | "name": "stderr", 278 | "output_type": "stream", 279 | "text": [ 280 | "Trying to unpickle estimator DecisionTreeClassifier from version 1.5.2 when using version 1.5.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", 281 | "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", 282 | "Trying to unpickle estimator RandomForestClassifier from version 1.5.2 when using version 1.5.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", 283 | "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", 284 | "Trying to unpickle estimator LogisticRegression from version 1.5.2 when using version 1.5.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", 285 | "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", 286 | "Trying to unpickle estimator AdaBoostClassifier from version 1.5.2 when using version 1.5.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", 287 | "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n" 288 | ] 289 | } 290 | ], 291 | "source": [ 292 | "# Create a mock server from the serving function\n", 293 | "local_server = serving_fn.to_mock_server()" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 8, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "Input -> [[82.38333699999998, 176.010817, 0.0, 38.79236982490288, 134.16, 417.81, 11.0, 37.982727272727274, 134.16, 1275.0599999999997, 44.0, 28.978636363636358, 90.0, 1.0, 2.0]]\n", 306 | "Input -> [[82.38333699999998, 176.010817, 0.0, 38.79236982490288, 134.16, 417.81, 11.0, 37.982727272727274, 134.16, 1275.0599999999997, 44.0, 28.978636363636358, 90.0, 1.0, 2.0]]\n", 307 | "Input -> [[82.38333699999998, 176.010817, 0.0, 38.79236982490288, 134.16, 417.81, 11.0, 37.982727272727274, 134.16, 1275.0599999999997, 44.0, 28.978636363636358, 90.0, 1.0, 2.0]]\n" 308 | ] 309 | }, 310 | { 311 | "name": "stderr", 312 | "output_type": "stream", 313 | "text": [ 314 | "X does not have valid feature names, but RandomForestClassifier was fitted with feature names\n", 315 | "X does not have valid feature names, but LogisticRegression was fitted with feature names\n", 316 | "X does not have valid feature names, but AdaBoostClassifier was fitted with feature names\n" 317 | ] 318 | }, 319 | { 320 | "data": { 321 | "text/plain": [ 322 | "{'id': 'f7f45c8d7e3a4c6993d6e10cc0f91acd',\n", 323 | " 'model_name': 'VotingEnsemble',\n", 324 | " 'outputs': [0],\n", 325 | " 'model_version': 'v1'}" 326 | ] 327 | }, 328 | "execution_count": 8, 329 | "metadata": {}, 330 | "output_type": "execute_result" 331 | } 332 | ], 333 | "source": [ 334 | "# Choose an id for your test\n", 335 | "sample_id = 'C1000148617'\n", 336 | "\n", 337 | "# Send your sample ID for prediction\n", 338 | "local_server.test(path='/v2/models/infer',\n", 339 | " body={'inputs': [[sample_id]]})\n", 340 | "\n", 341 | "# notice the input vector is printed 3 times (once per child model) and is enriched with data from the feature store" 342 | ] 343 | }, 344 | { 345 | "cell_type": "markdown", 346 | "metadata": { 347 | "tags": [] 348 | }, 349 | "source": [ 350 | "### Accessing the real-time feature vector directly\n", 351 | "\n", 352 | "If you would like to access the real-time features directly from your application instead of using the EnrichmentVotingEnsemble, you can call the feature store `get_online_feature_service()` method. This method is used internally in the EnrichmentVotingEnsemble router class." 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 9, 358 | "metadata": {}, 359 | "outputs": [ 360 | { 361 | "data": { 362 | "text/plain": [ 363 | "[{'amount_max_2h': 82.38333699999998,\n", 364 | " 'amount_max_12h': 134.16,\n", 365 | " 'amount_max_24h': 134.16,\n", 366 | " 'amount_count_2h': 0.0,\n", 367 | " 'amount_count_12h': 11.0,\n", 368 | " 'amount_count_24h': 44.0,\n", 369 | " 'amount_sum_2h': 176.010817,\n", 370 | " 'amount_sum_12h': 417.81,\n", 371 | " 'amount_sum_24h': 1275.0599999999997,\n", 372 | " 'es_transportation_sum_14d': 90.0,\n", 373 | " 'es_health_sum_14d': 1.0,\n", 374 | " 'es_otherservices_sum_14d': 2.0,\n", 375 | " 'amount_avg_2h': 38.79236982490288,\n", 376 | " 'amount_avg_12h': 37.982727272727274,\n", 377 | " 'amount_avg_24h': 28.978636363636358}]" 378 | ] 379 | }, 380 | "execution_count": 9, 381 | "metadata": {}, 382 | "output_type": "execute_result" 383 | } 384 | ], 385 | "source": [ 386 | "import mlrun.feature_store as fs\n", 387 | "\n", 388 | "# Create the online feature service\n", 389 | "svc = fs.get_feature_vector('short:latest').get_online_feature_service(impute_policy={\"*\": \"$mean\"})\n", 390 | "\n", 391 | "# Get sample feature vector\n", 392 | "sample_fv = svc.get([{'source': sample_id}])\n", 393 | "sample_fv" 394 | ] 395 | }, 396 | { 397 | "cell_type": "markdown", 398 | "metadata": {}, 399 | "source": [ 400 | "## Deploying the function on the Kubernetes cluster\n", 401 | "\n", 402 | "You can now deploy the function. Once deployed, you get a function with http trigger that can be called from other locations." 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": null, 408 | "metadata": {}, 409 | "outputs": [ 410 | { 411 | "name": "stdout", 412 | "output_type": "stream", 413 | "text": [ 414 | "> 2024-10-08 13:41:43,535 [info] Starting remote function deploy\n", 415 | "2024-10-08 13:41:43 (info) Deploying function\n", 416 | "2024-10-08 13:41:44 (info) Building\n", 417 | "2024-10-08 13:41:44 (info) Staging files and preparing base images\n", 418 | "2024-10-08 13:41:44 (warn) Using user provided base image, runtime interpreter version is provided by the base image\n", 419 | "2024-10-08 13:41:44 (info) Building processor image\n", 420 | "2024-10-08 13:42:49 (info) Build complete\n", 421 | "2024-10-08 13:43:13 (info) Function deploy complete\n", 422 | "> 2024-10-08 13:43:14,979 [info] Successfully deployed function: {\"external_invocation_urls\":[\"fraud-demo-felipe-test-function.default-tenant.app.cust-cs-illl--3-6-0.iguazio-cd2.com/\"],\"internal_invocation_urls\":[\"nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080\"]}\n" 423 | ] 424 | }, 425 | { 426 | "data": { 427 | "text/plain": [ 428 | "'http://fraud-demo-felipe-test-function.default-tenant.app.cust-cs-illl--3-6-0.iguazio-cd2.com/'" 429 | ] 430 | }, 431 | "execution_count": 10, 432 | "metadata": {}, 433 | "output_type": "execute_result" 434 | } 435 | ], 436 | "source": [ 437 | "# Enable model monitoring\n", 438 | "serving_fn.set_tracking()\n", 439 | "\n", 440 | "# Deploy the serving function\n", 441 | "serving_fn.deploy()" 442 | ] 443 | }, 444 | { 445 | "cell_type": "markdown", 446 | "metadata": {}, 447 | "source": [ 448 | "## Test the server\n", 449 | "\n", 450 | "You can test the serving function and examine the model output." 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": 11, 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "> 2024-10-08 13:43:15,027 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n" 463 | ] 464 | }, 465 | { 466 | "data": { 467 | "text/plain": [ 468 | "{'id': 'ee5635d0-bb82-4036-a71c-379696d9ed79',\n", 469 | " 'model_name': 'VotingEnsemble',\n", 470 | " 'outputs': [0],\n", 471 | " 'model_version': 'v1'}" 472 | ] 473 | }, 474 | "execution_count": 11, 475 | "metadata": {}, 476 | "output_type": "execute_result" 477 | } 478 | ], 479 | "source": [ 480 | "# Choose an id for your test\n", 481 | "sample_id = 'C1000148617'\n", 482 | "\n", 483 | "model_inference_path = '/v2/models/infer'\n", 484 | "\n", 485 | "# Send your sample ID for prediction\n", 486 | "serving_fn.invoke(path='/v2/models/infer',\n", 487 | " body={'inputs': [[sample_id]]})" 488 | ] 489 | }, 490 | { 491 | "cell_type": "markdown", 492 | "metadata": {}, 493 | "source": [ 494 | "You can also directly query the feature store values, which are used in the enrichment." 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": {}, 500 | "source": [ 501 | "### Simulate incoming data" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 12, 507 | "metadata": {}, 508 | "outputs": [], 509 | "source": [ 510 | "# Load the dataset\n", 511 | "data = mlrun.get_dataitem(mlrun.get_sample_path(\"data/fraud-demo-mlrun-fs-docs/data.csv\")).as_df()\n", 512 | "\n", 513 | "# use only first 10k\n", 514 | "data = data.sort_values(by='source', axis=0)[:10000]\n", 515 | "\n", 516 | "# keys\n", 517 | "sample_ids = data['source'].to_list()" 518 | ] 519 | }, 520 | { 521 | "cell_type": "code", 522 | "execution_count": 13, 523 | "metadata": {}, 524 | "outputs": [ 525 | { 526 | "name": "stdout", 527 | "output_type": "stream", 528 | "text": [ 529 | "> 2024-10-08 13:43:19,205 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n", 530 | "{'id': 'efa462b4-36ed-4f63-8d4d-8d6474fff08d', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n", 531 | "> 2024-10-08 13:43:20,718 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n", 532 | "{'id': 'b930f851-fd9a-498a-8b7f-4e84f3bd42ec', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n", 533 | "> 2024-10-08 13:43:21,987 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n", 534 | "{'id': 'c064d2e8-377a-4384-9a47-7c5881f83b0a', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n", 535 | "> 2024-10-08 13:43:22,928 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n", 536 | "{'id': 'd4d6a214-fcf5-4c94-9551-db129c1f52f2', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n", 537 | "> 2024-10-08 13:43:23,263 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n", 538 | "{'id': '72419b36-0e6e-4ade-8b23-70b3407115d6', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n", 539 | "> 2024-10-08 13:43:24,141 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n", 540 | "{'id': 'aa27481c-5903-4f02-9ce4-5540fb45d775', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n", 541 | "> 2024-10-08 13:43:25,491 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n", 542 | "{'id': 'fdc379d7-7a9c-452b-91ac-93b29abaffab', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n", 543 | "> 2024-10-08 13:43:26,392 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n", 544 | "{'id': '015fbeab-7f98-4295-a05e-a2fb38d54b3a', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n", 545 | "> 2024-10-08 13:43:27,417 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n", 546 | "{'id': '59733a3f-ffc8-4828-88e7-0e46ff9a129a', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n", 547 | "> 2024-10-08 13:43:28,915 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-test-function.default-tenant.svc.cluster.local:8080/v2/models/infer\"}\n", 548 | "{'id': '05e984c5-556d-415d-a4f4-e3eb389f2213', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n" 549 | ] 550 | } 551 | ], 552 | "source": [ 553 | "from random import choice, uniform\n", 554 | "from time import sleep\n", 555 | "\n", 556 | "# Sending random requests\n", 557 | "for _ in range(10):\n", 558 | " data_point = choice(sample_ids)\n", 559 | " try:\n", 560 | " resp = serving_fn.invoke(path=model_inference_path, body={'inputs': [[data_point]]})\n", 561 | " print(resp)\n", 562 | " sleep(uniform(0.2, 1.7))\n", 563 | " except OSError:\n", 564 | " pass" 565 | ] 566 | }, 567 | { 568 | "cell_type": "markdown", 569 | "metadata": { 570 | "pycharm": { 571 | "name": "#%% md\n" 572 | } 573 | }, 574 | "source": [ 575 | "## Done!\n", 576 | "\n", 577 | "You've completed the fraud-detection demo. \n" 578 | ] 579 | } 580 | ], 581 | "metadata": { 582 | "kernelspec": { 583 | "display_name": "Python 3 (ipykernel)", 584 | "language": "python", 585 | "name": "python3" 586 | }, 587 | "language_info": { 588 | "codemirror_mode": { 589 | "name": "ipython", 590 | "version": 3 591 | }, 592 | "file_extension": ".py", 593 | "mimetype": "text/x-python", 594 | "name": "python", 595 | "nbconvert_exporter": "python", 596 | "pygments_lexer": "ipython3", 597 | "version": "3.11.5" 598 | } 599 | }, 600 | "nbformat": 4, 601 | "nbformat_minor": 4 602 | } 603 | -------------------------------------------------------------------------------- /04-train-test-pipeline.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Model Training and Validation Pipeline\n", 8 | "Now that you have created features, you can use them to train one or more models. In this section, you will generate feature vectors with multiple features from one or more feature sets and feed them into an automated ML training and testing pipeline to create high-quality models.\n", 9 | "\n", 10 | "The ML pipeline can be triggered and tracked manually during the interactive devel‐ opment, or it can be saved (into Git) and be executed automatically on a given schedule or as a reaction to different events (such as code modification, CI/CD, data changes, model drift, and so on). See [MLRun project and CI/CD documentation](https://docs.mlrun.org/en/stable/projects/project.html) for details.\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "### Saving and loading projects from GIT\n", 18 | "\n", 19 | "After you saved your project and its elements (functions, workflows, artifacts, etc.) you can commit all your changes to a \n", 20 | "GIT repository. This can be done using standard GIT tools or using MLRun `project` methods such as `pull`, `push`, \n", 21 | "`remote`, which calls the Git API for you.\n", 22 | "\n", 23 | "Projects can then be loaded from Git using MLRun `load_project` method, for example: \n", 24 | "\n", 25 | " project = mlrun.load_project(\"./myproj\", \"git://github.com/mlrun/project-demo.git\", name=project_name)\n", 26 | " \n", 27 | "or using MLRun CLI:\n", 28 | "\n", 29 | " mlrun project -n myproj -u \"git://github.com/mlrun/project-demo.git\" ./myproj\n", 30 | " \n", 31 | "Projects can be loaded or created by using MLRun `get_or_create_project` method.\n", 32 | " \n", 33 | "Read [CI/CD integration](../../projects/ci-integration.html) for more details." 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "## MLRun installation and configuration\n", 41 | "Before running this notebook make sure the `mlrun` package is installed (`pip install mlrun`) and that you have configured the access to MLRun service." 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "# Install MLRun if not installed, run this only once (restart the notebook after the install !!!)\n", 51 | "%pip install mlrun" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "The history saving thread hit an unexpected error (DatabaseError('database disk image is malformed')).History will not be written to the database.\n", 64 | "> 2024-10-08 13:19:58,328 [info] Project loaded successfully: {\"project_name\":\"fraud-demo-felipe\"}\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "import mlrun\n", 70 | "\n", 71 | "project = mlrun.get_or_create_project(\n", 72 | " name=\"fraud-demo\",\n", 73 | " context=\"./\",\n", 74 | " user_project=True,\n", 75 | " )" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "## Creating and Evaluating a Feature Vector\n", 83 | "\n", 84 | "Models are trained with multiple features, which can arrive from different feature sets and be collected into training (feature) vectors. Feature stores know how to correctly combine the features into a vector by implementing smart JOINs and assessing the time dimension (time traveling).\n", 85 | "To define a feature vector, you need to specify a name, the list of features it contains, the target features (labels), and other optional parameters. Features are specified as `. or .*` (all the features in a feature set). The following part demonstrates how to create and use a feature vector.\n" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "### Create a feature vector" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 2, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "# Import MLRun's Feature Store\n", 102 | "import mlrun.feature_store as fstore\n", 103 | "\n", 104 | "# Define the list of features to use\n", 105 | "features = ['events.*',\n", 106 | " 'transactions.amount_max_2h', \n", 107 | " 'transactions.amount_sum_2h', \n", 108 | " 'transactions.amount_count_2h',\n", 109 | " 'transactions.amount_avg_2h', \n", 110 | " 'transactions.amount_max_12h', \n", 111 | " 'transactions.amount_sum_12h',\n", 112 | " 'transactions.amount_count_12h', \n", 113 | " 'transactions.amount_avg_12h', \n", 114 | " 'transactions.amount_max_24h',\n", 115 | " 'transactions.amount_sum_24h', \n", 116 | " 'transactions.amount_count_24h', \n", 117 | " 'transactions.amount_avg_24h',\n", 118 | " 'transactions.es_transportation_sum_14d', \n", 119 | " 'transactions.es_health_sum_14d',\n", 120 | " 'transactions.es_otherservices_sum_14d', \n", 121 | " 'transactions.es_food_sum_14d',\n", 122 | " 'transactions.es_hotelservices_sum_14d', \n", 123 | " 'transactions.es_barsandrestaurants_sum_14d',\n", 124 | " 'transactions.es_tech_sum_14d', \n", 125 | " 'transactions.es_sportsandtoys_sum_14d',\n", 126 | " 'transactions.es_wellnessandbeauty_sum_14d', \n", 127 | " 'transactions.es_hyper_sum_14d',\n", 128 | " 'transactions.es_fashion_sum_14d', \n", 129 | " 'transactions.es_home_sum_14d', \n", 130 | " 'transactions.es_travel_sum_14d', \n", 131 | " 'transactions.es_leisure_sum_14d',\n", 132 | " 'transactions.gender_F',\n", 133 | " 'transactions.gender_M',\n", 134 | " 'transactions.step', \n", 135 | " 'transactions.amount', \n", 136 | " 'transactions.timestamp_hour',\n", 137 | " 'transactions.timestamp_day_of_week']" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "### Create a feature vector" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 3, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "# Define the feature vector name for future reference\n", 154 | "fv_name = 'transactions-fraud'\n", 155 | "\n", 156 | "# Define the feature vector using the feature store (fstore)\n", 157 | "transactions_fv = fstore.FeatureVector(fv_name, \n", 158 | " features, \n", 159 | " label_feature=\"labels.label\",\n", 160 | " description='Predicting a fraudulent transaction')\n", 161 | "\n", 162 | "# Save the feature vector in the feature store\n", 163 | "transactions_fv.save()" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 4, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "> 2024-10-08 13:20:06,351 [info] Merger detected timestamp resolution incompatibility between feature set labels and others: datetime64[us] and datetime64[ms]. Converting feature set timestamp column 'timestamp' to type datetime64[us].\n", 176 | "> 2024-10-08 13:20:06,399 [info] wrote target: {'size': 151272, 'name': 'parquet', 'updated': '2024-10-08T13:20:06.399382+00:00', 'partitioned': True, 'path': 'v3io:///projects/fraud-demo-felipe/FeatureStore/transactions-fraud/parquet/vectors/transactions-fraud-latest.parquet', 'kind': 'parquet', 'status': 'ready'}\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "# test get_offline_features function\n", 182 | "\n", 183 | "from mlrun.datastore.targets import ParquetTarget\n", 184 | "data = transactions_fv.get_offline_features(target=ParquetTarget())\n" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 5, 190 | "metadata": {}, 191 | "outputs": [ 192 | { 193 | "data": { 194 | "text/plain": [ 195 | "" 196 | ] 197 | }, 198 | "execution_count": 5, 199 | "metadata": {}, 200 | "output_type": "execute_result" 201 | } 202 | ], 203 | "source": [ 204 | "data" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 8, 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "data": { 214 | "text/html": [ 215 | "
\n", 216 | "\n", 229 | "\n", 230 | " \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | " \n", 237 | " \n", 238 | " \n", 239 | " \n", 240 | " \n", 241 | " \n", 242 | " \n", 243 | " \n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \n", 251 | " \n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | " \n", 284 | " \n", 285 | " \n", 286 | " \n", 287 | " \n", 288 | " \n", 289 | " \n", 290 | " \n", 291 | " \n", 292 | " \n", 293 | " \n", 294 | " \n", 295 | " \n", 296 | " \n", 297 | " \n", 298 | " \n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | " \n", 306 | " \n", 307 | " \n", 308 | " \n", 309 | " \n", 310 | " \n", 311 | " \n", 312 | " \n", 313 | " \n", 314 | " \n", 315 | " \n", 316 | " \n", 317 | " \n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " \n", 322 | " \n", 323 | " \n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | " \n", 345 | " \n", 346 | " \n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | "
event_password_changeevent_details_changeevent_loginamount_max_2hamount_sum_2hamount_count_2hamount_avg_2hamount_max_12hamount_sum_12hamount_count_12h...es_home_sum_14des_travel_sum_14des_leisure_sum_14dgender_Fgender_Mstepamounttimestamp_hourtimestamp_day_of_weeklabel
00011.831.831.01.8300001.831.831.0...0.00.00.00.01.072.01.837.06.00.0
100118.7240.223.013.40666718.7240.223.0...0.00.00.00.01.066.018.727.06.00.0
210025.9264.863.021.62000025.9264.863.0...0.00.00.00.01.027.025.927.06.00.0
310024.7530.172.015.08500024.7530.172.0...0.00.00.00.01.0141.024.757.06.00.0
410064.1865.172.032.58500064.1865.172.0...0.00.00.01.00.0124.064.187.06.00.0
\n", 379 | "

5 rows × 36 columns

\n", 380 | "
" 381 | ], 382 | "text/plain": [ 383 | " event_password_change event_details_change event_login amount_max_2h \\\n", 384 | "0 0 0 1 1.83 \n", 385 | "1 0 0 1 18.72 \n", 386 | "2 1 0 0 25.92 \n", 387 | "3 1 0 0 24.75 \n", 388 | "4 1 0 0 64.18 \n", 389 | "\n", 390 | " amount_sum_2h amount_count_2h amount_avg_2h amount_max_12h \\\n", 391 | "0 1.83 1.0 1.830000 1.83 \n", 392 | "1 40.22 3.0 13.406667 18.72 \n", 393 | "2 64.86 3.0 21.620000 25.92 \n", 394 | "3 30.17 2.0 15.085000 24.75 \n", 395 | "4 65.17 2.0 32.585000 64.18 \n", 396 | "\n", 397 | " amount_sum_12h amount_count_12h ... es_home_sum_14d es_travel_sum_14d \\\n", 398 | "0 1.83 1.0 ... 0.0 0.0 \n", 399 | "1 40.22 3.0 ... 0.0 0.0 \n", 400 | "2 64.86 3.0 ... 0.0 0.0 \n", 401 | "3 30.17 2.0 ... 0.0 0.0 \n", 402 | "4 65.17 2.0 ... 0.0 0.0 \n", 403 | "\n", 404 | " es_leisure_sum_14d gender_F gender_M step amount timestamp_hour \\\n", 405 | "0 0.0 0.0 1.0 72.0 1.83 7.0 \n", 406 | "1 0.0 0.0 1.0 66.0 18.72 7.0 \n", 407 | "2 0.0 0.0 1.0 27.0 25.92 7.0 \n", 408 | "3 0.0 0.0 1.0 141.0 24.75 7.0 \n", 409 | "4 0.0 1.0 0.0 124.0 64.18 7.0 \n", 410 | "\n", 411 | " timestamp_day_of_week label \n", 412 | "0 6.0 0.0 \n", 413 | "1 6.0 0.0 \n", 414 | "2 6.0 0.0 \n", 415 | "3 6.0 0.0 \n", 416 | "4 6.0 0.0 \n", 417 | "\n", 418 | "[5 rows x 36 columns]" 419 | ] 420 | }, 421 | "execution_count": 8, 422 | "metadata": {}, 423 | "output_type": "execute_result" 424 | } 425 | ], 426 | "source": [ 427 | "data.to_dataframe().head()" 428 | ] 429 | }, 430 | { 431 | "cell_type": "markdown", 432 | "metadata": {}, 433 | "source": [ 434 | "Once you have defined the feature vector, you can use `get_offline_features()` to generate the vector dataset and return it as a dataframe or materialize it into a file (CSV or Parquet). The next part demonstrates how to retrieve a vector, materialize it, and view its results." 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "metadata": {}, 440 | "source": [ 441 | "## Building and Running an Automated Training and Validation Pipeline\n", 442 | "\n", 443 | "MLRun allows the building of distributed ML pipelines that can handle data processing, automated feature selection, training, optimization, testing, deployments, and so on. Pipelines are composed of steps that run or deploy custom or library (from the MLRun hub) serverless functions. Pipelines can be run locally (for debugging or small-scale tasks), on a scalable Kubernetes cluster (using Kubeflow), or in a CI/CD system.\n", 444 | "\n", 445 | "The example consists of the following pipeline steps (all using pre-defined MLRun hub functions):\n", 446 | "\n", 447 | "1. Materialize a feature vector (using `src/get_vector`). \n", 448 | "2. Select the most optimal features (using `hub://feature_selection`).\n", 449 | "3. Train the model with multiple algorithms (using `hub://auto_trainer`).\n", 450 | "4. Evaluate the model (using `hub://auto_trainer`).\n", 451 | "5. Deploy the model and its application to the test cluster (using `hub://v2_model_server`). The next section will explain the model and application pipeline in detail.\n", 452 | "\n", 453 | "Each step can accept the previous steps’ results or data, and generate results, multiple visual artifacts/charts, versioned data objects, and registered models.\n", 454 | "\n", 455 | "We have defined the workflow in [`src/new_train_workflow.py`](./src/new_train_workflow.py). " 456 | ] 457 | }, 458 | { 459 | "cell_type": "markdown", 460 | "metadata": { 461 | "tags": [] 462 | }, 463 | "source": [ 464 | "## Running the ML pipeline" 465 | ] 466 | }, 467 | { 468 | "cell_type": "markdown", 469 | "metadata": {}, 470 | "source": [ 471 | "The workflow/pipeline can be executed using the MLRun SDK (`project.run()` method) or using CLI commands (mlrun project), and can run directly from the source repo (GIT). See details in [MLRun Projects and Automation documentation](https://docs.mlrun.org/en/stable/projects/project.html).\n", 472 | "\n", 473 | "You can set arguments and destinations for the different artifacts when you run the workflow. The pipeline progress and results are shown in the notebook. Alternatively, you can check the progress, logs, artifacts, and more, in the MLRun UI or the CI/CD system. The next part demonstrates how to run the pipeline with custom arguments using the SDK." 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": null, 479 | "metadata": { 480 | "tags": [] 481 | }, 482 | "outputs": [ 483 | { 484 | "name": "stdout", 485 | "output_type": "stream", 486 | "text": [ 487 | "> 2024-10-08 13:20:13,879 [warning] WARNING!, You seem to have uncommitted git changes, use .push()\n" 488 | ] 489 | }, 490 | { 491 | "name": "stderr", 492 | "output_type": "stream", 493 | "text": [ 494 | "Missing type name was inferred as \"JsonArray\" based on the value \"[]\".\n" 495 | ] 496 | }, 497 | { 498 | "name": "stdout", 499 | "output_type": "stream", 500 | "text": [ 501 | "> 2024-10-08 13:20:14,738 [info] Pipeline submitted successfully: {\"id\":\"e27f0d6d-8f14-4642-abee-43b9867631ef\",\"pipeline_name\":\"fraud-demo-felipe-main 2024-10-08 13-20-14\"}\n", 502 | "> 2024-10-08 13:20:14,738 [info] Pipeline run id=e27f0d6d-8f14-4642-abee-43b9867631ef, check UI for progress\n" 503 | ] 504 | }, 505 | { 506 | "data": { 507 | "text/html": [ 508 | "Workflow started in project fraud-demo-felipe id=e27f0d6d-8f14-4642-abee-43b9867631ef" 509 | ], 510 | "text/plain": [ 511 | "" 512 | ] 513 | }, 514 | "metadata": {}, 515 | "output_type": "display_data" 516 | }, 517 | { 518 | "data": { 519 | "text/html": [ 520 | "
Pipeline running (id=e27f0d6d-8f14-4642-abee-43b9867631ef), click here to view the details in MLRun UI
" 521 | ], 522 | "text/plain": [ 523 | "" 524 | ] 525 | }, 526 | "metadata": {}, 527 | "output_type": "display_data" 528 | }, 529 | { 530 | "data": { 531 | "image/svg+xml": [ 532 | "\n", 533 | "\n", 535 | "\n", 537 | "\n", 538 | "\n", 540 | "\n", 541 | "kfp\n", 542 | "\n", 543 | "\n", 544 | "\n" 545 | ], 546 | "text/plain": [ 547 | "" 548 | ] 549 | }, 550 | "metadata": {}, 551 | "output_type": "display_data" 552 | }, 553 | { 554 | "name": "stdout", 555 | "output_type": "stream", 556 | "text": [ 557 | "> 2024-10-08 13:20:14,810 [info] Started run workflow fraud-demo-felipe-main with run id = 'e27f0d6d-8f14-4642-abee-43b9867631ef' by kfp engine\n", 558 | "> 2024-10-08 13:20:14,811 [info] Waiting for pipeline run completion: {\"project\":\"\",\"run_id\":\"e27f0d6d-8f14-4642-abee-43b9867631ef\"}\n" 559 | ] 560 | }, 561 | { 562 | "data": { 563 | "image/svg+xml": [ 564 | "\n", 565 | "\n", 567 | "\n", 569 | "\n", 570 | "\n", 572 | "\n", 573 | "kfp\n", 574 | "\n", 575 | "\n", 576 | "\n", 577 | "fraud-detection-pipeline-6l7vc-2645070770\n", 578 | "\n", 579 | "evaluate\n", 580 | "\n", 581 | "\n", 582 | "\n", 583 | "fraud-detection-pipeline-6l7vc-3318924573\n", 584 | "\n", 585 | "get-vector\n", 586 | "\n", 587 | "\n", 588 | "\n", 589 | "fraud-detection-pipeline-6l7vc-3653472374\n", 590 | "\n", 591 | "feature-selection\n", 592 | "\n", 593 | "\n", 594 | "\n", 595 | "fraud-detection-pipeline-6l7vc-3318924573->fraud-detection-pipeline-6l7vc-3653472374\n", 596 | "\n", 597 | "\n", 598 | "\n", 599 | "\n", 600 | "\n", 601 | "fraud-detection-pipeline-6l7vc-67864529\n", 602 | "\n", 603 | "train\n", 604 | "\n", 605 | "\n", 606 | "\n", 607 | "fraud-detection-pipeline-6l7vc-3653472374->fraud-detection-pipeline-6l7vc-67864529\n", 608 | "\n", 609 | "\n", 610 | "\n", 611 | "\n", 612 | "\n", 613 | "fraud-detection-pipeline-6l7vc-67864529->fraud-detection-pipeline-6l7vc-2645070770\n", 614 | "\n", 615 | "\n", 616 | "\n", 617 | "\n", 618 | "\n", 619 | "fraud-detection-pipeline-6l7vc-922820315\n", 620 | "\n", 621 | "\n", 622 | "\n", 623 | "\n", 624 | "deploy-serving\n", 625 | "\n", 626 | "\n", 627 | "\n", 628 | "fraud-detection-pipeline-6l7vc-67864529->fraud-detection-pipeline-6l7vc-922820315\n", 629 | "\n", 630 | "\n", 631 | "\n", 632 | "\n", 633 | "\n" 634 | ], 635 | "text/plain": [ 636 | "" 637 | ] 638 | }, 639 | "metadata": {}, 640 | "output_type": "display_data" 641 | }, 642 | { 643 | "data": { 644 | "text/html": [ 645 | "

Run Results

[info] Workflow e27f0d6d-8f14-4642-abee-43b9867631ef finished, state=Succeeded


click the hyper links below to see detailed results
\n", 646 | " \n", 647 | " \n", 648 | " \n", 649 | " \n", 650 | " \n", 651 | " \n", 652 | " \n", 653 | " \n", 654 | " \n", 655 | " \n", 656 | " \n", 657 | " \n", 658 | " \n", 659 | " \n", 660 | " \n", 661 | " \n", 662 | " \n", 663 | " \n", 664 | " \n", 665 | " \n", 666 | " \n", 667 | " \n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | " \n", 672 | " \n", 673 | " \n", 674 | " \n", 675 | " \n", 676 | " \n", 677 | " \n", 678 | " \n", 679 | " \n", 680 | " \n", 681 | " \n", 682 | " \n", 683 | " \n", 684 | " \n", 685 | " \n", 686 | " \n", 687 | " \n", 688 | " \n", 689 | " \n", 690 | " \n", 691 | " \n", 692 | " \n", 693 | " \n", 694 | " \n", 695 | "
uidstartstatekindnameparametersresults
Oct 08 13:21:44completedrunevaluate
label_columns=label
model=store://artifacts/fraud-demo-felipe/transaction_fraud_rf:latest@e27f0d6d-8f14-4642-abee-43b9867631ef
drop_columns=label
evaluation_accuracy=0.9905
evaluation_f1_score=0.17391304347826086
evaluation_precision_score=0.4
evaluation_recall_score=0.1111111111111111
Oct 08 13:21:10completedruntrain
sample=-1
label_column=label
test_size=0.1
best_iteration=1
accuracy=0.9905
f1_score=0.17391304347826086
precision_score=0.4
recall_score=0.1111111111111111
Oct 08 13:20:42completedrunfeature-selection
output_vector_name=short
label_column=label
k=18
min_votes=2
ignore_type_errors=True
top_features_vector=store://feature-vectors/fraud-demo-felipe/short
Oct 08 13:20:21completedrunget-vector
feature_vector=transactions-fraud
features=['events.*', 'transactions.amount_max_2h', 'transactions.amount_sum_2h', 'transactions.amount_count_2h', 'transactions.amount_avg_2h', 'transactions.amount_max_12h', 'transactions.amount_sum_12h', 'transactions.amount_count_12h', 'transactions.amount_avg_12h', 'transactions.amount_max_24h', 'transactions.amount_sum_24h', 'transactions.amount_count_24h', 'transactions.amount_avg_24h', 'transactions.es_transportation_sum_14d', 'transactions.es_health_sum_14d', 'transactions.es_otherservices_sum_14d', 'transactions.es_food_sum_14d', 'transactions.es_hotelservices_sum_14d', 'transactions.es_barsandrestaurants_sum_14d', 'transactions.es_tech_sum_14d', 'transactions.es_sportsandtoys_sum_14d', 'transactions.es_wellnessandbeauty_sum_14d', 'transactions.es_hyper_sum_14d', 'transactions.es_fashion_sum_14d', 'transactions.es_home_sum_14d', 'transactions.es_travel_sum_14d', 'transactions.es_leisure_sum_14d', 'transactions.gender_F', 'transactions.gender_M', 'transactions.step', 'transactions.amount', 'transactions.timestamp_hour', 'transactions.timestamp_day_of_week']
label_feature=labels.label
target={'name': 'parquet', 'kind': 'parquet'}
update_stats=True
return=
" 696 | ], 697 | "text/plain": [ 698 | "" 699 | ] 700 | }, 701 | "metadata": {}, 702 | "output_type": "display_data" 703 | } 704 | ], 705 | "source": [ 706 | "import json\n", 707 | "run_id = project.run(\n", 708 | " 'main',\n", 709 | " arguments={'vector_name':\"transactions-fraud\",\n", 710 | " 'features': json.dumps(features),\n", 711 | " 'label_column':\"labels.label\",\n", 712 | " }, \n", 713 | " dirty=True, watch=True, engine='remote')" 714 | ] 715 | }, 716 | { 717 | "cell_type": "markdown", 718 | "metadata": {}, 719 | "source": [ 720 | "## Test the model endpoint\n" 721 | ] 722 | }, 723 | { 724 | "cell_type": "markdown", 725 | "metadata": {}, 726 | "source": [ 727 | "Now that your model is deployed using the pipeline, you can invoke it as usual:" 728 | ] 729 | }, 730 | { 731 | "cell_type": "code", 732 | "execution_count": 10, 733 | "metadata": {}, 734 | "outputs": [ 735 | { 736 | "name": "stdout", 737 | "output_type": "stream", 738 | "text": [ 739 | "> 2024-10-08 13:41:17,914 [info] Invoking function: {\"method\":\"POST\",\"path\":\"http://nuclio-fraud-demo-felipe-serving.default-tenant.svc.cluster.local:8080/v2/models/fraud/infer\"}\n" 740 | ] 741 | }, 742 | { 743 | "data": { 744 | "text/plain": [ 745 | "{'id': 'c9b5036c-8957-48dc-adba-d7f60ccd7812',\n", 746 | " 'model_name': 'fraud',\n", 747 | " 'outputs': [0],\n", 748 | " 'timestamp': '2024-10-08 13:41:17.934699+00:00',\n", 749 | " 'model_version': 'latest'}" 750 | ] 751 | }, 752 | "execution_count": 10, 753 | "metadata": {}, 754 | "output_type": "execute_result" 755 | } 756 | ], 757 | "source": [ 758 | "# Define your serving function\n", 759 | "serving_fn = project.get_function('serving')\n", 760 | "\n", 761 | "# Choose an id for your test\n", 762 | "sample_id = 'C1000148617'\n", 763 | "model_inference_path = '/v2/models/fraud/infer'\n", 764 | "\n", 765 | "# Send our sample ID for predcition\n", 766 | "serving_fn.invoke(path=model_inference_path,\n", 767 | " body={'inputs': [[sample_id]]})" 768 | ] 769 | }, 770 | { 771 | "cell_type": "markdown", 772 | "metadata": {}, 773 | "source": [ 774 | "## Done!\n", 775 | "\n", 776 | "You've completed part 4 - the model training with the feature store.\n", 777 | "Proceed to [Part 5](06-real-time-serving-pipeline.ipynb) to learn how to deploy real-time application pipelines." 778 | ] 779 | } 780 | ], 781 | "metadata": { 782 | "kernelspec": { 783 | "display_name": "Python 3 (ipykernel)", 784 | "language": "python", 785 | "name": "python3" 786 | }, 787 | "language_info": { 788 | "codemirror_mode": { 789 | "name": "ipython", 790 | "version": 3 791 | }, 792 | "file_extension": ".py", 793 | "mimetype": "text/x-python", 794 | "name": "python", 795 | "nbconvert_exporter": "python", 796 | "pygments_lexer": "ipython3", 797 | "version": "3.11.5" 798 | } 799 | }, 800 | "nbformat": 4, 801 | "nbformat_minor": 4 802 | } 803 | -------------------------------------------------------------------------------- /02-interactive-data-preparation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Interactive Data Preparation\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Before training the model, you should clean the data and create meaningful features that will be good predictors for the target variable (was there a fraud?). The `interactive-data-prep.ipynb` notebook demonstrates how to interactively build features for training the model. While this approach is simple, it is unsuitable for production environments with continuous data ingestion, large scale, or real-time. In the next section, you will implement the same logic for production using a feature store." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "The training set is built from three datasets: credit transactions, user events, and labels indicating if there was fraud. In this example, we prepare each dataset separately and combine them later for training." 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## Preparing the Credit Transaction Dataset" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "The following transformations create more meaningful features, which can have a more significant impact on the prediction than the raw data:\n", 36 | " \n", 37 | "- Extracting the date components (hour, day of week) from the timestamp.\n", 38 | "- One-hot encoding for the age groups, transaction category, and the gender.\n", 39 | "- Aggregating the amount (avg., sum, count, max over 2/12/24 hour time win‐ dows).\n", 40 | "- Aggregating the transactions per category (over 14 days time windows).\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "#### Building categorical features" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 12, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/html": [ 58 | "
\n", 59 | "\n", 72 | "\n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | " \n", 131 | " \n", 132 | " \n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | "
stepagegenderzipcodeOrizipMerchantcategoryamountfraudtimestampsourcetargetdevice
274633915F2800728007es_transportation26.9202024-10-06 07:02:33.778149000C1022153336M182307268733832bb8607545df97632a7ab02d69c4
286902942M2800728007es_transportation48.2202024-10-06 07:02:52.071774913C1006176917M348934600fadd829c49e74ffa86c8da3be75ada53
4169981313M2800728007es_transportation17.5602024-10-06 07:02:57.178944939C1010936270M34893460058d0422a50bc40c89d2b4977b2f1beea
\n", 138 | "
" 139 | ], 140 | "text/plain": [ 141 | " step age gender zipcodeOri zipMerchant category amount \\\n", 142 | "274633 91 5 F 28007 28007 es_transportation 26.92 \n", 143 | "286902 94 2 M 28007 28007 es_transportation 48.22 \n", 144 | "416998 131 3 M 28007 28007 es_transportation 17.56 \n", 145 | "\n", 146 | " fraud timestamp source target \\\n", 147 | "274633 0 2024-10-06 07:02:33.778149000 C1022153336 M1823072687 \n", 148 | "286902 0 2024-10-06 07:02:52.071774913 C1006176917 M348934600 \n", 149 | "416998 0 2024-10-06 07:02:57.178944939 C1010936270 M348934600 \n", 150 | "\n", 151 | " device \n", 152 | "274633 33832bb8607545df97632a7ab02d69c4 \n", 153 | "286902 fadd829c49e74ffa86c8da3be75ada53 \n", 154 | "416998 58d0422a50bc40c89d2b4977b2f1beea " 155 | ] 156 | }, 157 | "execution_count": 12, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "import pandas as pd\n", 164 | "from src.date_adjust import adjust_data_timespan\n", 165 | "import mlrun\n", 166 | "\n", 167 | "# Fetch the transactions and event datasets from mlrun data samples \n", 168 | "data_path = mlrun.get_sample_path(\"data/fraud-demo-mlrun-fs-docs/\")\n", 169 | "transactions_data = pd.read_csv(data_path + \"data.csv\", parse_dates=['timestamp'])\n", 170 | "\n", 171 | "# use only first 10k\n", 172 | "transactions_data = transactions_data.sort_values(by='source', axis=0)[:10000]\n", 173 | "\n", 174 | "# Adjust the samples timestamp for the past 2 days\n", 175 | "transactions_data = adjust_data_timespan(transactions_data, new_period='2d')\n", 176 | "\n", 177 | "# Preview\n", 178 | "transactions_data.head(3)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 13, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "Index(['step', 'age', 'gender', 'zipcodeOri', 'zipMerchant', 'category',\n", 190 | " 'amount', 'fraud', 'timestamp', 'source', 'target', 'device'],\n", 191 | " dtype='object')" 192 | ] 193 | }, 194 | "execution_count": 13, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "transactions_data.columns" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "The next part is aggregating the transaction amounts by time windows and transaction categories, providing you with a long list of derived features that can potentially help make better predictions." 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 14, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "data": { 217 | "text/html": [ 218 | "
\n", 219 | "\n", 232 | "\n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | " \n", 237 | " \n", 238 | " \n", 239 | " \n", 240 | " \n", 241 | " \n", 242 | " \n", 243 | " \n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \n", 251 | " \n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | " \n", 284 | " \n", 285 | " \n", 286 | " \n", 287 | " \n", 288 | " \n", 289 | " \n", 290 | " \n", 291 | " \n", 292 | " \n", 293 | " \n", 294 | " \n", 295 | " \n", 296 | " \n", 297 | " \n", 298 | " \n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | " \n", 306 | " \n", 307 | " \n", 308 | " \n", 309 | " \n", 310 | " \n", 311 | " \n", 312 | " \n", 313 | " \n", 314 | " \n", 315 | " \n", 316 | " \n", 317 | " \n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " \n", 322 | " \n", 323 | " \n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | " \n", 345 | " \n", 346 | " \n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | " \n", 379 | " \n", 380 | " \n", 381 | "
stepagezipcodeOrizipMerchantamountfraudtimestampsourcetargetdevice...category_es_hypercategory_es_leisurecategory_es_otherservicescategory_es_sportsandtoyscategory_es_techcategory_es_transportationcategory_es_travelcategory_es_wellnessandbeautygender_Fgender_M
274633915280072800726.9202024-10-06 07:02:33.778149000C1022153336M182307268733832bb8607545df97632a7ab02d69c4...FalseFalseFalseFalseFalseTrueFalseFalseTrueFalse
286902942280072800748.2202024-10-06 07:02:52.071774913C1006176917M348934600fadd829c49e74ffa86c8da3be75ada53...FalseFalseFalseFalseFalseTrueFalseFalseFalseTrue
4169981313280072800717.5602024-10-06 07:02:57.178944939C1010936270M34893460058d0422a50bc40c89d2b4977b2f1beea...FalseFalseFalseFalseFalseTrueFalseFalseFalseTrue
334543108428007280074.5002024-10-06 07:03:09.471696118C1033736586M182307268730b269ae55984e5584f1dd5f642ac1a3...FalseFalseFalseFalseFalseTrueFalseFalseTrueFalse
21064772428007280071.8302024-10-06 07:03:43.360778001C1019071188M34893460097bee3503a984f59aa6139b59f933c0b...FalseFalseFalseFalseFalseTrueFalseFalseFalseTrue
\n", 382 | "

5 rows × 30 columns

\n", 383 | "
" 384 | ], 385 | "text/plain": [ 386 | " step age zipcodeOri zipMerchant amount fraud \\\n", 387 | "274633 91 5 28007 28007 26.92 0 \n", 388 | "286902 94 2 28007 28007 48.22 0 \n", 389 | "416998 131 3 28007 28007 17.56 0 \n", 390 | "334543 108 4 28007 28007 4.50 0 \n", 391 | "210647 72 4 28007 28007 1.83 0 \n", 392 | "\n", 393 | " timestamp source target \\\n", 394 | "274633 2024-10-06 07:02:33.778149000 C1022153336 M1823072687 \n", 395 | "286902 2024-10-06 07:02:52.071774913 C1006176917 M348934600 \n", 396 | "416998 2024-10-06 07:02:57.178944939 C1010936270 M348934600 \n", 397 | "334543 2024-10-06 07:03:09.471696118 C1033736586 M1823072687 \n", 398 | "210647 2024-10-06 07:03:43.360778001 C1019071188 M348934600 \n", 399 | "\n", 400 | " device ... category_es_hyper \\\n", 401 | "274633 33832bb8607545df97632a7ab02d69c4 ... False \n", 402 | "286902 fadd829c49e74ffa86c8da3be75ada53 ... False \n", 403 | "416998 58d0422a50bc40c89d2b4977b2f1beea ... False \n", 404 | "334543 30b269ae55984e5584f1dd5f642ac1a3 ... False \n", 405 | "210647 97bee3503a984f59aa6139b59f933c0b ... False \n", 406 | "\n", 407 | " category_es_leisure category_es_otherservices \\\n", 408 | "274633 False False \n", 409 | "286902 False False \n", 410 | "416998 False False \n", 411 | "334543 False False \n", 412 | "210647 False False \n", 413 | "\n", 414 | " category_es_sportsandtoys category_es_tech \\\n", 415 | "274633 False False \n", 416 | "286902 False False \n", 417 | "416998 False False \n", 418 | "334543 False False \n", 419 | "210647 False False \n", 420 | "\n", 421 | " category_es_transportation category_es_travel \\\n", 422 | "274633 True False \n", 423 | "286902 True False \n", 424 | "416998 True False \n", 425 | "334543 True False \n", 426 | "210647 True False \n", 427 | "\n", 428 | " category_es_wellnessandbeauty gender_F gender_M \n", 429 | "274633 False True False \n", 430 | "286902 False False True \n", 431 | "416998 False False True \n", 432 | "334543 False True False \n", 433 | "210647 False False True \n", 434 | "\n", 435 | "[5 rows x 30 columns]" 436 | ] 437 | }, 438 | "execution_count": 14, 439 | "metadata": {}, 440 | "output_type": "execute_result" 441 | } 442 | ], 443 | "source": [ 444 | "processed_transactions = transactions_data\n", 445 | "\n", 446 | "# Generate day and hour columns from the timestamp\n", 447 | "processed_transactions['day_of_week'] = processed_transactions['timestamp'].dt.weekday\n", 448 | "processed_transactions['hour'] = processed_transactions['timestamp'].dt.hour\n", 449 | "\n", 450 | "# Map age groups\n", 451 | "processed_transactions[\"age_mapped\"] = processed_transactions[\"age\"].map(\n", 452 | " lambda x: {'U': '0'}.get(x, x)\n", 453 | ")\n", 454 | "\n", 455 | "# encode categories and gender groups (using one hot encoding)\n", 456 | "processed_transactions = pd.get_dummies(processed_transactions, columns=['category', 'gender'])\n", 457 | "processed_transactions.head()" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": 15, 463 | "metadata": {}, 464 | "outputs": [], 465 | "source": [ 466 | "transactions_for_agg = processed_transactions.set_index(['timestamp'],)\n", 467 | "\n", 468 | "# Group/Aggregate amount stats (mean, max, ..) by time windows\n", 469 | "windows=['2H', '12H', '24H']\n", 470 | "operation = ['mean','sum', 'count','max']\n", 471 | "for window in windows:\n", 472 | " for op in operation:\n", 473 | " processed_transactions[f'amount_{op}_{window}'] = transactions_for_agg.groupby(['source', pd.Grouper(freq=window)])['amount'].transform(op).values" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 16, 479 | "metadata": {}, 480 | "outputs": [ 481 | { 482 | "data": { 483 | "text/html": [ 484 | "
\n", 485 | "\n", 498 | "\n", 499 | " \n", 500 | " \n", 501 | " \n", 502 | " \n", 503 | " \n", 504 | " \n", 505 | " \n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | " \n", 512 | " \n", 513 | " \n", 514 | " \n", 515 | " \n", 516 | " \n", 517 | " \n", 518 | " \n", 519 | " \n", 520 | " \n", 521 | " \n", 522 | " \n", 523 | " \n", 524 | " \n", 525 | " \n", 526 | " \n", 527 | " \n", 528 | " \n", 529 | " \n", 530 | " \n", 531 | " \n", 532 | " \n", 533 | " \n", 534 | " \n", 535 | " \n", 536 | " \n", 537 | " \n", 538 | " \n", 539 | " \n", 540 | " \n", 541 | " \n", 542 | " \n", 543 | " \n", 544 | " \n", 545 | " \n", 546 | " \n", 547 | " \n", 548 | " \n", 549 | " \n", 550 | " \n", 551 | " \n", 552 | " \n", 553 | " \n", 554 | " \n", 555 | " \n", 556 | " \n", 557 | " \n", 558 | " \n", 559 | " \n", 560 | " \n", 561 | " \n", 562 | " \n", 563 | " \n", 564 | " \n", 565 | " \n", 566 | " \n", 567 | " \n", 568 | " \n", 569 | " \n", 570 | " \n", 571 | " \n", 572 | " \n", 573 | " \n", 574 | " \n", 575 | " \n", 576 | " \n", 577 | " \n", 578 | " \n", 579 | " \n", 580 | " \n", 581 | " \n", 582 | " \n", 583 | " \n", 584 | " \n", 585 | " \n", 586 | " \n", 587 | " \n", 588 | " \n", 589 | " \n", 590 | " \n", 591 | " \n", 592 | " \n", 593 | " \n", 594 | " \n", 595 | " \n", 596 | " \n", 597 | " \n", 598 | " \n", 599 | " \n", 600 | " \n", 601 | " \n", 602 | " \n", 603 | " \n", 604 | " \n", 605 | " \n", 606 | " \n", 607 | " \n", 608 | " \n", 609 | " \n", 610 | " \n", 611 | " \n", 612 | " \n", 613 | " \n", 614 | " \n", 615 | " \n", 616 | " \n", 617 | " \n", 618 | " \n", 619 | " \n", 620 | " \n", 621 | " \n", 622 | " \n", 623 | " \n", 624 | " \n", 625 | " \n", 626 | " \n", 627 | " \n", 628 | " \n", 629 | " \n", 630 | " \n", 631 | " \n", 632 | " \n", 633 | " \n", 634 | " \n", 635 | " \n", 636 | " \n", 637 | " \n", 638 | " \n", 639 | " \n", 640 | " \n", 641 | " \n", 642 | " \n", 643 | " \n", 644 | " \n", 645 | " \n", 646 | " \n", 647 | " \n", 648 | " \n", 649 | " \n", 650 | " \n", 651 | " \n", 652 | " \n", 653 | " \n", 654 | " \n", 655 | " \n", 656 | " \n", 657 | " \n", 658 | " \n", 659 | " \n", 660 | " \n", 661 | " \n", 662 | " \n", 663 | " \n", 664 | " \n", 665 | " \n", 666 | " \n", 667 | " \n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | "
stepagezipcodeOrizipMerchantamountfraudtimestamptargetdeviceday_of_week...es_barsandrestaurants_sum_14Des_tech_sum_14Des_sportsandtoys_sum_14Des_wellnessandbeauty_sum_14Des_hyper_sum_14Des_fashion_sum_14Des_home_sum_14Des_contents_sum_14Des_travel_sum_14Des_leisure_sum_14D
source
C1022153336915280072800726.9202024-10-06 07:02:33.778149000M182307268733832bb8607545df97632a7ab02d69c46...1111010000
C1006176917942280072800748.2202024-10-06 07:02:52.071774913M348934600fadd829c49e74ffa86c8da3be75ada536...4011020000
C10109362701313280072800717.5602024-10-06 07:02:57.178944939M34893460058d0422a50bc40c89d2b4977b2f1beea6...4006600000
C1033736586108428007280074.5002024-10-06 07:03:09.471696118M182307268730b269ae55984e5584f1dd5f642ac1a36...3201302010
C101907118872428007280071.8302024-10-06 07:03:43.360778001M34893460097bee3503a984f59aa6139b59f933c0b6...1000140110
\n", 672 | "

5 rows × 56 columns

\n", 673 | "
" 674 | ], 675 | "text/plain": [ 676 | " step age zipcodeOri zipMerchant amount fraud \\\n", 677 | "source \n", 678 | "C1022153336 91 5 28007 28007 26.92 0 \n", 679 | "C1006176917 94 2 28007 28007 48.22 0 \n", 680 | "C1010936270 131 3 28007 28007 17.56 0 \n", 681 | "C1033736586 108 4 28007 28007 4.50 0 \n", 682 | "C1019071188 72 4 28007 28007 1.83 0 \n", 683 | "\n", 684 | " timestamp target \\\n", 685 | "source \n", 686 | "C1022153336 2024-10-06 07:02:33.778149000 M1823072687 \n", 687 | "C1006176917 2024-10-06 07:02:52.071774913 M348934600 \n", 688 | "C1010936270 2024-10-06 07:02:57.178944939 M348934600 \n", 689 | "C1033736586 2024-10-06 07:03:09.471696118 M1823072687 \n", 690 | "C1019071188 2024-10-06 07:03:43.360778001 M348934600 \n", 691 | "\n", 692 | " device day_of_week ... \\\n", 693 | "source ... \n", 694 | "C1022153336 33832bb8607545df97632a7ab02d69c4 6 ... \n", 695 | "C1006176917 fadd829c49e74ffa86c8da3be75ada53 6 ... \n", 696 | "C1010936270 58d0422a50bc40c89d2b4977b2f1beea 6 ... \n", 697 | "C1033736586 30b269ae55984e5584f1dd5f642ac1a3 6 ... \n", 698 | "C1019071188 97bee3503a984f59aa6139b59f933c0b 6 ... \n", 699 | "\n", 700 | " es_barsandrestaurants_sum_14D es_tech_sum_14D \\\n", 701 | "source \n", 702 | "C1022153336 1 1 \n", 703 | "C1006176917 4 0 \n", 704 | "C1010936270 4 0 \n", 705 | "C1033736586 3 2 \n", 706 | "C1019071188 1 0 \n", 707 | "\n", 708 | " es_sportsandtoys_sum_14D es_wellnessandbeauty_sum_14D \\\n", 709 | "source \n", 710 | "C1022153336 1 1 \n", 711 | "C1006176917 1 1 \n", 712 | "C1010936270 0 6 \n", 713 | "C1033736586 0 1 \n", 714 | "C1019071188 0 0 \n", 715 | "\n", 716 | " es_hyper_sum_14D es_fashion_sum_14D es_home_sum_14D \\\n", 717 | "source \n", 718 | "C1022153336 0 1 0 \n", 719 | "C1006176917 0 2 0 \n", 720 | "C1010936270 6 0 0 \n", 721 | "C1033736586 3 0 2 \n", 722 | "C1019071188 1 4 0 \n", 723 | "\n", 724 | " es_contents_sum_14D es_travel_sum_14D es_leisure_sum_14D \n", 725 | "source \n", 726 | "C1022153336 0 0 0 \n", 727 | "C1006176917 0 0 0 \n", 728 | "C1010936270 0 0 0 \n", 729 | "C1033736586 0 1 0 \n", 730 | "C1019071188 1 1 0 \n", 731 | "\n", 732 | "[5 rows x 56 columns]" 733 | ] 734 | }, 735 | "execution_count": 16, 736 | "metadata": {}, 737 | "output_type": "execute_result" 738 | } 739 | ], 740 | "source": [ 741 | "# Group/Aggregate amount stats (mean, max, ..) by transaction category\n", 742 | "main_categories = [\"es_transportation\", \"es_health\", \"es_otherservices\",\n", 743 | " \"es_food\", \"es_hotelservices\", \"es_barsandrestaurants\",\n", 744 | " \"es_tech\", \"es_sportsandtoys\", \"es_wellnessandbeauty\",\n", 745 | " \"es_hyper\", \"es_fashion\", \"es_home\", \"es_contents\",\n", 746 | " \"es_travel\", \"es_leisure\"]\n", 747 | "for category in main_categories:\n", 748 | " processed_transactions[f'{category}_sum_14D'] = transactions_for_agg.groupby(['source', pd.Grouper(freq='14D')])[f'category_{category}'].transform('sum').values\n", 749 | "\n", 750 | "processed_transactions.set_index(['source'], inplace=True)\n", 751 | "processed_transactions.head()" 752 | ] 753 | }, 754 | { 755 | "cell_type": "code", 756 | "execution_count": 17, 757 | "metadata": {}, 758 | "outputs": [ 759 | { 760 | "data": { 761 | "text/plain": [ 762 | "step int64\n", 763 | "age object\n", 764 | "zipcodeOri int64\n", 765 | "zipMerchant int64\n", 766 | "amount float64\n", 767 | "fraud int64\n", 768 | "timestamp datetime64[ns]\n", 769 | "target object\n", 770 | "device object\n", 771 | "day_of_week int32\n", 772 | "hour int32\n", 773 | "age_mapped object\n", 774 | "category_es_barsandrestaurants bool\n", 775 | "category_es_contents bool\n", 776 | "category_es_fashion bool\n", 777 | "category_es_food bool\n", 778 | "category_es_health bool\n", 779 | "category_es_home bool\n", 780 | "category_es_hotelservices bool\n", 781 | "category_es_hyper bool\n", 782 | "category_es_leisure bool\n", 783 | "category_es_otherservices bool\n", 784 | "category_es_sportsandtoys bool\n", 785 | "category_es_tech bool\n", 786 | "category_es_transportation bool\n", 787 | "category_es_travel bool\n", 788 | "category_es_wellnessandbeauty bool\n", 789 | "gender_F bool\n", 790 | "gender_M bool\n", 791 | "amount_mean_2H float64\n", 792 | "amount_sum_2H float64\n", 793 | "amount_count_2H int64\n", 794 | "amount_max_2H float64\n", 795 | "amount_mean_12H float64\n", 796 | "amount_sum_12H float64\n", 797 | "amount_count_12H int64\n", 798 | "amount_max_12H float64\n", 799 | "amount_mean_24H float64\n", 800 | "amount_sum_24H float64\n", 801 | "amount_count_24H int64\n", 802 | "amount_max_24H float64\n", 803 | "es_transportation_sum_14D int64\n", 804 | "es_health_sum_14D int64\n", 805 | "es_otherservices_sum_14D int64\n", 806 | "es_food_sum_14D int64\n", 807 | "es_hotelservices_sum_14D int64\n", 808 | "es_barsandrestaurants_sum_14D int64\n", 809 | "es_tech_sum_14D int64\n", 810 | "es_sportsandtoys_sum_14D int64\n", 811 | "es_wellnessandbeauty_sum_14D int64\n", 812 | "es_hyper_sum_14D int64\n", 813 | "es_fashion_sum_14D int64\n", 814 | "es_home_sum_14D int64\n", 815 | "es_contents_sum_14D int64\n", 816 | "es_travel_sum_14D int64\n", 817 | "es_leisure_sum_14D int64\n", 818 | "dtype: object" 819 | ] 820 | }, 821 | "execution_count": 17, 822 | "metadata": {}, 823 | "output_type": "execute_result" 824 | } 825 | ], 826 | "source": [ 827 | "processed_transactions.dtypes" 828 | ] 829 | }, 830 | { 831 | "cell_type": "markdown", 832 | "metadata": {}, 833 | "source": [ 834 | "## Preparing the User Events(Activities) Dataset" 835 | ] 836 | }, 837 | { 838 | "cell_type": "markdown", 839 | "metadata": {}, 840 | "source": [ 841 | "The events dataset contains user activities such as login, change of details, or password, which can hint at a fraud attempt. The next part shows how to load the events dataset and create categorical features per event type." 842 | ] 843 | }, 844 | { 845 | "cell_type": "markdown", 846 | "metadata": {}, 847 | "source": [ 848 | "### Processing the events dataset" 849 | ] 850 | }, 851 | { 852 | "cell_type": "code", 853 | "execution_count": 18, 854 | "metadata": {}, 855 | "outputs": [ 856 | { 857 | "data": { 858 | "text/html": [ 859 | "
\n", 860 | "\n", 873 | "\n", 874 | " \n", 875 | " \n", 876 | " \n", 877 | " \n", 878 | " \n", 879 | " \n", 880 | " \n", 881 | " \n", 882 | " \n", 883 | " \n", 884 | " \n", 885 | " \n", 886 | " \n", 887 | " \n", 888 | " \n", 889 | " \n", 890 | " \n", 891 | " \n", 892 | " \n", 893 | " \n", 894 | " \n", 895 | " \n", 896 | " \n", 897 | " \n", 898 | " \n", 899 | " \n", 900 | " \n", 901 | " \n", 902 | "
sourceeventtimestamp
45553C137986193password_change2024-10-06 07:02:34.836980000
24134C1940951230details_change2024-10-06 07:02:35.885162091
64444C247537602login2024-10-06 07:02:37.539945103
\n", 903 | "
" 904 | ], 905 | "text/plain": [ 906 | " source event timestamp\n", 907 | "45553 C137986193 password_change 2024-10-06 07:02:34.836980000\n", 908 | "24134 C1940951230 details_change 2024-10-06 07:02:35.885162091\n", 909 | "64444 C247537602 login 2024-10-06 07:02:37.539945103" 910 | ] 911 | }, 912 | "execution_count": 18, 913 | "metadata": {}, 914 | "output_type": "execute_result" 915 | } 916 | ], 917 | "source": [ 918 | "# Fetch the user_events dataset from the server\n", 919 | "user_events_data = pd.read_csv(data_path + \"events.csv\", \n", 920 | " index_col=0, quotechar=\"\\'\", parse_dates=['timestamp'])\n", 921 | "\n", 922 | "# Adjust to the last 2 days to see the latest aggregations in the online feature vectors\n", 923 | "user_events_data = adjust_data_timespan(user_events_data, new_period='2d')\n", 924 | "\n", 925 | "# Preview\n", 926 | "user_events_data.head(3)" 927 | ] 928 | }, 929 | { 930 | "cell_type": "code", 931 | "execution_count": 19, 932 | "metadata": {}, 933 | "outputs": [ 934 | { 935 | "data": { 936 | "text/html": [ 937 | "
\n", 938 | "\n", 951 | "\n", 952 | " \n", 953 | " \n", 954 | " \n", 955 | " \n", 956 | " \n", 957 | " \n", 958 | " \n", 959 | " \n", 960 | " \n", 961 | " \n", 962 | " \n", 963 | " \n", 964 | " \n", 965 | " \n", 966 | " \n", 967 | " \n", 968 | " \n", 969 | " \n", 970 | " \n", 971 | " \n", 972 | " \n", 973 | " \n", 974 | " \n", 975 | " \n", 976 | " \n", 977 | " \n", 978 | " \n", 979 | " \n", 980 | " \n", 981 | " \n", 982 | " \n", 983 | " \n", 984 | " \n", 985 | " \n", 986 | " \n", 987 | " \n", 988 | " \n", 989 | " \n", 990 | " \n", 991 | " \n", 992 | " \n", 993 | " \n", 994 | " \n", 995 | " \n", 996 | " \n", 997 | " \n", 998 | " \n", 999 | " \n", 1000 | " \n", 1001 | " \n", 1002 | " \n", 1003 | " \n", 1004 | " \n", 1005 | "
timestampevent_details_changeevent_loginevent_password_change
source
C1379861932024-10-06 07:02:34.836980000FalseFalseTrue
C19409512302024-10-06 07:02:35.885162091TrueFalseFalse
C2475376022024-10-06 07:02:37.539945103FalseTrueFalse
C4700796172024-10-06 07:02:38.830394428FalseFalseTrue
C11421183592024-10-06 07:02:39.620686830FalseTrueFalse
\n", 1006 | "
" 1007 | ], 1008 | "text/plain": [ 1009 | " timestamp event_details_change event_login \\\n", 1010 | "source \n", 1011 | "C137986193 2024-10-06 07:02:34.836980000 False False \n", 1012 | "C1940951230 2024-10-06 07:02:35.885162091 True False \n", 1013 | "C247537602 2024-10-06 07:02:37.539945103 False True \n", 1014 | "C470079617 2024-10-06 07:02:38.830394428 False False \n", 1015 | "C1142118359 2024-10-06 07:02:39.620686830 False True \n", 1016 | "\n", 1017 | " event_password_change \n", 1018 | "source \n", 1019 | "C137986193 True \n", 1020 | "C1940951230 False \n", 1021 | "C247537602 False \n", 1022 | "C470079617 True \n", 1023 | "C1142118359 False " 1024 | ] 1025 | }, 1026 | "execution_count": 19, 1027 | "metadata": {}, 1028 | "output_type": "execute_result" 1029 | } 1030 | ], 1031 | "source": [ 1032 | "# Generate categorical features from the event type\n", 1033 | "processed_events = user_events_data\n", 1034 | "processed_events = pd.get_dummies(processed_events, columns=['event'])\n", 1035 | "processed_events.set_index(['source'], inplace=True)\n", 1036 | "processed_events.head()" 1037 | ] 1038 | }, 1039 | { 1040 | "cell_type": "markdown", 1041 | "metadata": {}, 1042 | "source": [ 1043 | "## Extracting Labels and Training a Model" 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "markdown", 1048 | "metadata": {}, 1049 | "source": [ 1050 | "The final step is to generate a target label column (the fraud yes/no indication) and train a basic model to evaluate your assumptions. The next part demonstrates how to create the labels dataset and use sklearn to train and evaluate a basic model." 1051 | ] 1052 | }, 1053 | { 1054 | "cell_type": "markdown", 1055 | "metadata": {}, 1056 | "source": [ 1057 | "### Label df" 1058 | ] 1059 | }, 1060 | { 1061 | "cell_type": "code", 1062 | "execution_count": 20, 1063 | "metadata": {}, 1064 | "outputs": [], 1065 | "source": [ 1066 | "def create_labels(df):\n", 1067 | " labels = df[['fraud','timestamp']].copy()\n", 1068 | " labels = labels.rename(columns={\"fraud\": \"label\"})\n", 1069 | " labels['timestamp'] = labels['timestamp'].astype(\"datetime64[ns]\")\n", 1070 | " labels['label'] = labels['label'].astype(int)\n", 1071 | " return labels" 1072 | ] 1073 | }, 1074 | { 1075 | "cell_type": "code", 1076 | "execution_count": 21, 1077 | "metadata": {}, 1078 | "outputs": [ 1079 | { 1080 | "data": { 1081 | "text/html": [ 1082 | "
\n", 1083 | "\n", 1096 | "\n", 1097 | " \n", 1098 | " \n", 1099 | " \n", 1100 | " \n", 1101 | " \n", 1102 | " \n", 1103 | " \n", 1104 | " \n", 1105 | " \n", 1106 | " \n", 1107 | " \n", 1108 | " \n", 1109 | " \n", 1110 | " \n", 1111 | " \n", 1112 | " \n", 1113 | " \n", 1114 | " \n", 1115 | " \n", 1116 | " \n", 1117 | " \n", 1118 | " \n", 1119 | " \n", 1120 | " \n", 1121 | " \n", 1122 | " \n", 1123 | " \n", 1124 | " \n", 1125 | " \n", 1126 | " \n", 1127 | " \n", 1128 | " \n", 1129 | " \n", 1130 | " \n", 1131 | " \n", 1132 | " \n", 1133 | " \n", 1134 | " \n", 1135 | " \n", 1136 | "
labeltimestamp
source
C102215333602024-10-06 07:02:33.778149000
C100617691702024-10-06 07:02:52.071774913
C101093627002024-10-06 07:02:57.178944939
C103373658602024-10-06 07:03:09.471696118
C101907118802024-10-06 07:03:43.360778001
\n", 1137 | "
" 1138 | ], 1139 | "text/plain": [ 1140 | " label timestamp\n", 1141 | "source \n", 1142 | "C1022153336 0 2024-10-06 07:02:33.778149000\n", 1143 | "C1006176917 0 2024-10-06 07:02:52.071774913\n", 1144 | "C1010936270 0 2024-10-06 07:02:57.178944939\n", 1145 | "C1033736586 0 2024-10-06 07:03:09.471696118\n", 1146 | "C1019071188 0 2024-10-06 07:03:43.360778001" 1147 | ] 1148 | }, 1149 | "execution_count": 21, 1150 | "metadata": {}, 1151 | "output_type": "execute_result" 1152 | } 1153 | ], 1154 | "source": [ 1155 | "# Create the target label dataset (fraud indication)\n", 1156 | "labels_set = create_labels(processed_transactions)\n", 1157 | "labels_set.head()" 1158 | ] 1159 | }, 1160 | { 1161 | "cell_type": "markdown", 1162 | "metadata": {}, 1163 | "source": [ 1164 | "## Train" 1165 | ] 1166 | }, 1167 | { 1168 | "cell_type": "code", 1169 | "execution_count": 22, 1170 | "metadata": {}, 1171 | "outputs": [ 1172 | { 1173 | "name": "stdout", 1174 | "output_type": "stream", 1175 | "text": [ 1176 | "Fitting 3 folds for each of 100 candidates, totalling 300 fits\n", 1177 | "Accuracy: 1.0\n", 1178 | "Precision: 1.0\n", 1179 | "Recall: 1.0\n", 1180 | "F1 Score: 1.0\n" 1181 | ] 1182 | }, 1183 | { 1184 | "data": { 1185 | "text/html": [ 1186 | "
RandomForestClassifier(bootstrap=False, max_depth=100, min_samples_leaf=2,\n",
1591 |        "                       n_estimators=50)
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.
" 1593 | ], 1594 | "text/plain": [ 1595 | "RandomForestClassifier(bootstrap=False, max_depth=100, min_samples_leaf=2,\n", 1596 | " n_estimators=50)" 1597 | ] 1598 | }, 1599 | "execution_count": 22, 1600 | "metadata": {}, 1601 | "output_type": "execute_result" 1602 | } 1603 | ], 1604 | "source": [ 1605 | "# Train a model based on the transactions, events, and labels\n", 1606 | "from src.train_sklearn import train_and_val, prepare_data_to_train\n", 1607 | "\n", 1608 | "X_train, X_test, y_train, y_test = prepare_data_to_train(processed_transactions, processed_events, labels_set)\n", 1609 | "rf_best = train_and_val(X_train, X_test, y_train, y_test)\n", 1610 | "\n", 1611 | "# print the model results (Accuracy, ..)\n", 1612 | "rf_best" 1613 | ] 1614 | }, 1615 | { 1616 | "cell_type": "markdown", 1617 | "metadata": {}, 1618 | "source": [ 1619 | "## Done!\n", 1620 | "\n", 1621 | "You've completed the second part - interactive data preparation.\n", 1622 | "Proceed to [Part 3](03-ingest-with-feature-store.ipynb) to learn how to build data ingestion services with the Feature Store." 1623 | ] 1624 | } 1625 | ], 1626 | "metadata": { 1627 | "kernelspec": { 1628 | "display_name": "Python 3 (ipykernel)", 1629 | "language": "python", 1630 | "name": "python3" 1631 | }, 1632 | "language_info": { 1633 | "codemirror_mode": { 1634 | "name": "ipython", 1635 | "version": 3 1636 | }, 1637 | "file_extension": ".py", 1638 | "mimetype": "text/x-python", 1639 | "name": "python", 1640 | "nbconvert_exporter": "python", 1641 | "pygments_lexer": "ipython3", 1642 | "version": "3.11.5" 1643 | }, 1644 | "toc-showtags": false 1645 | }, 1646 | "nbformat": 4, 1647 | "nbformat_minor": 4 1648 | } 1649 | --------------------------------------------------------------------------------