Skip to content

Instantly share code, notes, and snippets.

/*
purchase table example
transaction_date | customer_id | purchase_price | category
2022-01-01 customer_A $10 jewelry
2022-01-02 customer_A $1 t-shirts
...
import boto3
dynamodb = boto3.resource("dynamodb")
params = {
"TableName": "customer_purchase_ts",
"KeySchema": [
{"AttributeName": "customer_id", "KeyType": "HASH"}
],
"LocalSecondaryIndexes": [
import boto3
dynamodb = boto3.resource("dynamodb")
params = {
'TableName': "customer_purchase_ts",
"KeySchema": [
{"AttributeName": "customer_id", "KeyType": "HASH"},
{"AttributeName": "transaction_date_epoch", "KeyType": "RANGE"}
],
@achad4
achad4 / batch_customer_ml_inference_with_dynamodb.py
Created February 5, 2023 16:44
Example code for batch ML inference from DynamoDB using GSI
import boto3
from datetime import datetime
import numpy as np
from fastapi import FastAPI
app = FastAPI()
dynamodb = boto3.resource("dynamodb")
s3 = boto3.client('s3')
import ...
import fire
class CustomerModel(BaseModel):
def __init__(self):
super().__init__()
def train_model(lookback_days=30: int):
from airflow.models import Variable
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
MODEL_ENTRY_POINT = "customer_model.py"
# This dynamic configuration mechanism is not ideal but makes for an easy demo
model_version = Variable.get("model_version")
@achad4
achad4 / simple_ml_dag.py
Last active April 7, 2022 22:24
Example of ML DAG using Python operator
from airflow.models import Variable
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
def train_model(lookback_days=30: int):
"""
Trains a model using data from the past <lookback_days> and persists to a model store
"""
...
@achad4
achad4 / customer_spend_anamoly_detection.sql
Last active October 20, 2023 12:32
Ex. Flagging outliers in SQL
WITH transaction AS (
SELECT transaction_id,
customer_id,
state,
amount_spent_usd
FROM < FACT_TABLE_TRANSACTION >
),
customer_spend AS (
SELECT customer_id,
state,
@achad4
achad4 / customer_spend_anamoly_detection.sql
Last active March 31, 2022 13:20
Ex. Flagging outliers based on Z-scores
WITH transaction AS (
SELECT transaction_id,
customer_id,
state,
amount_spent_usd
FROM FACT_TABLE_TRANSACTION
),
customer_spend AS (
SELECT customer_id,
state,
@achad4
achad4 / metrics_query_example.sql
Created February 14, 2022 14:00
Example of metrics query for article
WITH month_0 AS
(
SELECT user_id,
metric_a
FROM analytics_table
WHERE execution_date = < month_0 >
),
month_1 AS
(
SELECT user_id,