Skip to content

Instantly share code, notes, and snippets.

@banditkings
banditkings / random_sampling_examples.py
Last active June 3, 2024 17:23
Syntax for random sampling from distributions with numpy, scipy, jax, and numpyro
# --- NUMPY ---
import numpy as np
np.random.seed(42)
np.random.binomial(n=1, p=0.5, size=(10,))
# array([0, 1, 1, 1, 0, 0, 0, 1, 1, 1])
# --- SCIPY ---
import numpy as np
@banditkings
banditkings / quarto_python.md
Last active May 16, 2024 06:14
Getting quarto to use the right python version and virtual environment

Quarto with Python, Poetry, and VSCode

If you're having issues with getting pyenv + poetry + quarto + vscode to work together, the solution is to set the QUARTO_PYTHON environment variable to the location of the specific python version you want to use.

For instance, in bash we'd do the following within the project directory:

poetry shell
export QUARTO_PYTHON=$(which python)
@banditkings
banditkings / clean_annotations.py
Created May 15, 2024 23:22
Remove facet annotations from plotly
def clean_annotations(fig):
"""Removes the annoying 'Feature=' label that clutters plotly graphs when you do facet_row or facet_col"""
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[1]))
return fig
@banditkings
banditkings / wsl.md
Last active March 13, 2024 22:01
New Windows WSL2 Ubuntu Setup

Supplemental WSL2 dev setups

Been a few years since I had to make a new dev environment in WSL2, and my previous article is a little outdated from 2021.

Install Script updated

# Update apt-get if this is a fresh Linux distro install
sudo apt update && sudo apt upgrade
# Install Git and zsh
@banditkings
banditkings / weeks_from_pandas_datetime.py
Last active January 3, 2024 05:25
Create a week of year feature from a pandas datetime column
import pandas as pd
def create_week_feature(df:pd.DataFrame, date_col='DateTime'):
"""
Given a dataframe with a column `date_col` of type pd.DateTime
"""
if type(df[date_col]) != pd.DateTime:
df[date_col] = pd.to_datetime(df[date_col])
# Use the pd.Series.dt.isocalendar() method to access
@banditkings
banditkings / graphviz_dag.py
Last active December 29, 2023 22:36
Draw a DAG with `graphviz` python api
# Simple example of using graphviz to draw DAGs while working in a jupyter notebook environment
import graphviz as gr
# Simple DAG with a confounder
h = gr.Digraph('Simple DAG with confounder')
h.edge('Treatment', 'Outcome')
h.edge('Confounder', 'Treatment')
h.edge('Confounder', 'Outcome')
display(h)
@banditkings
banditkings / numpyro_boilerplate_mcmc.py
Created November 15, 2023 23:40
numpyro boilerplate MCMC example using a NUTS sampler
import jax.numpy as jnp
from jax import random
import numpyro
from numpyro.diagnostics import hpdi
import numpyro.distributions as dist
from numpyro.infer import MCMC, NUTS, Predictive
# Simulated Data
# 3000 samples, 3 coefficients
@banditkings
banditkings / timeseriesdata.py
Last active November 2, 2023 00:00
Python Time Series Datasets
# ------- STATSMODELS ---------
# Statsmodels has a few built in datasets as well as a utility for R-Datasets
# Yearly Nile River flows at Ashwan 1871-1970
import statsmodels.api as sm
df = sm.datasets.nile.load().data
df['ds'] = pd.date_range(start='1871', end='1970', freq='AS')
# Mauna Loa Weekly Atmospheric CO2 Data
import statsmodels.api as sm
@banditkings
banditkings / random_walk_anim.py
Last active October 6, 2023 23:12
Random Walk animation in Plotly
import numpy as np
import plotly.graph_objects as go
# A Random Walk animation in Plotly using a line and a marker for the current position
n = 200
# Random Walk with n points
x = np.arange(n)
i = 0
state = 0 # initial state
@banditkings
banditkings / project_scaffold.md
Last active June 7, 2023 22:28
Starting a new DS project with cookiecutter, pyenv, poetry, pytest, git

Project Scaffolding

Let's start a brand new project here with:

  • cookiecutter: project templating
  • pyenv: managing virtual environments for different python versions
  • poetry: dependency management
  • pytest: testing
  • git: version control