Skip to content

Instantly share code, notes, and snippets.

@FrankRuns
FrankRuns / ml_jitter_experiment_prompt.txt
Created January 9, 2025 12:28
Prompt to get python script experimenting with jittering input training data for an ML model.
I want a Python script demonstrating a simple approach for “shaking up” that historical data. Specifically, show me how to:
Load the Boston Housing dataset (or a similar publicly available dataset).
Split the data into training and test sets.
Add a small amount of random noise (jitter) to the training set features.
Train one linear regression model on the unmodified data and another on the jittered data.
Compare the MSE (Mean Squared Error) of each model on the same test set.
For the jitter, just use a normal distribution with a small standard deviation, something like 0.01. Then show me how the MSE differs between the original and jittered data. If the jittered version yields a lower MSE, let me know in the script output. If it’s worse, let me know that, too.
Nothing too fancy, just enough that I can make a point about how “bad data” might become surprisingly helpful when we own the uncertainty and inject it. And please include some print statements that display the MSEs. That’s it.
You are a highly capable Python programmer who has access to locations.csv, which contains columns name, longitude, latitude, and type.
Please write a Python script that does the following:
Reads locations.csv into a pandas DataFrame.
Enumerates every possible Origin–Destination (OD) pair, but skips certain flows based on the following rules (via a helper function is_valid_flow(origin_type, dest_type)):
No shipments from Plant -> Customer
No shipments from DC -> Plant
No shipments from Customer -> DC
No shipments from Customer -> Plant
name longitude latitude type
Washington DC -77.0369 38.9072 DC
Dallas TX -96.797 32.7767 DC
Los Angeles CA -118.2437 34.0522 DC
Phoenix AZ -112.074 33.4484 Plant
Charlotte NC -80.8431 35.2271 Plant
0 Washington DC -76.16186430611484 38.96475995358956 Customer
1 Washington DC -77.85084407238416 40.23905626401316 Customer
2 Washington DC -78.33383248877686 37.28207518409593 Customer
3 Washington DC -77.18345675251808 38.38733808629542 Customer
You have a CSV file called `locations.csv` with columns: name, longitude, latitude, type (including 'Customer' rows), DCs, and plants.
I want you to:
1. Filter the data to only include rows where `type == 'Customer'`.
2. Generate synthetic one-period demand for these customers:
- Normal scenario: Draw from a normal distribution (mean=100, std=20), clip negatives at 0.
Generate a Python Script for [Project Objective] Visualization with [Visualization Tools] in a Jupyter Notebook
Body:
Objective:
Clearly describe the purpose of the project, the type of data involved, and the key insights or lessons you aim to convey through visualization. Mention whether you have an existing dataset or need to generate synthetic data.
Example:
Create a Python script to visualize supply chain network scenarios using Folium maps. The visualization should compare an optimal distribution strategy (multiple Distribution Centers) versus a suboptimal one (single Distribution Center) to highlight the impact on costs and delivery times. If no data file is provided, generate synthetic data for Distribution Centers (DCs) and Customers.
Write a Python script to generate synthetic supply chain data with the following rules:
Here is the detailed description based on the supply chain network:
Distribution Centers (DCs)
Washington, DC:
Located in the northeastern United States near major population centers.
Likely serves as a key hub for East Coast distribution.
Dallas, TX:
Positioned centrally in the southern United States.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2
from scipy.spatial.distance import cdist
import matplotlib.cm as cm
# Set random seed for reproducibility
np.random.seed(42)
@FrankRuns
FrankRuns / analyze-ontime-three-models.py
Created April 28, 2024 15:19
Supporting analysis for How supply chain leaders improve on-time delivery with multiple models
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import dowhy
from dowhy import CausalModel
import networkx as nx
import math
import sklearn
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
@FrankRuns
FrankRuns / white-noise.R
Created January 23, 2024 11:20
Calculations supporting substack post called Analytics, now what?
# Replicate Bob's results from this LinkedIn post:
# https://www.linkedin.com/posts/bob-wilson-77a22ab_people-sometimes-say-ab-testing-requires-activity-7152792859878871040-X1Sr?utm_source=share&utm_medium=member_desktop
### Implement Fisher's Exact Test
# Create the contingency table
contingency_table <- matrix(c(0, 4, 7, 3), nrow = 2)
dimnames(contingency_table) <- list(c("Control", "Treatment"),
@FrankRuns
FrankRuns / data-analyst-mistakes.R
Created January 10, 2024 19:47
Script to simulate tv holiday episode data for data analysts make mistakes article
# Load Required Libraries
if (!require("MASS")) install.packages("MASS")
library(MASS)
# Define TV Shows
# A vector of TV show titles
tv_shows <- c(
"Breaking Bad", "Game of Thrones", "The Wire",
"Stranger Things", "The Crown", "Mad Men",
"The Sopranos", "Friends", "The Office",